Oracle根據字段值找到表名和列名

2021-09-07 10:07:50 字數 2944 閱讀 9656

方法1:

--oracle 根據字段值查詢其所在的表、字段  

declare

cursor cur_query is

select table_name, column_name, data_type from user_tab_columns;

a number;

sql_hard varchar2(

2000

);

vv number;

begin

for rec1 in cur_query loop

a:=0

;

if rec1.data_type ='

varchar2

' or rec1.data_type='

char

'then

a := 1

;

end if;

if a>0

then

sql_hard := ''

;

sql_hard := '

select count(*) from

'|| rec1.table_name ||'

where

'

||rec1.column_name|| '

like

''吳芳

''';--字段值

dbms_output.put_line(sql_hard);

execute immediate sql_hard into vv;

if vv > 0 then dbms_output.put_line('

[字段值所在的表.欄位]:[

'||rec1.table_name||'

].['||rec1.column_name||']'

);

end if;

end if;

end loop;

end;

方法2:

declare  

cursor cur_query is

select table_name, column_name, data_type from user_tab_columns;

a number;

sql_hard varchar2(

2000

);

vv number;

begin

for rec1 in cur_query loop

a:=0

; if rec1.data_type ='

number

'then

a := 1

; end if;

if a>0

then

sql_hard := ''

; sql_hard := '

select count(*) from

'|| rec1.table_name ||'

where

' ||rec1.column_name

|| '

=123456

';--字段值

dbms_output.put_line(sql_hard);

execute immediate sql_hard into vv;

if vv > 0

then

dbms_output.put_line(

'[字段值所在的表.欄位]:[

'||rec1.table_name||'

].['||rec1.column_name||']'

);

end if;

end if;

end loop;

end;

sqlserver中如何實現

declare @what varchar(800

) set @what='

123456

' --要搜尋的字串

declare @sql varchar(

8000

)

declare tablecursor cursor local for

select sql='

if exists ( select 1 from [

'+o.name+'

]

where ['

+c.name+

'] like ''%'

+@what+

'%'' ) print ''[字段值所在的表.欄位]:['

+o.name+

'].['

+c.name+

']'''

from syscolumns c join sysobjects o on c.id=o.id

-- 175=char

56=int 可以查 select * from

sys.types

where o.xtype='

u' and c.status>=0 and c.xusertype in (175, 239, 231, 167

)

open tablecursor

fetch next from tablecursor into @sql

while @@fetch_status=0

begin

exec( @sql )

fetch next from tablecursor into @sql

end

close tablecursor

--刪除游標引用

deallocate tablecursor

根據字段內容查詢表名

儲存指定資料庫中的表名 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 nul...

根據表名獲取表字段資訊

實際應用中不免需要調取資料庫表字段的相關資訊,特此將 貼出,以作備用,調取的資訊 欄位名 字段型別 字段長度 是否主鍵 說明 建立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...