假如字段值=123456,根據其查詢表名和欄位名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
假如字段值=123456,根據其查詢表名和欄位名方法一:
--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''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;
方法二:
--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 ='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;
根據表字段值left join 不同的表
首先說下思路吧,搗鼓了好半天,網上找到解決辦法,大概兩種方法 1.在left join 的後面將兩張表union all連線為一張表,並加上 區分表關鍵字 然後根據 區分表關鍵字 篩選我們需要的資料 2.使用兩個left join,每個left join 跟上一張表,並加上 區分表關鍵字 然後在 o...
mysql查詢表字段預設值
mysql查詢表字段預設值。假設表user有個字段 login times int 11 unsigned not null default 0 comment 登入次數 需要獲取該字段的預設值,實現方法 desc user 查詢結果 field type null default id varch...
根據系統表查詢使用者表字段資訊
在sql server中查詢字段資訊的語句 select a.name as tablename,b.name as columnname,case when h.id is not null then pk else no end as primarykey,type name b.xuserty...