Loading... 有时候我们知道一个值,但是不知道这个值在库里边的那个表中,是哪个字段,可以用以下的语句来进行查询,比如我们查询“张三”在那个表的那个字段: ```sql DECLARE @cloumns VARCHAR(40); DECLARE @tablename VARCHAR(40); DECLARE @str VARCHAR(40); DECLARE @counts INT; DECLARE @sql NVARCHAR(2000); DECLARE MyCursor CURSOR FOR SELECT a.name AS Columns, b.name AS TableName FROM syscolumns a, sysobjects b, systypes c WHERE a.id = b.id AND b.type = 'U' AND a.xtype = c.xtype AND c.name LIKE '%char%'; SET @str = '张三'; OPEN MyCursor; FETCH NEXT FROM MyCursor INTO @cloumns, @tablename; WHILE (@@Fetch_Status = 0) BEGIN SET @sql = N'select @tmp_counts=count(*) from ' + @tablename + N' where ' + @cloumns + N' = ''' + @str + N''''; EXECUTE sp_executesql @sql, N'@tmp_counts int out', @counts OUT; IF @counts > 0 BEGIN PRINT '表名为:' + @tablename + ',字段名为' + @cloumns; END; FETCH NEXT FROM MyCursor INTO @cloumns, @tablename; END; CLOSE MyCursor; DEALLOCATE MyCursor; ``` 结果:  最后修改:2023 年 11 月 13 日 © 允许规范转载 打赏 赞赏作者 支付宝微信 赞 -