例如 知道两个表
表1ctalbe 中如下列
cno cname
表2stalbe 中如下列
sno sname
现知道123 必为 cno或者sno中的值
如何用sql语句 找到 其对应的表名我通过select * from sysobjects s where exists(select from syscolumns where cno=s.cno)
据说 只能 知道 列名查找到表名   是否有我需要的  麻烦了· 

解决方案 »

  1.   

    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*/
      

  2.   


    declare @value varchar(50)
    set @value=''--这里输入你要的查询变量的名字
    select cno from table1 where cno=@value
    if @@ROWCOUNT <>0
    print 'table1'
    else
    print 'table2'
      

  3.   


    谢谢 不过我表多的话 就比较麻烦·了· 
      我是Delphi 里面的 query 控件的 sql 属性·
      

  4.   

    select fn from
    (select sno qno,'sno' fn from stalbe
    union all 
    select cno qno,'cno' fn from ctalbe
    )A
    where qno='123'
      

  5.   

    select fn from
    (select sno qno,'stalbe' fn from stalbe
    union all 
    select cno qno,'ctalbe' fn from ctalbe
    )A
    where qno='123'
      

  6.   

    select name from sysobjects
    where id in(select id from syscolumns where name=字段名)