Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/84.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 Server UNION在WHILE语句中进行多次选择_Sql_Sql Server - Fatal编程技术网

SQL Server UNION在WHILE语句中进行多次选择

SQL Server UNION在WHILE语句中进行多次选择,sql,sql-server,Sql,Sql Server,我在存储过程中有一个WHILE语句: DECLARE @reverseindex int DECLARE @index INT SET @index = 1 WHILE (@index <= @workingyears) BEGIN SET @reverseindex = @workingyears-@index+1 SELECT daysallowed, date, holidaytype, leavetype, isactive FROM setup_h

我在存储过程中有一个WHILE语句:

DECLARE @reverseindex int
DECLARE @index INT

SET @index = 1

WHILE (@index <= @workingyears)
BEGIN
    SET @reverseindex = @workingyears-@index+1

    SELECT daysallowed, date, holidaytype, leavetype, isactive 
    FROM setup_holiday_schedule 
    WHERE workingyears = @reverseindex
      AND holidayschedulecode = @holidayschedulecode
END
DECLARE@reverseindex int
声明@index INT
设置@index=1

而(@index一个表值变量怎么样

DECLARE @reverseindex int
DECLARE @index INT
DECLARE @results TABLE (
    daysallowed INT,
    date DATE,
    holidaytype INT,
    leavetype INT,
    isactive BIT
)

SET @index = 1
WHILE (@index <= @workingyears)
BEGIN

    SET @reverseindex = @workingyears-@index+1
    INSERT INTO @results (
        daysallowed, date, holidaytype, leavetype, isactive
    )
    SELECT daysallowed, date, holidaytype, leavetype, isactive 
    FROM setup_holiday_schedule 
    WHERE workingyears = @reverseindex
    AND holidayschedulecode = @holidayschedulecode

END

SELECT daysallowed, date, holidaytype, leavetype, isactive FROM @results
DECLARE@reverseindex int
声明@index INT
声明@results表(
daysallowed INT,
日期,
holidaytype INT,
类型INT,
无活性钻头
)
设置@index=1

WHILE(@index为什么您需要一个
WHILE
函数?我认为这个查询做的事情基本相同:

SELECT daysallowed, date, holidaytype, leavetype, isactive 
FROM setup_holiday_schedule 
WHERE workingyears between 1 and @workingyears AND
      holidayschedulecode = @holidayschedulecode
ORDER BY workingyears desc;

我假设你不想只选择工作年数在1和@workingyears之间的位置,然后按工作年数顺序描述。对吗?确切地说,我想留下WHILE语句,这只是一个例子,你可以创建一个临时表,在
WHILE
循环中插入,然后在最后从中选择。是的,我认为临时表就是ri解决方案:)