如何在一个SQL数据库中搜索到特定值的记录
发布网友
发布时间:2022-04-09 19:34
我来回答
共1个回答
热心网友
时间:2022-04-09 21:04
这样的话,给你一个思路,你先根据这个库,查找里面所有表名,然后再根据表名,查找字段名,然后再查找所有不是数字或者是二进制的字段进行搜索,当然这个需要多次循环,多次调用,肯定是很麻烦的!肯定得使用游标,虽然效率低,但是这个是唯一的一个办法:declare @tabName VARCHAR(40) declare @colName VARCHAR(40)DECLARE @sql nVARCHAR(2000)
set @sql = '';
DECLARE tabCursor CURSOR FOR sELECT name From sysobjects WHERE xtype = 'u' --查找库中所有的表OPEN tabCursorFETCH NEXT FROM tabCursor INTO @tabName
WHILE @@fetch_status = 0
BEGIN
-- SET @sql = 'UPDATE ' + @tabName + ' SET '
DECLARE colCursor CURSOR FOR Select Name FROM SysColumns Where id=Object_Id(@tabName) --查找表中所有的字段
OPEN colCursor
FETCH NEXT FROM colCursor INTO @colName
set @sql = ' select 1 from '+@tabName+ ' where '+ @colName +' like ''%10%'' '
WHILE @@fetch_status = 0
BEGIN
-- SET @sql = @sql + @colName + '=REPLACE(' +@colName+ ',''XX'',''''),'
set @sql += CHAR(10)+CHAR(13)+ ' select 1 from '+@tabName+ ' where '+ @colName +' like ''%10%'' '
FETCH NEXT FROM colCursor INTO @colName
END
-- SET @sql = LEFT(@sql,LEN(@sql)-1)
-- print @sql
EXEC(@sql)
CLOSE colCursor
DEALLOCATE colCursor
FETCH NEXT FROM tabCursor INTO @tabName
ENDCLOSE tabCursorDEALLOCATE tabCursor 此方法经过验证,但是需要你进行简单的修改!