mssql查詢某個值存在某個表裡的哪個欄位的值裡面

來源:互聯網
上載者:User

標籤:fetch   函數   dealloc   arc   and   語句   object_id   char   insert   

第一步:建立 查詢某個值存在某個表裡的哪個欄位的值裡面 的預存程序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         for             select '['+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)     end    else    begin         declare curTable cursor fast_forward         for         select '['+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)     end    open curtable     fetch next from curtable into @tbl,@col     while @@FETCH_STATUS=0     begin        set @sql='if exists (select * from '[email protected]+' where '        if @type=1         begin            set @sql += @col + ' like ''%'[email protected] +'%'')'        end        else         begin            set @sql [email protected] + ' in ('[email protected]+'))'        end          set @sql += ' INSERT #TBL(tbl,col) VALUES('''[email protected]+''','''[email protected]+''')'        --print @sql         exec (@sql)         fetch next from curtable into @tbl,@col     end    close curtable      deallocate curtable     select * from #tbl可能和它一起用的SQL語句:查看與某一個表相關的視圖、預存程序、函數select a.* from sysobjects a, syscomments b where a.id = b.id and b.text like '%表名%'

mssql查詢某個值存在某個表裡的哪個欄位的值裡面

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.