[英]SQL Server FOR EACH Loop
我有以下SQL查詢:
DECLARE @MyVar datetime = '1/1/2010'
SELECT @MyVar
這自然會返回'2010年1月1日'。
我想要做的是有一個日期列表,比如說:
1/1/2010
2/1/2010
3/1/2010
4/1/2010
5/1/2010
然后我想通過數字FOR FOR EACH並運行SQL Query。
像(偽代碼)的東西:
List = 1/1/2010,2/1/2010,3/1/2010,4/1/2010,5/1/2010
For each x in List
do
DECLARE @MyVar datetime = x
SELECT @MyVar
所以這會回來: -
2010年1月1日2010年1月1日2010年1月1日2010年4月1日2010年5月1日
我希望這將數據作為一個結果集而不是多個結果集返回,因此我可能需要在查詢結束時使用某種聯合,因此循環的每次迭代都會聯合到下一個結果集。
編輯
我有一個接受'到期'參數的大型查詢,我需要運行24次,每次都需要能夠提供特定的日期(這些日期將是動態的)我想避免使用union alls重復我的查詢24次,就好像我需要回來添加額外的列一樣,這將是非常耗時的。
SQL主要是一種面向集合的語言 - 在其中使用循環通常是一個壞主意。
在這種情況下,使用遞歸CTE可以實現類似的結果:
with cte as
(select 1 i union all
select i+1 i from cte where i < 5)
select dateadd(d, i-1, '2010-01-01') from cte
這是一個帶有表變量的選項:
DECLARE @MyVar TABLE(Val DATETIME)
DECLARE @I INT, @StartDate DATETIME
SET @I = 1
SET @StartDate = '20100101'
WHILE @I <= 5
BEGIN
INSERT INTO @MyVar(Val)
VALUES(@StartDate)
SET @StartDate = DATEADD(DAY,1,@StartDate)
SET @I = @I + 1
END
SELECT *
FROM @MyVar
您可以使用臨時表執行相同操作:
CREATE TABLE #MyVar(Val DATETIME)
DECLARE @I INT, @StartDate DATETIME
SET @I = 1
SET @StartDate = '20100101'
WHILE @I <= 5
BEGIN
INSERT INTO #MyVar(Val)
VALUES(@StartDate)
SET @StartDate = DATEADD(DAY,1,@StartDate)
SET @I = @I + 1
END
SELECT *
FROM #MyVar
您應該告訴我們您的主要目標是什么,正如@JohnFx所說,這可能是另一種(更有效)的方式。
您可以使用變量表,如下所示:
declare @num int
set @num = 1
declare @results table ( val int )
while (@num < 6)
begin
insert into @results ( val ) values ( @num )
set @num = @num + 1
end
select val from @results
這種取決於你想要對結果做什么。 如果您只是在數字之后,基於集合的選項將是一個數字表 - 它可以用於各種各樣的事情。
對於MSSQL 2005+,您可以使用遞歸CTE生成內聯數字表:
;WITH Numbers (N) AS (
SELECT 1 UNION ALL
SELECT 1 + N FROM Numbers WHERE N < 500
)
SELECT N FROM Numbers
OPTION (MAXRECURSION 500)
declare @counter as int
set @counter = 0
declare @date as varchar(50)
set @date = cast(1+@counter as varchar)+'/01/2013'
while(@counter < 12)
begin
select cast(1+@counter as varchar)+'/01/2013' as date
set @counter = @counter + 1
end
當然是一個老問題。 但我有一個簡單的解決方案,不需要循環,CTE,表變量等。
DECLARE @MyVar datetime = '1/1/2010'
SELECT @MyVar
SELECT DATEADD (DD,NUMBER,@MyVar)
FROM master.dbo.spt_values
WHERE TYPE='P' AND NUMBER BETWEEN 0 AND 4
ORDER BY NUMBER
注意: spt_values
是Mircrosoft的未記錄表。 它有各種類型的數字。 它不能被使用,因為它可以在沒有事先信息的任何新版本的sql server中刪除,因為它沒有文檔。 但是我們可以在某些情況下將其用作快速解決方法,如上所述。
[CREATE PROCEDURE [rat].[GetYear]
AS
BEGIN
-- variable for storing start date
Declare @StartYear as int
-- Variable for the End date
Declare @EndYear as int
-- Setting the value in strat Date
select @StartYear = Value from rat.Configuration where Name = 'REPORT_START_YEAR';
-- Setting the End date
select @EndYear = Value from rat.Configuration where Name = 'REPORT_END_YEAR';
-- Creating Tem table
with [Years] as
(
--Selecting the Year
select @StartYear [Year]
--doing Union
union all
-- doing the loop in Years table
select Year+1 Year from [Years] where Year < @EndYear
)
--Selecting the Year table
selec]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.