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

t-sql :與父母,孩子,孫子...... n 個孩子一起獲得

t-sql : get a single row with parents, children, grandchildren... n children(t-sql :與父母,孩子,孫子...... n 個孩子一起獲得一行)
本文介紹了t-sql :與父母,孩子,孫子...... n 個孩子一起獲得一行的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我有一個包含 2 列的表格:ID, ParentID

I have a table with 2 columns: ID, ParentID

我想創建一個新表/視圖,每一行都包含所有級別的所有孩子到某個父母...

i want to make a new table/view that each row includes all the children in all levels to a certain parents...

例如:

ParentID  Level_1  Level_2  Level_3 ... Level_n

這意味著 parentID 是 Level_1 的父級,它是 level_3 的父級 2 的父級,依此類推...

it means that parentID is the parent of Level_1 which is the Parent of level 2 which is the parent of level_3 and so on...

我需要做沒有遞歸所以沒有機會堆棧溢出...

i need to do it with no recursion so no chance to stack overflow...

在 T-SQL 中有一個名為 @@rowcount 的函數,它返回我在最后一次選擇中得到的行,所以也許我可以在 while 循環或其他東西中使用它...

in T-SQL there's a function called @@rowcount which returns the rows i got in the last select so maybe i can use it inside a while loop or something...

問題是我怎么知道一開始要構造多少列?知道如何參加這個挑戰嗎?

the question is how can i know how many columns to construct at the beginning ? any idea how to access this challenge ?

推薦答案

你想要得到的是一個數據透視表,但在我看來,這個任務不屬于數據庫.我寧愿得到一個包含行的結果集并從外部"旋轉它們.

What you're trying to get is a pivot table, but, in my opinion, this task doesn't belong to the database. I'd rather get a result set with rows and pivot them "externally".

為了實現我所描述的,在 SQL Server 2005+ 中,您可以使用公共表表達式 (在這里你可以找到一個例子,而 SQL Server 2000 需要一個稍微不同的方法.

To achieve what I described, in SQL Server 2005+ you can use a Common Table Expression (here you can find an example, while SQL Server 2000 requires a slightly different approach.

注意:雖然你寫了沒有遞歸所以沒有機會堆棧溢出",你仍然需要保護自己免受無限循環的影響.我個人使用遞歸并為其建立最大深度";當遞歸嘗試比限制更深"時,我停止它并返回錯誤.

Note: although you wrote "no recursion so no chance to stack overflow", you'll still have to proect yourself from infinite loops. I personally use recursion and establish a maximum "depth" for it; when the recursion attempts to go "deeper" than the limit, I stop it and return an error.

這篇關于t-sql :與父母,孩子,孫子...... n 個孩子一起獲得一行的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

Converting Every Child Tags in to a Single Column with multiple Delimiters -SQL Server (3)(將每個子標記轉換為具有多個分隔符的單列-SQL Server (3))
How can I create a view from more than one table?(如何從多個表創建視圖?)
Create calculated value based on calculated value inside previous row(根據前一行內的計算值創建計算值)
How do I stack the first two columns of a table into a single column, but also pair third column with the first column only?(如何將表格的前兩列堆疊成一列,但也僅將第三列與第一列配對?) - IT屋-程序員軟件開發技
Recursive t-sql query(遞歸 t-sql 查詢)
Convert Month Name to Date / Month Number (Combinations of Questions amp; Answers)(將月份名稱轉換為日期/月份編號(問題和答案的組合))
主站蜘蛛池模板: 亚洲精品乱码久久久久久蜜桃91 | 国产精品高清一区二区三区 | 成人中文字幕在线观看 | 色婷婷久久久久swag精品 | 午夜爽爽爽男女免费观看影院 | 一区欧美 | 午夜精品久久 | 久久国产亚洲 | 国产va| 男女视频免费 | 色在线免费视频 | 国产成人午夜电影网 | 亚洲精品一区二区三区蜜桃久 | 免费看黄色国产 | 免费在线观看成人 | 成人免费看| 91精品无人区卡一卡二卡三 | 在线视频一区二区 | 男女羞羞的网站 | 日韩精品一二三区 | 国产三级精品视频 | 干狠狠 | 玖玖玖在线观看 | 国产高清精品在线 | 久久久久亚洲精品 | h免费观看| 91就要激情 | av免费网站在线观看 | 免费黄色大片 | 欧美精品一区二区三区四区 在线 | 国产一区二区免费电影 | 99精品热视频 | 国产精品一区二区欧美黑人喷潮水 | 黑人巨大精品欧美一区二区一视频 | 亚洲综合精品 | 精品国产一区二区三区成人影院 | 在线国产视频 | 一区二区三区在线免费 | 日韩av成人在线 | 色橹橹欧美在线观看视频高清 | 国产欧美日韩在线 |