使用SQL語句匯出表結構

2021-04-13 08:57:29 字數 2948 閱讀 3605

--老大寫的sql

select tablename=o.name,owner=user_name(o.uid),tabledescription=isnull(ptb.value,n''),

fieldid=c.colid,fieldname=c.name,

fieldtype=quotename(t.name)

+case

when t.name in (n'decimal',n'numeric')

then n'('+cast(c.prec as varchar)+n','+cast(c.scale as varchar)+n')'

when t.name=n'float'

or t.name like n'%char'

or t.name like n'%binary'

then n'('+cast(c.prec as varchar)+n')'

else n'' end,

fieldconstraint=case when c.isnullable=1 then n'' else n' not' end+n' null',

fielddescription=isnull(pfd.value,''),

defilelength=c.length,

fielddefault=isnull(df.text,n''),

isidentity=columnproperty(o.id,c.name,n'isidentity'),

iscomputed=columnproperty(o.id,c.name,n'iscomputed'),

isrowguid=columnproperty(o.id,c.name,n'isrowguidcol'),

isprimarykey=case when opk.xtype is null then '' else 'primary key' end

from sysobjects o

join syscolumns c

on c.id=o.id

and objectproperty(o.id,n'isusertable')=1

join systypes t

on t.xusertype=c.xusertype

left join syscomments df

on df.id=c.cdefault

left join sysproperties ptb

on ptb.id=o.id and ptb.**allid=0

left join sysproperties pfd

on pfd.id=o.id and pfd.**allid=c.colid

left join sysindexkeys idxk

on idxk.id=o.id

and idxk.colid=c.colid

left join sysindexes idx

on idx.indid=idxk.indid

and idx.id=idxk.id

and idx.indid not in(0,255)

left join sysobjects opk

on opk.parent_obj=o.id

and opk.name=idx.name

and objectproperty(opk.id,n'isprimarykey')=1

order by o.name,c.colid

sxycgxj(雲中客)

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,

標識=case when columnproperty( a.id,a.name,'isidentity')=1 then '√'else '' end,

主鍵=case when exists(select 1 from sysobjects where xtype='pk' and name in (select name from sysindexes where indid in(select indid from sysindexkeys where id = a.id and colid=a.colid))) then '√' else '' end,

型別=b.name,

占用位元組數=a.length,

長度=columnproperty(a.id,a.name,'precision'),

小數字數=isnull(columnproperty(a.id,a.name,'scale'),0),

允許空=case when a.isnullable=1 then '√'else '' end,

預設值=isnull(e.text,''),

字段說明=isnull(g.[value],''),

建立時間=d.crdate

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 sysproperties g on a.id=g.id  and a.colid=g.**allid

left join sysproperties f on d.id=f.id and f.**allid=0

--where d.name='此處可新增要查詢的表名'

order by a.id,a.colorder

使用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...

表資料匯出sql語句

go object storedprocedure dbo outputdata script date 08 04 2010 17 50 53 set ansi nulls on go set quoted identifier on go create procedure dbo outputd...

使用SQL語句操作表結構

刪除員工表 drop table db user 刪除角色表 drop table db role 清空 站 recycle bin purge recyclebin 建立角色資訊表 create table db role roleid varchar2 8 not null primary ke...