根據字段內容查詢表名

2021-04-13 03:26:31 字數 1383 閱讀 6507

--儲存指定資料庫中的表名

create table t(id int identity(1,1),t_name varchar(100) null,rc int null)

--儲存指定表的所有列名

create table t_c(id int identity(1,1),t_c varchar(100) null,t_t varchar(20) null)

declare @t varchar(300) --表名

declare @t_c varchar(20)--欄位名稱

declare @t_t varchar(20)--欄位所屬表名

declare @sql varchar(8000)

/*******************************

處理思路:取出資料庫中所用的表,外迴圈每個表

取表的所用列,內迴圈每個列是否有合符條件的資料,

有則儲存該錶名

*******************************/

--具體的實現細節如下:

declare cur_test cursor for select name from sysobjects where type='u'

open cur_test

fetch cur_test into @t

while @@fetch_status=0

begin

insert into t_c(t_c,t_t) select name,@t  from syscolumns where id=object_id(@t)

declare cur_c cursor for select t_c,t_t from t_c

open cur_c

fetch cur_c into @t_c,@t_t

while @@fetch_status=0

begin

set @sql=isnull(@sql,'')+'insert into t(t_name )select  '''+@t_t+''' from '+ @t_t+' as a  where  exists(select * from '+@t_t+' where  a.id ='+@t_t+'.id and '+ @t_c+' like ''%貴在堅持%'')'

print   @sql  

exec(@sql)

set @sql=''

fetch cur_c into @t_c,@t_t

endclose cur_c

deallocate cur_c

truncate table t_c

fetch cur_test into @t

endclose cur_test

deallocate cur_test

Mssql 根據表名查詢所有字段資訊

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

根據表名獲取表字段資訊

實際應用中不免需要調取資料庫表字段的相關資訊,特此將 貼出,以作備用,調取的資訊 欄位名 字段型別 字段長度 是否主鍵 說明 建立sqlparameter public class cp public sqldbtype coltype public int collength public boo...

根據字段值 查詢 欄位名,表名

declare what varchar 800 set what bffc43e1 6646 434e b47d 0a32ac793b1c 要搜尋的字串 declare sql varchar 8000 declare tablecursor cursor local for select sql...