对多列使用IS NOT NULL

时间:2016-02-29 23:28:33

标签: sql-server

我想检查WHERE子句中单个SQL语句中多列的is not null约束,有没有办法这样做? 另外,我不想要在列定义上强制执行NOT NULL类型约束。

SELECT * FROM AB_DS_TRANSACTIONS 
WHERE FK_VIOLATION IS NULL 
AND TRANSACTION_ID NOT IN(
    SELECT distinct TRANSACTION_ID FROM AB_TRANSACTIONS) 
AND COUNTRY_ID IS NOT NULL 
AND GEO_CUST_COUNTRY_ID IS NOT NULL 
AND INVOICE_DATE IS NOT NULL 
AND ABB_GLOBALID IS NOT NULL 
AND SALES_ORG_ID IS NOT NULL 
AND DIST_ID IS NOT NULL 
AND CUSTOMER_ID IS NOT NULL 
AND REPORT_UNIT_ID IS NOT NULL 
AND CURR_INVOICE IS NOT NULL 
AND DIVISION_CODE IS NOT NULL 

因此我不是一次又一次地使用IS NOT NULL而是想简化一些事情

4 个答案:

答案 0 :(得分:9)

您可以使用

SELECT * FROM table1 
WHERE NOT (Column1 IS NULL OR 
Column2 IS NULL OR
 Column3 IS NULL OR
 Column4 IS NULL
    IS NOT NULL)

根据OP评论,更新答案

使用INSERT和SELECT子查询插入行

INSERT INTO Table_A
    SELECT column1, column2, column3,column4 
    FROM Table_B 
    WHERE NOT (Column1 IS NULL OR 
    Column2 IS NULL OR
    Column3 IS NULL OR
    Column4 IS NULL
    IS NOT NULL);

您的查询

我可以减少50个字符

SELECT * FROM AB_DS_TRANSACTIONS 
WHERE 
FK_VIOLATION IS NULL 
AND TRANSACTION_ID NOT 
IN(SELECT distinct TRANSACTION_ID FROM AB_TRANSACTIONS) 
AND 
NOT (
COUNTRY_ID IS NULL  
OR GEO_CUST_COUNTRY_ID IS NULL 
OR INVOICE_DATE IS NULL 
OR ABB_GLOBALID IS NULL 
OR SALES_ORG_ID IS NULL 
OR DIST_ID IS NULL 
OR CUSTOMER_ID IS NULL 
OR REPORT_UNIT_ID IS NULL 
OR CURR_INVOICE IS NULL 
OR DIVISION_CODE IS NULL
) 

答案 1 :(得分:1)

我认为您要查找的语法只显示所有非空的行

 function myFunction(el) {
     el.nextSibling.classList.toggle('show');
 }


 <button onclick="myFunction(this)" class="dropbtn">SPECIFY PRODUCT</button>

答案 2 :(得分:1)

在BigQuery中(可能在其他数据库中工作)-我将使用concat函数。确保在需要时具有字符串或将字段强制转换为字符串。如果其中一个字段为空,则Concat返回null。

SELECT * FROM AB_DS_TRANSACTIONS 
WHERE FK_VIOLATION IS NULL 
AND TRANSACTION_ID NOT IN(
    SELECT distinct TRANSACTION_ID FROM AB_TRANSACTIONS) 
AND concat(COUNTRY_ID,GEO_CUST_COUNTRY_ID,INVOICE_DATE,ABB_GLOBALID,SALES_ORG_ID,DIST_ID,CUSTOMER_ID,REPORT_UNIT_ID,CURR_INVOICE,DIVISION_CODE) IS NOT NULL 

答案 3 :(得分:1)

SELECT * FROM AB_DS_TRANSACTIONS 
WHERE COALESCE(COUNTRY_ID,GEO_CUST_COUNTRY_ID,INVOICE_DATE,ABB_GLOBALID,SALES_ORG_ID,DIST_ID,CUSTOMER_ID,REPORT_UNIT_ID,CURR_INVOICE,DIVISION_CODE) IS NOT NULL