简体   繁体   English

用于检查列是否存在的案例表达式抛出无效的列名错误

[英]Case expression to check if column exists throws invalid column name error

I'm building a Stored Procedure meant to be run daily.我正在构建一个每天运行的存储过程。 Due to how the data is provided me, some days some of the columns necessary for the output are not in the file that's imported to a temporary table.由于向我提供数据的方式,有时 output 所需的一些列不在导入到临时表的文件中。

For the output, the value can be null/empty, but the actual column has to be there.对于 output,该值可以为 null/空,但实际列必须存在。

I've tried the following code:我试过以下代码:

select

case

when exists(
        select * from INFORMATION_SCHEMA.COLUMNS
    where TABLE_NAME = 'tableName' and COLUMN_NAME = 'ABC'
    ) then ABC
                
else ''                 
end 
as 'XYZ' from tableName

I know for a fact that in the tests I'm running the column DOES NOT exist, so I'm expecting the SELECT statment to simply return an empty string for column XYZ.我知道一个事实,即在我运行的测试中,列不存在,所以我期望 SELECT 语句只为 XYZ 列返回一个空字符串。

However when running such SELECT statement, I get the following error:但是,当运行这样的 SELECT 语句时,出现以下错误:

Invalid column name 'ABC'.列名称“ABC”无效。

Since I know from the start that the column ABC does not exist, I was expecting that the EXISTS(...) would evaluate to FALSE and jump straight to the ELSE statement.因为我从一开始就知道 ABC 列不存在,所以我期望 EXISTS(...) 的计算结果为 FALSE 并直接跳转到 ELSE 语句。 However it seems that the column name is still evaluated.但是,似乎仍在评估列名。

How can I get around this?我该如何解决这个问题?

I believe i have solved my troubles thanks to the workaround provided by @MartinSmith ( https://dba.stackexchange.com/questions/66741/why-cant-i-use-a-case-statement-to-see-if-a-column-exists-and-not-select-from-i/66755#66755 )我相信由于@MartinSmith 提供的解决方法( https://dba.stackexchange.com/questions/66741/why-cant-i-use-a-case-statement-to-see-if- a-column-exists-and-not-select-from-i/66755#66755 )

I ended up using an alternative version of the workaround provided, linked by the poster of @MartinSmith's solution, but the results seem to be what i was expecting.我最终使用了提供的解决方法的替代版本,由 @MartinSmith 的解决方案的海报链接,但结果似乎是我所期望的。

Regarding the Only one expression can be specified in the select list when the subquery is not introduced with EXISTS error, it was being caused by me trying to SELECT two columns in the subquery.关于Only one expression can be specified in the select list when the subquery is not introduced with EXISTS错误,这是由我尝试 SELECT 子查询中的两列引起的。 Solution came from here: https://stackoverflow.com/a/7684626/18191554解决方案来自这里: https://stackoverflow.com/a/7684626/18191554

Now, my code is as follows:现在,我的代码如下:

select
.
.
.
.
(select (SELECT [ABC]
           from dbo.tableName 
           where ID = temp.ID])
           from (select '' as [ABC]) as dummy)
           
        as 'XYZ',

.
.
.
from tableName

where ABC is the column that may or may not exist.其中 ABC 是可能存在也可能不存在的列。

Thanks to everyone involved感谢所有参与的人

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM