Create Procedure sp_Search(@Val Varchar(4000))
As
Begin
Declare @TBName Varchar(200)
Declare @ColName Varchar(200)
Declare @Rst int
Declare @Sql NVarchar(4000)
Declare @TB Table(TbName Varchar(200),ColName Varchar(200))
Declare myCur Cursor For
Select A.Name,B.Name From sys.objects A Inner join sys.columns B on a.object_id=b.object_id
Where A.Type='U' And system_Type_ID in(35,99,167,175,231,239)
Open myCur;
Fetch Next From myCur Into @TBName,@ColName
While @@FETCH_STATUS = 0
Begin
Set @Sql='If Exists(Select * From ['+@TBName+'] Where ['+@ColName+'] Like @Val)'
Set @Sql=@Sql+'Set @Rst=1 '
Set @Sql=@Sql+'Else '
Set @Sql=@Sql+'Set @Rst=0 '
Exec sp_Executesql @sql,N'@Val Varchar(4000),@Rst int output',@Val,@Rst output
If(@Rst=1)
Insert Into
@TB values(@TBName,@ColName)
Fetch Next From myCur Into @TBName,@ColName
End
Close myCur
DeAllocate myCur
Select * From @TB
End
/*
测试:
这样就能搜到你的公司在哪个表了,然后去更新吧,速度跟表名,这段和数据量都有关系
Exec sp_search '公司名称'
Exec sp_search '%公司名称%'
以上搜索包含的字段数据类型为Char,Varchar,Text,NChar,NVarchar,NText
思路:
先查询出所有表所有字符型栏位,然后每个表每个栏位查询值是否等于@Val,如果等则保存表名和字段名,最后返回结果
*/