Ibanez1408
Ibanez1408

Reputation: 5058

Iterate through derived value from temp table so it value can be used it a where condition using for loop

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...

enter image description here

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?

EDIT

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

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?

Upvotes: 0

Views: 61

Answers (1)

PSK
PSK

Reputation: 17943

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

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

EDIT:

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

Upvotes: 1

Related Questions