能把表的 create 結構 生成出來

2021-05-10 12:36:21 字數 4262 閱讀 2919

sql code

alter

procedure

[dbo].

[sp_helptable](

@tablename

sysname)

asset

nocount

ondeclare

@objectid

intdeclare

@tablescript

table

(iden

intidentity(1

, 1), scriptline

nvarchar

(4000

))

set@objectid

=object_id

(@tablename)

if@objectid

isnull

orobjectproperty

(@objectid, '

istable')

=0begin

raiserror('

指定的物件不是表物件',

16, 1)

return

end--

獲取表的建立指令碼

--插入表頭

insert

into

@tablescript

(scriptline)

selectn'

create table ['+

user_name

(objectproperty

(@objectid

, n'

ownerid

')) +n

'].['+

object_name

(@objectid) +

n']('

--插入字段

insert

into

@tablescript

(scriptline)

selectn'

['+a.name +n

'] ['+

b.name +n

']'+

case

when

c.object_id

isnot

null

thenn'

identity('+

convert

(nvarchar

, c.seed_value) +n

', '+

convert

(nvarchar

, c.increment_value) +n

')'else

''end

+case

when

b.xusertype in(

167,

175,

231,

239)

thenn'

('+convert

(nvarchar

, a.prec) +n

')'when

b.xusertype in(

106,

108)

thenn'

('+convert

(nvarchar

, a.xprec) +n

', '+

convert

(nvarchar

, a.xscale) +n

')'else

''end

+case

a.isnullable

when

1thenn''

elsen'

not'

end+n'

null'+

case

when

d.name

isnot

null

thenn'

default '+

d.definition

elsen''

end+n'

,'from

sys.syscolumns a

left

join

sys.systypes b

ona.xusertype

=b.xusertype

left

join

sys.identity_columns c

onc.

object_id

=a.id

andc.column_id

=a.colid

left

join

sys.default_constraints d

ond.parent_object_id

=a.id

andd.parent_column_id

=a.colid

wherea.[

id]=@objectid

order

bya.colorder

--插入主鍵和索引

declare

@indexid

int,

@indexscript

nvarchar

(4000)

declare

indexcursor

cursor

forselect

b.index_id, n

'constraint ['+

a.name +n

'] '+

case

a.type

when'pk

'thenn'

primary key

'when'uq

'thenn'

unique

'end

+case

b.type

when

1thenn'

clustered

'when

2thenn'

nonclustered

'end+n

'('from

sys.key_constraints a

left

join

sys.indexes b

onb.

object_id

=a.parent_object_id

andb.index_id

=a.unique_index_id

where

a.parent_object_id

=@objectid

open

indexcursor

fetch

next

from

indexcursor

into

@indexid

, @indexscript

while

@@fetch_status=0

begin

select

@indexscript

=@indexscript+n

'['+

index_col

(object_name

(@objectid

), 2, 1

) +n'

],'from

sys.index_columns

where

object_id

=@objectid

andindex_id =2

set@indexscript

=left

(@indexscript

, len

(@indexscript) -

1) +n

'),'insert

into

@tablescript

(scriptline)

values

(@indexscript

)

fetch

next

from

indexcursor

into

@indexid

, @indexscript

endclose

indexcursor

deallocate

indexcursor

insert

into

@tablescript

(scriptline)

values('

)')

select

*from

@tablescript

生成表結構

2005 08 02 t sql store procedure 修訂為 完全不受字段數量或字段值影響 而生成正確完整的 insert into sql alter procedure z sp geninsertsql tablename varchar 256 whereclause varch...

生成sql表結構

dataconstruct.php created by phpstorm.user administrator date 2017 7 21 time 14 26 列子 pdo new dataconstruct tables pdo getalltablenames pdo printlog p...

SQLSERVER 生成表結構的SQL

sqlserver 生成表結構的sql select 表名 case when a.colorder 1then d.name else end 表說明 case when a.colorder 1then isnull f.value,else end 字段序號 a.colorder,欄位名 a....