
SQL查询两个表中不同数据的步骤如下:
我们需要准备的材料分别是:电脑、sql查询器。
1、首先,打开sql查询器,连接上相应的数据库表,以查询c1表和c2表的name字段不同为例。
2、点击“查询”按钮,输入:
select c1.`name` from c1 left join c2 on c1.`name`=c2.`name` where c2.`name` is null
union select c2.`name` from c2 left join c1 on c1.`name`=c2.`name` where c1.`name` is null。
3、点击“运行”按钮,此时不同的name都被查询出了。
select d.name as 表名,COUNT (*)as 记录 from syscolumns a inner join sysobjects d on a.id = d.id and d.xtype = 'U'group by d.name
这是sqlserver 实现的,不知道符不符合。不过刚刚验证了一下,不是很对,估计是主键的原因,修改好了再看看
以上语句只能测试出部分,这个存储过程可以实现全部,sqlserver直接执行即可:
create table #temp(Recordcount int ,tableName varchar(30))
declare @tablename varchar(30)
declare @sql varchar(100)
declare @str varchar(30)
declare tablecursor cursor for
select name from sysobjects where xtype='u'
open tablecursor
fetch next from tablecursor into @tablename
while @@fetch_status=0
begin
set @str=@tablename
set @sql='insert into #temp(recordcount,tablename) select count(*),'+''''+@tablename+''''+' from '+@tablename
exec(@sql)
fetch next from tablecursor into @tablename
end
close tablecursor
deallocate tablecursor
select * from #temp drop table #temp
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)