use[資料庫名]go
declare
@tablename1
nvarchar(100);
declare
@tablename2
nvarchar(100);
declare
@dataname1
nvarchar(100);
declare
@dataname2
nvarchar(100);
set@dataname1='
庫1';
set@dataname2='
庫2';
set@tablename1='
表1';
set@tablename2='
表2';set
@tablename2
=@tablename1;
select
(case
when a.colorder=
1then d.name else
null
end) 表名1,
a.name 欄位名1,
(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
'))>
0then'√
'else
''end
) 主鍵1,b.name 型別1,
columnproperty(a.id,a.name,'
precision
') as
長度1 ,1as
aa into
#temp1
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
left
join sys.extended_properties f on d.id=f.class and f.minor_id=
0where b.name is
notnull
and (select
count(1) from master..sysdatabases sysa where sysa.name=
@dataname1 )>
0and d.name=
@tablename1
--where d.name='要查詢的表' --如果只查詢指定表,加上此條件
order
bya.name,a.id,a.colorder
select
(case
when a.colorder=
1then d.name else
null
end) 表名2,
a.name 欄位名2,
(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
'))>
0then'√
'else
''end
) 主鍵2,b.name 型別2,
columnproperty(a.id,a.name,'
precision
') as
長度2,1as
aa into
#temp2
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
left
join sys.extended_properties f on d.id=f.class and f.minor_id=
0where b.name is
notnull
and (select
count(1) from master..sysdatabases sysa where sysa.name=
@dataname2 )>
0and d.name=
@tablename2
--where d.name='要查詢的表' --如果只查詢指定表,加上此條件
order
bya.name,a.id,a.colorder
select
*from
(
select
count(1) as 表1欄位數量 from
#temp1
)a ,
(
select
count(1) as 表2欄位數量 from
#temp2
)bselect
count(1) from #temp1 a left
join #temp2 b on a.欄位名1=
b.欄位名2
where a.長度1!=
isnull(b.長度2,'') or a.型別1!=
isnull(b.型別2,''
);select
count(1) from #temp1 a right
join #temp2 b on a.欄位名1=
b.欄位名2
where
isnull(a.長度1,'')!=b.長度2 or b.型別2!=
isnull(a.型別1,''
);select a.欄位名1,b.欄位名2,a.型別1,b.型別2,a.長度1,b.長度2 into #temp_1 from #temp1 a left
join #temp2 b on a.欄位名1=
b.欄位名2
select a.欄位名1,b.欄位名2,a.型別1,b.型別2,a.長度1,b.長度2 into #temp_2 from #temp1 a right
join #temp2 b on a.欄位名1=
b.欄位名2
select
*from
#temp_1;
select
*from
#temp_2;
drop
table
#temp1;
drop
table
#temp2;
drop
table
#temp_1;
drop
table
#temp_2;
go
使用SQL語句 匯出SQLServer表結構
select 表名 case when a.colorder 1 then d.name else end,表說明 case when a.colorder 1 then isnull f.value,else end,字段序號 a.colorder,欄位名 a.name,字段說明 isnull g...
詳解sqlserver查詢表索引
select 索引名稱 a.name 表名 c.name 索引欄位名 d.name 索引字段位置 d.colid from sysindexes a join sysindexkeys b on a.id b.id and a.indid b.indid join sysobjects c on b...
sqlserver 批量刪除相同字首名的表
方法1 declare table nvarchar 30 declare tmpcur cursor forselect name from sys.objects where type u and name like n hsupa open tmpcur fetch next from tmp...