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

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

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

問題描述

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

I have a table with 2 columns: ID, ParentID

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

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 的函數(shù),它返回我在最后一次選擇中得到的行,所以也許我可以在 while 循環(huán)或其他東西中使用它...

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

問題是我怎么知道一開始要構(gòu)造多少列?知道如何參加這個挑戰(zhàn)嗎?

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

推薦答案

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

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

為了實現(xiàn)我所描述的,在 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.

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

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.

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

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

相關(guān)文檔推薦

Converting Every Child Tags in to a Single Column with multiple Delimiters -SQL Server (3)(將每個子標記轉(zhuǎn)換為具有多個分隔符的單列-SQL Server (3))
How can I create a view from more than one table?(如何從多個表創(chuàng)建視圖?)
Create calculated value based on calculated value inside previous row(根據(jù)前一行內(nèi)的計算值創(chuàng)建計算值)
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屋-程序員軟件開發(fā)技
Recursive t-sql query(遞歸 t-sql 查詢)
Convert Month Name to Date / Month Number (Combinations of Questions amp; Answers)(將月份名稱轉(zhuǎn)換為日期/月份編號(問題和答案的組合))
主站蜘蛛池模板: 97色在线视频 | 仙人掌旅馆在线观看 | 成人av网站在线观看 | 一级爱爱片 | 欧美精品在线一区 | 二区三区av | 中文精品视频 | 天天艹 | 999久久久国产精品 欧美成人h版在线观看 | 欧美1区2区 | 亚洲国产成人av好男人在线观看 | 国产不卡一 | 91久久国产综合久久 | 国产精品区二区三区日本 | 性欧美xxxx | 日韩精品在线观看一区二区三区 | 欧美一级淫片免费视频黄 | 日韩高清成人 | 精品1区| 国产日韩视频在线 | 亚洲欧美一区二区三区情侣bbw | 日韩美女在线看免费观看 | 成年人在线观看 | 亚洲午夜视频 | 久久久www成人免费精品 | 一片毛片 | 日韩免费毛片 | 久草在线在线精品观看 | av一区二区三区在线观看 | 国产在线观看一区二区三区 | 亚洲a在线观看 | 国产欧美精品一区二区色综合 | 污片在线观看 | 一区二区三区四区在线 | 亚洲成人免费视频在线 | 中文日韩在线 | 亚洲精品资源 | 欧美亚洲视频 | 国产japanhdxxxx麻豆 | 久久国产精品久久 | 蜜桃日韩|