日期:2014-05-17 浏览次数:20503 次
--很简单的,SQL的系统表里面保存了其它用户表的结构字段等
--可以从系统表里面获取某数据表的所有字段。然后循环生成 where 段的语句
DECLARE @TNAME VARCHAR(20),@KEY VARCHAR(10),@I INT,@COUNT INT ,@WHERE VARCHAR(5000) ,@FN VARCHAR(20)
SET @TNAME ='SC_SCGCD_M' --要查询的数据表名
SET @KEY ='1' --要查询的关键字
select ID=IDENTITY(INT,1,1), NAME INTO #TEMP01 from syscolumns where id=OBJECT_ID(@TNAME)
SELECT @COUNT=COUNT(0) FROM #TEMP01
SET @I=1
SET @WHERE =' (1=2) '
WHILE @I <= @COUNT
BEGIN
SELECT @FN =[NAME] FROM #TEMP01 WHERE ID=@I
SET @WHERE = @WHERE +' OR ('+ @FN + ' LIKE ''%'+@KEY+'%'')'
SET @I=@I + 1
END
EXEC('SELECT * FROM '+@TNAME+ ' WHERE '+@WHERE)
DROP TABLE #TEMP01