Sql Server 在数据库中所有表所有栏位 找出匹配某个值的 脚本


每次只需修改红色字体部分即可.

可以方便在找出某值被引用的脚本


CREATE  PROCEDURE [dbo].[usp_Tool_GetDataOfKey] AS


declare @strTableName nvarchar(100)
declare @strTableID nvarchar(50)
declare @strColumnName nvarchar(100)
declare @strSql nvarchar(500)


create table #KeyTable(Table_Name nvarchar(100),Column_Name nvarchar(50))


declare Table_Cursor cursor
for
select name,id from sysobjects where xtype='U'
open Table_Cursor
fetch next from Table_Cursor into @strTableName,@strTableID
while(@@fetch_status=0)
	begin
		declare Column_Cursor cursor
		for
		select name from syscolumns where id=@strTableID and (((xtype=239 or xtype=231) and length>30) or ((xtype=175 or xtype=167) and length>15))
		open Column_Cursor
		fetch next from Column_Cursor into @strColumnName
		while(@@fetch_status=0)
			begin
				select @strSql='if exists(select * from '+@strTableName+' where '+@strColumnName+' like ''%C_S_D_N%'') '
				select @strSql=@strSql+'insert into #KeyTable(Table_Name,Column_Name) values('''+@strTableName+''','''+@strColumnName+''')'					
				print @strSql
				exec(@strSql)
				fetch next from Column_Cursor into @strColumnName
			end 
		close Column_Cursor
		deallocate Column_Cursor


		fetch next from Table_Cursor into @strTableName,@strTableID
	end
close Table_Cursor
deallocate Table_Cursor


select * from #KeyTable


你可能感兴趣的:(Sql Server 在数据库中所有表所有栏位 找出匹配某个值的 脚本)