【问题标题】:Querying other shema syscolumns table查询其他 shema syscolumns 表
【发布时间】:2019-04-18 10:48:40
【问题描述】:

我正在执行 SQL 查询来检查天气模式 abc_hist 是否有表 @table_name。但是即使表存在,以下查询也无法返回任何结果,即每次都会使 if 条件为假:

use abc
go
----procedure---
IF EXISTS(select  1
                  from    abc_hist..syscolumns
                  where   status & 128 = 128
                          and     object_name(id) = @table_name )
----procedure---

那么,有没有其他方法可以有效地检查其他模式中的表是否存在或在我当前的 sql 中进行更正?

【问题讨论】:

    标签: sql sybase sap-iq


    【解决方案1】:

    运行这个:

    select 
      CASE WHEN status & 8> 0 THEN 'allows null' ELSE 'no nulls' end, 
      CASE WHEN status & 16 > 0 THEN 'check constraint exists' ELSE 'no checks' end, 
      CASE WHEN status & 128 > 0 THEN 'is identity' ELSE 'not identity' end,
      *
                  from    abc_hist..syscolumns
                  where   object_name(id) = @table_name
    

    它将在每一行中显示“非身份”,这意味着您作为参数传入的 @table_name 没有身份列,并且因为您将 where 子句的条件设为结果必须是身份列为了被返回,没有结果,所以 EXISTS 总是 false

    如果您想使用此查询来检查表的存在,请删除关于状态的 WHERE 子句:

    IF EXISTS(select  1
                  from    abc_hist..syscolumns
                  where   object_name(id) = @table_name )
    

    【讨论】:

    • 谢谢。您对 status 的使用使其更易于理解。
    猜你喜欢
    • 1970-01-01
    • 2023-01-12
    • 2018-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多