I need to use values derived from a temporary table and use it as where criteria. Please see my code:
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 ASC
select * from #tempLaneNumber
set @laneNum = (select * from #tempLaneNumber)
begin
select COUNT(*)
from [dbo].[TOLL]
where convert(date, TRXN_DTIME) = @startDate and EXIT_LANE = @laneNum
end
If I run up to select * from #tempLaneNumber I get this result:
But if I use this values as a criteria on a where statement, on the begin statement, I don't get my expected result.

set @laneNum = (select * from #tempLaneNumber)will contain one of the values in your temp table. Maybe you wantwhere EXIT_LANE in (select LANE_NUMBER from #tempLaneNumber)