問題描述
我可以從派生表中獲取每個項目的總數,如下所示:
I can get the total for each of the items from a derived table like so:
declare @laneNum int
declare @startDate date = '2019-02-07'
declare @class int = 1
declare @id int
if OBJECT_ID('tempdb..#tempLaneNumber') IS NOT NULL
drop table [#tempLaneNumber]
create table #tempLaneNumber
(
LANE_NUMBER INT NULL
)
INSERT INTO #tempLaneNumber (LANE_NUMBER)
SELECT DISTINCT EXIT_LANE
FROM [dbo].[TOLL]
ORDER BY EXIT_LANE DESC
select l.LANE_NUMBER, COUNT(*)
from [dbo].[TOLL] t
inner join #tempLaneNumber l on t.EXIT_LANE = l.LANE_NUMBER
where convert(date, TRXN_DTIME) = @startDate
GROUP BY l.LANE_NUMBER
但我現在需要的是遍歷每個派生值,以便我可以在語句中使用它,其中每個結果都可以放在變量中.這是我在當前代碼中得到的......
But what I need now is to iterate through each of the derived values so I can use it in a statement where each result can be placed in a variable. This is what I get in my current code...
我需要將 LANE_NUMBER 4 放入 x4 變量,將 LANE_NUMBER 6 放入 x6 變量,依此類推.我如何獲得它?
I need to put LANE_NUMBER 4 into x4 variable and LANE_NUMBER 6 into x6 variable and so forth. How do I get to it?
編輯
declare @laneNum int
declare @startDate date = '2019-02-07'
declare @class int = 1
declare @id int
if OBJECT_ID('tempdb..#tempLaneNumber') IS NOT NULL
drop table [#tempLaneNumber]
create table #tempLaneNumber
(
LANE_NUMBER INT NULL
)
INSERT INTO #tempLaneNumber (LANE_NUMBER)
SELECT DISTINCT EXIT_LANE
FROM [dbo].[TOLL]
ORDER BY EXIT_LANE DESC
;WITH CTE AS
(
select l.LANE_NUMBER, COUNT(*) CT
from [dbo].[TOLL] t
inner join #tempLaneNumber l on t.EXIT_LANE = l.LANE_NUMBER
where convert(date, TRXN_DTIME) = @startDate
GROUP BY l.LANE_NUMBER
)
SELECT * FROM CTE where LANE_NUMBER = 4
這是對的,但問題是我需要對值4"或6"或7"進行硬編碼.這個樣本有 4 個結果,所以沒關系.但是如果我有 10 個或更多呢?
This is about right but the problem is I would need to hardcode the value "4" or "6" or "7". This sample has 4 results so it's okay. but what if I have 10 or more?
推薦答案
如果你想稍后使用結果,你可以使用如下臨時表.
If you want to use the result later on you can use temp table like following.
create table #Results
(
LANE_NUMBER INT NULL,
[Count_LN] INT
)
INSERT INTO #Results
select l.LANE_NUMBER, COUNT(*) CT
from [dbo].[TOLL] t
inner join #tempLaneNumber l on t.EXIT_LANE = l.LANE_NUMBER
where convert(date, TRXN_DTIME) = @startDate
GROUP BY l.LANE_NUMBER
如果你想在下一個語句中立即使用輸出,你可以像下面那樣使用 CTE.
If you want to use the output immediately in the next statement, you can go for CTE like following.
;WITH CTE AS
(
select l.LANE_NUMBER, COUNT(*) CT
from [dbo].[TOLL] t
inner join #tempLaneNumber l on t.EXIT_LANE = l.LANE_NUMBER
where convert(date, TRXN_DTIME) = @startDate
GROUP BY l.LANE_NUMBER
)
SELECT * FROM CTE --USER YOUR CTE HERE
我無法完全理解您的要求,無論出于何種原因,如果您想迭代表并將每一行的列值存儲到變量中,您可以嘗試如下.
I am not able to understand your requirement fully, by any reason if you want to iterate the table and store every row's column value into a variable, you can try like following.
create table #Results
(
LANE_NUMBER INT NULL,
[Count_LN] INT
)
INSERT INTO #Results
select l.LANE_NUMBER, COUNT(*) CT
from [dbo].[TOLL] t
inner join #tempLaneNumber l on t.EXIT_LANE = l.LANE_NUMBER
where convert(date, TRXN_DTIME) = @startDate
GROUP BY l.LANE_NUMBER
declare @ln int
declare @ct int
While (Select Count(*) From #Results) > 0
Begin
select top 1 @ln = LANE_NUMBER, @ct = [Count_LN] from #Results
-- Use the variable @ln and @ct. For example, if you want to call a sp
-- exec call_someothersp @ln,@ct
Delete From #Results Where LANE_NUMBER = @ln and [Count_LN]=@ct
End
這篇關于迭代來自臨時表的派生值,以便它的值可以用于使用 for 循環的 where 條件的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!