在SQL2008查詢某資料庫中的列是否存在某個值

2021-07-04 11:13:21 字數 2555 閱讀 4339

--

sql2008查詢某資料庫中的列是否存在某個值

create

proc

spfind_column_in_db

(

@type

int,--

型別:1為文字型別、2為數值型別

@str

nvarchar(100)--

需要搜尋的名字)as

--建立臨時表存放結果

create

table #tbl(pk int

identity

primary

key,tbl sysname,col sysname)

declare

@tbl

nvarchar(300),@col sysname,@sql

nvarchar(1000)

if@type

=1

begin

declare curtable cursor

fast_forward

forselect'[

'+schema_name(schema_id)+

'].[

'+o.name+']

' tablename,'['

+c.name+']

' columnname from sys.columns c inner

join sys.objects o on c.object_id

=o.object_id

where o.type_desc=

'user_table

'and user_type_id in (167,175,231,239,35,99)

endelse

begin

declare curtable cursor

fast_forward

forselect'[

'+schema_name(schema_id)+

'].[

'+o.name+']

' tablename,'['

+c.name+']

' columnname from sys.columns c inner

join sys.objects o on c.object_id

=o.object_id

where o.type_desc=

'user_table

'and user_type_id in (56,48,52,59,60,62,106,108,122)

endopen

curtable

fetch

next

from curtable into

@tbl,@col

while

@@fetch_status=

0

begin

set@sql='

if exists (select * from '+

@tbl+'

where 'if

@type

=1

begin

set@sql

+=@col+'

like ''%

'+@str+'

%'')'

endelse

begin

set@sql

+=@col+'

in ('+

@str+'

))'endset

@sql+='

insert #tbl(tbl,col) values(

'''+

@tbl

+'''

,'''

+@col

+''')'

--print @sql

exec (@sql

)

fetch

next

from curtable into

@tbl,@col

endclose

curtable

deallocate

curtable

select

*from

#tbl

--使用例子,查詢庫中存在aaa這個值的列:

exec spfind_column_in_db1,'

aaa'

**:

SQL2008資料庫級角色

db owner db owner固定資料庫角色的成員可以執行資料庫的所有配置和維護活動,還可以刪除資料庫。db securityadmin db securityadmin固定資料庫角色的成員可以修改角色成員身份和管理許可權。向此角色中新增主體可能會導致意外的許可權公升級。db accessadm...

SQL2008資料庫映象操作

1.檢視終端點 select from sys.endpoints 2.刪除某終端點 終端點不帶引號 drop endpoint 3.刪除證書 在master security certificates drop master key.4.刪除使用者 在master user 5.然後可以刪除登入名...

SQL2008附加資料庫報錯

sql server 2008如何匯入mdf,ldf檔案網上找了很多解決sql server匯入其他電腦拷過來的mdf檔案,多數是不全,遇到的解決方法不一樣等問題,下邊是找到的解決問題的最全面方法!將mdf,ldf檔案匯入到sql server 2008資料庫當中,下面提供兩種辦法 第一種 1 選擇...