【问题标题】:Find SQL Server Tables that have two specified column names查找具有两个指定列名的 SQL Server 表
【发布时间】:2012-03-26 17:16:27
【问题描述】:

我想在 sql server 中搜索具有两个特定列名 ex(columnA 和 columnB)的表。我有一个搜索一个列名的查询

SELECT name FROM sysobjects WHERE id IN 
( SELECT id FROM syscolumns WHERE name = 'columnA' )

我不知道如何修改它来搜索两列。

【问题讨论】:

  • 你选择了一个无效的答案。
  • 你是对的......我已经改变了选择

标签: sql-server tsql


【解决方案1】:
SELECT name FROM sysobjects WHERE id IN 
( SELECT id FROM syscolumns WHERE name = 'columnA' )
and id in 
( SELECT id FROM syscolumns WHERE name = 'columnB' )

应该可以解决问题。

【讨论】:

    【解决方案2】:

    这是正确的做法:

    select so.name 
    from sysobjects so 
    where so.type = 'U' -- it's a user's table
    and exists (select * from syscolumns sc where sc.id = so.id and sc.name='columnA')
    and exists (select * from syscolumns sc where sc.id = so.id and sc.name='columnB')
    

    检查它是否是用户表很重要。相反,您可以找到视图、表值函数等。

    【讨论】:

      【解决方案3】:

      尝试类似:

      select syscolumns.id, sysobjects.name from syscolumns 
        join sysobjects so on sysobjects.id = syscolumns.id
       where exists (select 1 from syscolumns sc where sc.id = syscolumns.id and name = 'columnA') 
         and exists (select 1 from syscolumns sc2 where sc2.id = syscolumns.id and name = 'columnB')
      

      【讨论】:

      • 我认为如果您为其分配了不同的别名,您将无法按原始名称引用该表。 (我说的是sysobjects。)
      猜你喜欢
      • 2011-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-19
      • 1970-01-01
      • 1970-01-01
      • 2021-04-11
      • 1970-01-01
      相关资源
      最近更新 更多