IF语句中的逻辑OR运算符

时间:2014-04-13 15:46:58

标签: sql sql-server sql-server-2008 tsql

我想如果col1_list不为null或不为空,或者如果col2_list不为null或不为空,请执行一些逻辑并设置rc = 0,否则设置rc = 1。如果某些col_list为null或为空设置rc 1。但逻辑OR运算符不适用于if语句或我做错了什么?

declare @col1_list varchar(max) , @col2_list varchar(max)
declare @tbl TABLE (col1 int , col2 int)
declare @rc char(1) = '0'
set @col1_list = '2|6|7|8|'
set @col2_list = '1|'

IF (@col1_list IS NOT NULL OR @col1_list <> '' OR @col2_list IS NOT NULL OR @col2_list <> '')
BEGIN
    DECLARE @myXML1 AS XML = N'<H><r>' + REPLACE(@col1_list, '|', '</r><r>') + '</r></H>'
    DECLARE @myXML2 AS XML = N'<H><r>' + REPLACE(@col2_list, '|', '</r><r>') + '</r></H>';

    with mycte as (SELECT Vals1.id.value('.', 'NVARCHAR(50)') AS val1
    FROM @myXML1.nodes('/H/r') AS Vals1(id)),
    mycte1 as (SELECT Vals2.id.value('.', 'NVARCHAR(50)') AS val2
    FROM @myXML2.nodes('/H/r') AS Vals2(id))

    insert into @tbl (col1,col2)
    select val1,val2
    from mycte,mycte1
    where val1 <> '' and val2 <> ''
    set @rc = '0'
END 
    ELSE
        set @rc ='1'

    select @rc as [rc]

当某些col_list为空或空

时,Rc = 0

1 个答案:

答案 0 :(得分:1)

尝试将if条件更改为:

IF (NOT(@col1_list IS NULL OR @col1_list = '') AND NOT(@col2_list IS NULL OR @col2_list = ''))

这个检查两件事:

  1. @col1_list既不是空也不是空,和
  2. @col2_list既不是空也不是空的。