sql语句查两个表的列是否⼀致
⼀、⾸先要了解Sqlserver 中系统表对象及表结构查询的函数:sysobjects、syscolumns以及函数object_id
1. sysobjects ——  系统对象表。保存当前数据库的对象,如约束、默认值、⽇志、规则、存储过程等
注:重要字段需要解释的是 xtype,他的数据字段内容分别表⽰为:
C = CHECK 约束
D = 默认值或 DEFAULT 约束
F = FOREIGN KEY 约束
FN = 标量函数
IF = 内嵌表函数
K = PRIMARY KEY 或 UNIQUE 约束
L = ⽇志
P = 存储过程
R = 规则
RF = 复制筛选存储过程
S = 系统表
TF = 表函数
TR = 触发器
U = ⽤户表
V = 视图
X = 扩展存储过程
2. sysolumns —— 当前数据库的所有表⾥⾯创建的字段都保留在⾥⾯
注:SQL中的sysobjects 的id与syscolumns 的id 存在主键关系,即 syscolumns 中的id字段是 sysobject
s 表的主键对应值(查询时: select name from syscolumns where id=(select id from sysobjects where name='表名')
3. object_id('表名') —— 函数表⽰直接取表对象的ID值。此⽅法返回数据库对象标识号
注:查询时:  select name from syscolumns where  id =object_id('TB')  等同于上述查询
⼆、⽐较同⼀数据库中两个表的对应字段的差异(当两个表结构⼀样时,查询表对应的字段是否⼀致)
select * from (
select name
from syscolumns
where id=(
select id from sysobjects
where name='表名1')
) T1
FULL OUTER JOIN(
select name from syscolumns
where id=(
select id from sysobjects
where name='表名2')
) T2 on T1.name=T2.name
where T1.name is null or T2.name is null
三、⽐较在不同数据库中两个表的对应字段的差异(当两个表结构⼀样时,查询表对应的字段是否⼀致)select * from (
select name
from gxjmxy.dbo.syscolumns
sql语句怎么查询两张表的数据where id=(
select id from gxjmxy.dbo.sysobjects
where name='TB1')
) T1 FULL OUTER JOIN(
select name from Test.dbo.syscolumns
where id=(
select id from Test.dbo.sysobjects
where name='TB'
)
) T2 on T1.name=T2.name
where T1.name is null or T2.name is null

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。