例如 知道两个表
表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 godeclare @name nvarchar(100)declare @str nvarchar(100)set @str = N'Carson' -- 这里输入你的变量值declare cur cursor for select name from sysobjects where type = 'U'open curfetch next from cur into @nameWHILE @@FETCH_STATUS = 0begin 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([email protected]+''') and xtype in(175,239,99,231,35,167) ' exec sp_executesql @sql,[email protected] varchar(500) out',@s out if len(@s) > 0 exec ('if exists(select 1 from (select '+ @s+' as col from [[email protected]+']) b where charindex(''' + @str + ''',col)>0) print [email protected]+'''') fetch next from cur into @nameendclose curDEALLOCATE cur/*storesauthors*/
------解决方案--------------------
select name from sysobjects
where id in(select id from syscolumns where name=字段名)