SELECT ( CASE WHEN a.colorder = 1 THEN d.name ELSE '' END ) AS 表名 ,--如果表名相同就返回空 a.colorder AS 字段序号 , a.name AS 字段名 , ( CASE WHEN COLUMNPROPERTY(a.id, a.name, 'IsIdentity') = 1 THEN '√' ELSE '' END ) AS 标识 , ( CASE WHEN ( SELECT COUNT(*) FROM sysobjects--查询主键 WHERE ( name IN ( SELECT name FROM sysindexes WHERE ( id = a.id ) AND ( indid IN ( SELECT indid FROM sysindexkeys WHERE ( id = a.id ) AND ( colid IN ( SELECT colid FROM syscolumns WHERE ( id = a.id ) AND ( name = a.name ) ) ) ) ) ) ) AND ( xtype = 'PK' ) ) > 0 THEN '√' ELSE '' END ) AS 主键 ,--查询主键END b.name AS 类型 , a.length AS 占用字节数 , COLUMNPROPERTY(a.id, a.name, 'PRECISION') AS 长度 , ISNULL(COLUMNPROPERTY(a.id, a.name, 'Scale'), 0) AS 小数位数 , ( CASE WHEN a.isnullable = 1 THEN '√' ELSE '' END ) AS 允许空 , ISNULL(e.text, '') AS 默认值 , ISNULL(g.[value], '') AS 字段说明 FROM syscolumns a LEFT JOIN systypes b ON a.xtype = b.xusertype INNER JOIN sysobjects d ON a.id = d.id AND d.xtype = 'U' AND d.name <> 'dtproperties' LEFT JOIN syscomments e ON a.cdefault = e.id LEFT JOIN sys.extended_properties g ON a.id = g.major_id AND a.colid = g.minor_id where d.name in('T_GC_SuperviseCo','T_GC_Supervisor','T_CC_SupervisorAllocate') ORDER BY a.id , a.colorder; ---表说明 select ROW_NUMBER() OVER (ORDER BY a.object_id) AS No, a.name AS 表名, isnull(g.[value],'-') AS 说明 from sys.tables a left join sys.extended_properties g on (a.object_id = g.major_id AND g.minor_id = 0); --增加表说明 EXECUTE sp_addextendedproperty N'MS_Description','表说明',N'user',N'dbo',N'table',N'表名',NULL,NULL --修改: EXECUTE sp_updateextendedproperty N'MS_Description','表说明',N'user',N'dbo',N'table',N'表名',NULL,NULL;
--创建远程链接服务器 execute sys.sp_addlinkedserver @server='DB2', --被访问的服务器别名 @srvproduct='', @provider='SQLOLEDB', @datasrc='121.36.223.1,4433ecs-6366' --被访问的服务器地址(IP地址,端口号服务器名称) 服务器名称也可以不用( @datasrc='121.36.223.1,4433') --创建本地用户与远程服务器中用户之间的映射 execute sys.sp_addlinkedsrvlogin @rmtsrvname='DB2', --被访问的服务器别名 @useself='false', --是否通过模拟本地登录名或显式提交登录名和密码来连接到远程服务器 @locallogin=null, --本地登录 @rmtuser='sa', --用户名 @rmtpassword='888888.' --密码 go --建立完成之后就可以正常使用了 select * from DB2.RealName.dbo.RnsProjects; --连接服务器别名.数据库名称.dbo.表名称(直接写,可能不会有自动提醒) go --删除运行本地与远程之间的用户映射 execute sys.sp_droplinkedsrvlogin @rmtsrvname='DB2',@locallogin=null --删除链接服务器 execute sys.sp_dropserver @server='DB2',@droplogins='droplogins' go
详细参考:https://www.cnblogs.com/lusunqing/p/3660190.html
select * from ( select name from 数据库A.dbo.syscolumns where id=( select id from 数据库A.dbo.sysobjects where name='表名A') ) T1 FULL OUTER JOIN( select name from 数据库B.dbo.syscolumns where id=( select id from 数据库B.dbo.sysobjects where name='表B' ) ) T2 on T1.name=T2.name 只显示字段字段名有差异的字段,增加一个条件即可where T1.name is null or T2.name is null
详细参考:https://www.cnblogs.com/dz-boss/p/7826477.html