Sample SQL 2008迴圈所有表和所有列

2021-04-21 06:07:44 字數 2083 閱讀 2443

其實很簡單,相對於sql 2000改了sys

.all_objects和sys

.columns

declare

@tblname

varchar

(200

)

declare

@object_id  

int

declare

tbl_cursor

cursor

forselect

name

,object_id

from

sys.

all_objects

where

type

='u'

open

tbl_cursor

fetch

next

from

tbl_cursor

into

@tblname

,@object_id

while

@@fetch_status=0

begin

declare

@str

varchar

(8000

)

declare

@selectstr

varchar

(2000

)

set@selectstr=''

set@str

='insert into '

+@tblname

+'('

--print @tblname,@object_id

declare

@colname

varchar

(200

)

declare

col_cursor

cursor

forselect

name

from

sys.

columns

where

object_id

=@object_id

order

bycolumn_id

open

col_cursor

fetch

next

from

col_cursor

into

@colname

while

@@fetch_status=0

begin

set@str

=@str

+@colname

+','

--print @str

set@selectstr

=@selectstr

+@colname

+','

--print @colname

fetch

next

from

col_cursor

into

@colname

endclose

col_cursor

deallocate

col_cursor

set@str

=substring

(@str,0

,len

(@str

))

set@selectstr

=substring

(@selectstr,0

,len

(@selectstr

))

set@str

=@str

+') select '

+@selectstr

+' from '

+@tblname

print

@str

fetch

next

from

tbl_cursor

into

@tblname

,@object_id 

end

close

tbl_cursor

deallocate

tbl_cursor

sqlserver 2008 刪除所有表

declare c cursor for 定義游標 select name from sysobjects where xtype u 查詢所有表 declare t varchar 20 open c fetch next from c into t while fetch status 0 be...

SQLserver查詢所有表和表下面所有列

select case when a.colorder 1 then d.name else null end 表名,a.colorder 字段序號,a.name 欄位名,case when columnproperty a.id,a.name,isidentity 1 then else end ...

搜尋sql server中的所有表和所有列

我們平時做調查的時候,經常會疑惑某個資料到底是從哪來的,往往使用事件檢視器之類的捕捉sql來找,其實我們也可以用下邊這條sql試試。不過在資料量大的時候確實是比較慢。drop table declare t varchar 255 c varchar 255 create table name va...