sql语句中,通过知道列中内容 是否可以查找到表名解决方法
sql语句中,通过知道列中内容 是否可以查找到表名
例如 知道两个表
表1ctalbe 中如下列
cno cname
表2stalbe 中如下列
sno sname
现知道123 必为 cno或者sno中的值
如何用sql语句 找到 其对应的表名
我通过select * from sysobjects s where exists(select from syscolumns where cno=s.cno)
据说 只能 知道 列名查找到表名
是否有我需要的 麻烦了·
------解决方案--------------------
例如 知道两个表
表1ctalbe 中如下列
cno cname
表2stalbe 中如下列
sno sname
现知道123 必为 cno或者sno中的值
如何用sql语句 找到 其对应的表名
我通过select * from sysobjects s where exists(select from syscolumns where cno=s.cno)
据说 只能 知道 列名查找到表名
是否有我需要的 麻烦了·
------解决方案--------------------
- SQL code
use pubs go declare @name nvarchar(100) declare @str nvarchar(100) set @str = N'Carson' -- 这里输入你的变量值 declare cur cursor for select name from sysobjects where type = 'U' open cur fetch next from cur into @name WHILE @@FETCH_STATUS = 0 begin declare @sql nvarchar(500),@s varchar(500) set @s ='' set @sql='select @s=isnull(@s+''+'','''')+'''''',''''''+''+cast(''+name+'' as varchar)'' from syscolumns where id=object_id('''+@name+''') and xtype in(175,239,99,231,35,167) ' exec sp_executesql @sql,N'@s varchar(500) out',@s out if len(@s) > 0 exec ('if exists(select 1 from (select '+ @s+' as col from ['+@name+']) b where charindex(''' + @str + ''',col)>0) print '''+@name+'''') fetch next from cur into @name end close cur DEALLOCATE cur /* stores authors */
------解决方案--------------------
select name from sysobjects
where id in(select id from syscolumns where name=字段名)