SQL - select 列(如果存在于 OLEQuery 中)

SQL - select column if exists in OLEQuery

在某些客户中,我使用旧数据库,但现在我必须为新客户添加另一列。如果新列存在,我的程序现在将查询它,如果不存在,则使用 NULL。

到目前为止,使用 C# 和 OLEQuery 是可行的:

string sql = @"SELECT i.[ID] AS cID, 
                        pg.[Name], 
                        pg.[ID] AS pgid, 
                        i.[time],
                        defect = CASE i.[defect] WHEN 0 THEN 'OK' WHEN 1 THEN 'NOK' END,                                           
                  FROM inspection AS i WITH (NOLOCK) INNER JOIN programs as pg ON i.programid = pg.[ID]
                  WHERE i.[time] BETWEEN ... ///SOME MORE CODE IS FOLLOWING HERE!
                    ";

但现在我想在其中添加(may/may 不是)现有列 timeTo。

string sql = @"SELECT i.[ID] AS cID, 
                        pg.[Name], 
                        pg.[ID] AS pgid, 
                        i.[time],
                        defect = CASE i.[defect] WHEN 0 THEN 'OK' WHEN 1 THEN 'NOK' END,                      
                        timeTo = CASE i.[timeTo] WHEN EXISTS THEN i.[timeTo] ELSE NULL END                     
                  FROM inspection AS i WITH (NOLOCK) INNER JOIN programs as pg ON i.programid = pg.[ID]
                  WHERE i.[time] BETWEEN ... //SOME MORE CODE IS FOLLOWING HERE!
                    ";

我知道这个 "exists" 关键字很难,但是有什么 "one-line" 解决方案吗?我发现了几个额外的 case select 解决方案。由于我在多个不同列的地方需要此解决方案,因此 select 解决方案不适合。 感谢您的帮助

SQL 服务器不允许您引用不存在的字段。我怀疑这是因为查询优化器无法生成包含不存在字段的执行计划。

您的应用需要:

  1. 确定它正在使用哪个版本的数据库。
  2. 选择适当的查询。
  3. 确保它处理丢失的数据(通过关闭依赖于新字段的功能或通过选择适当的默认值)。

您可以执行 if 语句并查询系统表。您需要在动态 sql 中执行此操作才能解析。

declare @sql nvarchar(max);

if exists (
    select 1 from sys.columns c
    inner join sys.tables t on c.object_id = t.object_id
    where c.name = 'timeTo' and t.name = 'inspection')
begin
    set @sql = 'SELECT i.[ID] AS cID, 
        pg.[Name], 
        pg.[ID] AS pgid, 
        i.[time],
        defect = CASE i.[defect] WHEN 0 THEN ''OK'' WHEN 1 THEN ''NOK'' END,                      
        timeTo = CASE i.[timeTo] WHEN EXISTS THEN i.[timeTo] ELSE NULL END                     
    FROM inspection AS i WITH (NOLOCK) INNER JOIN programs as pg ON i.programid = pg.[ID]
    WHERE i.[time] BETWEEN ... //SOME MORE CODE IS FOLLOWING HERE!';
end
else 
begin
    set @sql = 'SELECT i.[ID] AS cID, 
        pg.[Name], 
        pg.[ID] AS pgid, 
        i.[time],
        defect = CASE i.[defect] WHEN 0 THEN ''OK'' WHEN 1 THEN ''NOK'' END,                                           
    FROM inspection AS i WITH (NOLOCK) INNER JOIN programs as pg ON i.programid = pg.[ID]
    WHERE i.[time] BETWEEN ... ///SOME MORE CODE IS FOLLOWING HERE!';
end
exec (@sql);