久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

迭代來自臨時表的派生值,以便它的值可以用于

Iterate through derived value from temp table so it value can be used it a where condition using for loop(迭代來自臨時表的派生值,以便它的值可以用于使用 for 循環的 where 條件) - IT屋-程序員軟件開發技術分享
本文介紹了迭代來自臨時表的派生值,以便它的值可以用于使用 for 循環的 where 條件的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我可以從派生表中獲取每個項目的總數,如下所示:

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模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Modify Existing decimal places info(修改現有小數位信息)
The correlation name #39;CONVERT#39; is specified multiple times(多次指定相關名稱“CONVERT)
T-SQL left join not returning null columns(T-SQL 左連接不返回空列)
remove duplicates from comma or pipeline operator string(從逗號或管道運算符字符串中刪除重復項)
Change an iterative query to a relational set-based query(將迭代查詢更改為基于關系集的查詢)
concatenate a zero onto sql server select value shows 4 digits still and not 5(將零連接到 sql server 選擇值仍然顯示 4 位而不是 5)
主站蜘蛛池模板: 91麻豆蜜桃一区二区三区 | 无码一区二区三区视频 | 视频在线一区二区 | 成人午夜免费视频 | 嫩草影院网址 | 亚洲国产成人精品女人久久久 | 日韩av视屏 | 欧美日韩中文字幕在线 | 久久一区二区三区四区五区 | 日韩精品一区二区三区第95 | 在线观看av网站永久 | 午夜精品一区 | 国产精品污www在线观看 | 欧美日韩高清一区 | 久久天堂网 | 精品欧美乱码久久久久久 | 日韩伦理一区二区三区 | 欧美日韩一区二区在线播放 | 日本a视频 | 成人午夜精品 | 精品一区二区久久久久久久网精 | 欧美日韩国产一区二区三区 | 伊人青青久久 | 久久久精选 | 一区二区三区久久 | 综合久久综合久久 | 午夜视频在线免费观看 | 日韩午夜激情 | 亚洲视频免费在线观看 | 亚洲男人天堂网 | 综合久久国产 | 久久久91精品国产一区二区三区 | 69视频在线播放 | 久久久久久999 | 日韩激情网| 天天操夜夜骑 | 成人综合一区二区 | 亚洲免费观看 | 荷兰欧美一级毛片 | 高清黄色 | 99亚洲精品视频 |