Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在SQL查询中检查表中的列是否存在_Sql_Sql Server_Apache Drill - Fatal编程技术网

如何在SQL查询中检查表中的列是否存在

如何在SQL查询中检查表中的列是否存在,sql,sql-server,apache-drill,Sql,Sql Server,Apache Drill,在上面的查询中,只有当表中存在operationType列时,才应执行where条件。如果不存在不需要条件的operationType列。对于此特定查询,您可以使用: 选择t1.customerInformation.customerid作为customerid 来自EndUserNotificationHistoryEvent t1 其中t1.operationType“DELETE”或t1.operationType为NULL; 如果列为NULL,则向where子句添加IS NULL检查将有

在上面的查询中,只有当表中存在operationType列时,才应执行where条件。如果不存在不需要条件的operationType列。

对于此特定查询,您可以使用:

选择t1.customerInformation.customerid作为customerid 来自EndUserNotificationHistoryEvent t1 其中t1.operationType“DELETE”或t1.operationType为NULL; 如果列为NULL,则向where子句添加IS NULL检查将有效地关闭delete检查。

尝试以下操作:

SELECT t1.customerInformation.customerid AS CustomerId
FROM EndUserNotificationHistoryEvent t1
WHERE t1.operationType <> 'DELETE';

除布尔运算外,另一种方法是按如下方式使用合并:

SELECT t1.customerInformation.customerid AS CustomerId
FROM EndUserNotificationHistoryEvent t1
WHERE (t1.operationType <> 'DELETE' OR t1.operationType IS NULL)

我试过了,但是得到了**错误:系统错误:NumberFormatException:DELETE**错误@Tim BiegeleisionWell你显然是想运行某种删除,但这不是你的问题所要问的。当我使用t1.operationType“DELETE”时,它会抛出系统错误:NumberFormatException:DELETE@Tim Biegeleision我尝试过,但它会抛出错误:系统错误:NumberFormatException:DELETE@ThiyaguWHERE CASTt1.operationType作为VARCHAR50“DELETE”或t1.operationType为NULL-Try此@kamalraje请包含EndUserNotificationHistoryEvent的表定义。您的问题似乎缺少一些关键信息。此处需要动态SQL,仅在列存在时包含WHERE子句。
SELECT T1.CUSTOMERINFORMATION.CUSTOMERID AS CUSTOMERID
  FROM ENDUSERNOTIFICATIONHISTORYEVENT T1
 WHERE COALESCE(T1.OPERATIONTYPE,'NOT DELETE') <> 'DELETE'; 
-- WHEN T1.OPERATIONTYPE IS NULL, CONDITION WILL BE ALWAYS TRUE