SQL列存在于表中,但不存在于INFORMATION_SCHEMA中

时间:2012-06-19 08:32:54

标签: sql-server

我有一个在sql中创建的表。但在我的应用程序中,我使用INFORMATION_SCHEMA执行某些任务,我很惊讶它与我的表定义不同步,即

代码

SELECT  SatComRatingTableID         -- This is working
FROM    dbo.tblSatComRatingTable


SELECT  f.field ,
        c.COLUMN_NAME
FROM    dbo.wiztbl_Fields f
        LEFT JOIN INFORMATION_SCHEMA.COLUMNS c ON c.COLUMN_NAME = f.Field
                                                  AND c.TABLE_NAME = 'tblSatComBillingPackage'
WHERE   f.DataObjectID = 2717
        AND COLUMN_NAME IS NULL

第二个查询返回:

SatComRatingTableID NULL

发生了什么以及如何解决? 提前致谢

修改

注意:tblSatComBillingPackage包含一个FK字段SatComRatingTableID。我正在使用该字段从INFORMATION_SCHEMA获取信息......

1 个答案:

答案 0 :(得分:2)

您正在查询错误的表格,似乎正在过滤结果以阻止成功:

SELECT  SatComRatingTableID
FROM    dbo.tblSatComRatingTable --<--This


SELECT  f.field ,
        c.COLUMN_NAME
FROM    dbo.wiztbl_Fields f
        LEFT JOIN INFORMATION_SCHEMA.COLUMNS c ON c.COLUMN_NAME = f.Field
             AND c.TABLE_NAME = 'tblSatComBillingPackage'  --<-- Does't match this
WHERE   f.DataObjectID = 2717
        AND COLUMN_NAME IS NULL --<-- And this seems wrong also