T Sql ,自定義函式,返回遞迴結果集

2021-08-26 22:52:40 字數 558 閱讀 5903

寫程式是總是用到父子關係的資料,通過給定節點得到其子節點的記錄,寫檢視但是不支援傳入引數。

那就用 自定義函式來完成這個需求吧!

1.建立檢視

create function myfunc(@id int)

returns @tab table (id int,parentid int,[level] int,tname nvarchar(50))

asbegin

--declare @typeid int;

--set @typeid =6;

with cte as

(select * from tab where id= @id

union all

select a.* from tab a, cte b

where a.parentid = b.id

)insert @tab select id ,parentid,[level],tname from cte;

return

end2.錶值函式呼叫

select * from dbo.myfunc(6)

T SQL程式設計 使用者自定義函式 標量函式

在使用sql server的時候,除了其內建的函式之外,還允許使用者根據需要自己定義函式。根據使用者定義函式返回值的型別,可以將使用者定義的函式分為三個類別 如果使用者定義函式包含了單個select語句且語句可更新,則該函式返回的表也可更新,這樣的函式稱為內嵌錶值函式。如果使用者定義函式包含多個se...

返回標量CLR自定義函式

昨天有學習了返回表自定義函式 clr table valued函式 今天學習另乙個,實現返回標量 scalar valued function。這個標量函式獲取分類全名。select categoryname kindname fruitname from dbo tvf fruit where f...

MySQL自定義函式遞迴查詢

用於遞迴查詢id 通過parentid關聯 引數為int 型別的值 create definer root function getchildlist rootid int returns text charset utf8 begin declare stemp text declare stem...