SQL数据库的操作
这是根据表中的字段数据找到该表,这个输出的是PRINT结果,我想把这个表明输出来,能够取到这个值,想Select一样输出个表格形式,print取不出来,请大家帮帮忙!非常感谢!!!!
DECLARE @what varchar(800)
SET @what='111' --要搜索的字符串
DECLARE @sql varchar(8000)
DECLARE TableCursor CURSOR LOCAL FOR
SELECT sql='IF EXISTS ( SELECT 1 FROM ['+o.name+'] WHERE ['+c.name+'] LIKE ''%'+@what+'%'' ) PRINT ''所在的表及字段:['+o.name+'].['+c.name+']'''
FROM syscolumns c JOIN sysobjects o ON c.id=o.id
-- 175=char 56=int 可以查 select * from sys.types
WHERE o.xtype='U' AND c.status>=0 AND c.xusertype IN (175, 239, 231, 167 )
OPEN TableCursor
FETCH NEXT FROM TableCursor INTO @sql
WHILE @@FETCH_STATUS=0
BEGIN
EXEC( @sql )
FETCH NEXT FROM TableCursor INTO @sql
END
CLOSE TableCursor
-- 删除游标引用
DEALLOCATE TableCursor
[解决办法]
3.获取当前数据库中的所有用户表 select * from sysobjects where xtype='U' and category=0 4.获取某一个表的所有字段 select name from syscolumns where id=object_id('表名')--使用这两个语句嵌套查询
[解决办法]
--本机测试select name from sysobjects where id in(select id from syscolumns where id in(select id from sysobjects where xtype='U' and category=0)and name='费用名称')/*name收费表费用表*/
[解决办法]
select o.name,c.name FROM syscolumns c JOIN sysobjects o ON c.id=o.id-- 175=char 56=int 可以查 select * from sys.types WHERE o.xtype='U' AND c.status>=0 AND c.xusertype IN (175, 239, 231, 167 )and c.name like '%column_name%'
[解决办法]