【问题标题】:How to use an And-Or as a variable如何使用 And-Or 作为变量
【发布时间】:2017-11-30 18:25:50
【问题描述】:

我实际上是在尝试从表中获取数据,但我希望“和/或”根据表进行更改。我正在使用 SQL Server。

我喜欢这样的事情:

DECLARE @TURNOS TABLE (ID INT IDENTITY(1, 1),
                       INITURNO INT, 
                       ENDTURNO INT, 
                       ANDOR VARCHAR(3)
                      );

INSERT INTO @TURNOS 
VALUES (23, 7, 'OR'), (7, 15, 'AND'), (15, 23, 'AND')

我正在尝试做这样的事情:

WHILE (@count <= (SELECT COUNT(ID) FROM @TURNOS))
BEGIN
    SET @initurno = SELECT INITURNO FROM @TURNOS WHERE ID = @count
    SET @endturno = SELECT ENDTURNO FROM @TURNOS WHERE ID = @count
    SET @andor = SELECT ANDOR FROM @TURNOS WHERE ID = @count

    INSERT INTO @AnotherTable
        SELECT * 
        FROM dbo.TableA 
        WHERE DATES BETWEEN DATEPART(hh, DATES) >= @initurno @andor DATEPART(hh, DATES) < @enturno
END

有什么方法可以使用变量和/或就像我尝试使用 @andor 一样?

提前致谢:)

【问题讨论】:

    标签: sql sql-server variables table-variable


    【解决方案1】:

    如果您确实需要将运算符作为变量,则可以使用动态 SQL。但是,如果您只需要条件查询,您只需包含两个条件并根据变量切换它们。

    insert into @AnotherTable
    select * from dbo.TableA 
    where 
    /*First condition */
    (
    @andor = 'AND'
    AND DATES between Datepart(hh,DATES)>= @INITURNO 
    AND Datepart(hh,DATES)< @ENDTURNO
    )
    
    OR
    /* Second condition */
    (
    @andor = 'OR'
    AND (
         DATES between Datepart(hh,DATES)>= @INITURNO 
         OR Datepart(hh,DATES)< @ENDTURNO
         )
    )
    

    【讨论】:

    • 哦,我什至没有注意到 GSazheniuk 注意到(并更正)使用 BETWEEN 运算符。
    【解决方案2】:

    您正在寻找动态 SQL。你可以这样试试;

    declare @query nvarchar(max)
    while(@count <= (select count(ID) from @TURNOS))
    begin
        SET @num1 = select INITURNO from @TURNOS  where ID = @count
        SET @num2 = select ENDTURNO from @TURNOS  where ID = @count
        SET @andor = select ANDOR  from @TURNOS where ID = @count
    
        set @query = 'insert into @AnotherTable
        select * from dbo.TableA where DATES between Datepart(hh,DATES)>= '+cast(@INITURNO as nvarchar(5))+' '+@andor+' Datepart(hh,DATES)< '+cast(@ENDTURNO as nvarchar(5))+''
        EXECUTE sp_executesql @query
    end
    

    【讨论】:

      【解决方案3】:

      我不是动态 SQL 的忠实粉丝,但即使我是,我认为这不是 BETWEEN 关键字的正确语法?

      这就是我的解决方案的样子,我不会复制整个查询,而只会复制 select 的一部分:

          select * from dbo.TableA 
          where (@andor = 'AND' AND Datepart(hh,DATES) BETWEEN @initurno AND @enturno) 
          OR (@andor = 'OR' AND (Datepart(hh,DATES) >= @initurno OR Datepart(hh,DATES) <= @enturno))
      

      请注意,我将&lt;=@enturno 一起使用,因为BETWEEN 就是这样工作的,它包含右限。如果您不想包含@enturno,则需要重写上层查询以在两种情况下都使用&gt;=&lt;

          select * from dbo.TableA 
          where (@andor = 'AND' AND Datepart(hh,DATES) >= @initurno AND Datepart(hh,DATES) < @enturno) 
          OR (@andor = 'OR' AND (Datepart(hh,DATES) >= @initurno OR Datepart(hh,DATES) < @enturno))
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-02-19
        • 1970-01-01
        • 2021-03-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-02-26
        相关资源
        最近更新 更多