sql 查詢表定義

2022-01-15 11:32:59 字數 3240 閱讀 7895

1

2alter

proc

[dbo

].[test_querytableinfo]3

(

4@tablename

varchar(200

)

5) 6as

7begin

8select

9 表名=

case

when a.colorder =

1then d.name else

''end

,

10 表說明=

case

when a.colorder =

1then

isnull(f.value, '') else

''end

,

11 字段序號=

a.colorder ,

12 欄位名=

a.name ,

13 標識=

case

when

columnproperty(a.id, a.name, '

isidentity

') =

1then'√

'else

''end

,

14 主鍵=

case

when

exists

(

15select116

from

dbo.sysindexes si

17inner

join dbo.sysindexkeys sik on si.id = sik.id and si.indid =

sik.indid

18inner

join dbo.syscolumns sc on sc.id = sik.id and sc.colid =

sik.colid

19inner

join dbo.sysobjects so on so.name = so.name and so.xtype ='pk

'20where sc.id = a.id and sc.colid = a.colid) then'√

'21else

''22

end,

23 外來鍵=

case

when tony.fkey is

notnull

and tony.fkey=a.colid then'√

'else

''end

,

24 外來鍵表=

case

when tony.fkey is

notnull

and tony.fkey=

a.colid

25then

object_name(tony.fkeyid) else

''26

end,

27 外來鍵字段=

case

when tony.fkey is

notnull

and tony.fkey=

a.colid

28then (select name from

syscolumns

29where colid=tony.fkey and id=

tony.fkeyid)

30else

''31

end,

32 型別=

b.name ,

33 長度=

a.length ,

34 精度=

columnproperty(a.id, a.name, '

precision

') ,

35 小數字數=

isnull(columnproperty(a.id, a.name, '

scale

'), 0

) ,

36 允許空=

case

when a.isnullable =

1then'√

'else

''end

,

37 預設值=

isnull(e.text, ''

),

38 字段說明=

isnull(g.[

value

], ''

),

39 建立時間=

d.crdate,

40 更改時間=

case

when a.colorder =

1then d.refdate else

null

end41

from

dbo.syscolumns a

42left

join dbo.systypes b on a.xtype =

b.xusertype

43inner

join dbo.sysobjects d on a.id = d.id and d.xtype ='u

'and d.status >=044

left

join dbo.syscomments e on a.cdefault =

e.id

45left

join sys.extended_properties g on a.id = g.major_id and a.colid =

g.minor_id

46left

join sys.extended_properties f on d.id = f.major_id and f.minor_id =047

left

join sysobjects htl on htl.parent_obj=d.id and d.xtype='f

'48left

join sysforeignkeys tony on htl.id=

tony.constid

49where d.name=

@tablename

--這裡輸入包含表名稱的條件

50order

byd.id, a.colorder

51end

5253

go

SQL獲取表定義

查詢某個庫里所有表的定義,包括欄位名,值型別,長度,是否為空,是否為主鍵等 select sysobjects.name astb name,syscolumns.name ascol name systypes.name ascol type,syscolumns.length ascol len...

SQL之表定義

sql 結構化查詢語言 1 用create table命令定義sql關係 create table r a1 d1,a2 d2,an dn,完整性約束1 完整性約束k 其中,r是關係名,每個ai是關係r模式中的乙個屬性名,di是屬性ai的域 2 完整性約束 not null,乙個屬性上的not nu...

sql 查詢多個表

直接黏貼sql cook book上的資料 1 select ename as ename and dname,deptno 2 from emp 3 where deptno 10 4 union all 5 select null 6 from t1 7 union all 8 select d...