20080213 数据库 表 数据 比较
http://www.yippeesoft.com
有一个数据库db1,里边已经包括了一些用户表,将此作为基础条件;
数据库db2是在db1的基础上增加了另外一些表,这些表可能利用db1里边的表作为外键约束,但db1里边的表没有db2作为外键约束;并且未对db1中的基础表进行修改;
现在的要求是删除db2里边所有的新增表,与db1里边相同的表全部保留,请大家指点!
查出所有新增的表名:
select name from db2..sysobjects b where name not in(select name from db1..sysobjects where xtype = u) and xtype = u
遍历结果集,从db2里一个一个的drop掉
–删除db2的所有新增表
use db2
go
exec sp_msforeachtable
@whereand= and not exists(select * from db1.dbo.sysobjects where name collate chinese_prc_ci_as=o.name),
@command1= drop table ?
create proc procname as
select t1.wlmc,t1.wlsl,t1.bz,t2.wlmc as t2_wlmc,t2.wlsl as t2_wlsl,t2.bz a t2_bz
from t1 left join t2
on t1.dh=t2.dh where t1.wlmc<>t2.wlmc or t1.wlsl<>t2.wlsl or
t1.bz<>t2.bz
用
select a.wlmc,a.wlsl,a.bz,b.wlmc,b.wlsl,b.bz
from t1 a, t2 b
where a.dh=b.dh and (a.wlmc <> b.wlmc Or a.wlsl <> b.wlsl Or a.bz <> b.bz)
或
select a.wlmc,a.wlsl,a.bz,b.wlmc,b.wlsl,b.bz
from t1 a, t2 b
where a.dh=b.dh and
a.dh not in (select a.dh
from u1 a, u2 b
where a.dh=b.dh and a.wlmc=b.wlmc and a.wlsl=b.wlsl and a.bz=b.bz)
try
select t1.dh
, case when t1.wlmc = t2.wlmc then \’same\’ else \’different\’ end as wlmc
, case when t1.wlsl = t2.wlsl then \’same\’ else \’different\’ end as wlsl
, case when t1.bz = t2.bz then \’same\’ else \’different\’ end as bz
from t1, t2
where t1.dh=t2.dh and (t1.wlmc <> t2.wlmc Or t1.wlsl <> t2.wlsl Or t1.bz <> t2.bz)
如果你的数据库支持minus的话,那很容易了
select * from table1
minus
select * from table2
就能得到table1中存在而table2中不存在的纪录了,反之就能得到table2中存在而table1中不存在的纪录
select * from table1
where exists ( select * from table2
where table1.pkey = table2.pkey
and( table1.field1 <> table2.field1
or table1.field2 <> table2.field2
…
or table1.fieldn <> table2.fieldn));
select * —-在table1但不在table2中
from table1 as t1
where not exists(
select *
from table2 as t2
where t1.f1=t2.f1 and t1.f2=t2.f2 and ….
)
union
select * —-在table2但不在table1中
from table2 as t1
where not exists(
select *
from table1 as t2
where t1.f1=t2.f1 and t1.f2=t2.f2 and ….
)
select ID from a
union
select ID from b
联合查询方式,可以从结构相同的表中查询并删除重复的行
—-求6个字段完全对应不上的记录
select A.* from A, B where A.药品编号<>B.药品编号 and A.通用名<>B.通用名 and A.剂型<>B.剂型 and A.规格<>B.规格 and A.包装数量<>B.包装数量 and AA.生产企业<>B.生产企业
—–求6个字段完全对应上的记录
select A.* from A, B where A.药品编号=B.药品编号 and A.通用名=B.通用名 and A.剂型=B.剂型 and A.规格=B.规格 and A.包装数量=B.包装数量 and AA.生产企业=B.生产企业
select table1.* from table1,table2 where table1.field1<>table2.field1 or table1.field1=table2.field2
如果name为关键字
select * from tableB where name not in (select name from tableA)
select * from tableB
where name not in (select distinct name from tableA)
selct * from tableB where id not in (select id from tableA)
select *
from rs
where card not in (select card from down)
select count(name) [统计],bm from @rs where name not in (select name from @down) group by bm 要的就是这个
–没有打卡的人
select distinct name from rs where name not in(select name from down)
–没有打卡的人的数量
select count(distinct name) from rs where name not in(select name from down)
–没有打卡的部门
select distinct rs.bm from rs where bm not in
(select distinct rs.bm from rs , down where rs.name = down.name)
–没有打卡的部门的数量
select count(distinct rs.bm) from rs where bm not in
(select distinct rs.bm from rs , down where rs.name = down.name)
两张表table1和table2,里边的字段名一样,table2包含table1中的一部分内容,我想分别显示出两张表中相同的和不同的内容,应该怎么写?
用外连接
或者内连接就行
内连接显示交集
外连接显示出了table1有的,和table2有的,xx相同的id号
内select * from table1 a inner join table2 b on a.xx=b.xx
外select * from table1 a left outer join table2 on a.xx=b.xx
不知是不是你的意思
或者
select * from table1 a where id not in (select * from table2 b)
select * from table1 a where id in (select * from table2 b)
有几个不同点:
1 :A 表有的数据,B 表没有;
2 :A 表有的数据,B 表也有,而且所有字段信息的值都相同;
3 :A 表有的数据,B 表也有,只是有个别字段信息的值不相同;
4 :B 表有的数据,A 表没有;
现在要求是除了2之外,其他情况的数据要拿到。如何写SQL语句的速度最快?
1.select * from a
minus
select * from b
3.select * from a where not exists(select * from b where a.col1=b.col1 and a.col2=b.col2…)
4.select * from b
minus
select * from a
历史博文
- c# sdk2000 - 2009
- 20070614 彪悍的网页 木马 TrojanDownloader - 2007
- 0523 mpx220 smartphone 桌面主题 - 2006
- VSTS 1115 郁闷 女排输球 安装VSTF失败 - 2005
- PERL 时间 日期 LOCALTIME TIME HTTP::DATE - 2005