SQL Server UNION在WHILE语句中进行多次选择
我在存储过程中有一个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
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解决方案:)