sql oracle 遞歸查詢(轉)

 

☆ 獲取數據庫所有表名,表的所有列名

      select name from sysobjects where xtype='u'
     select name from syscolumns where id=(select max(id) from sysobjects where xtype='u' and name='表名')

 

遞歸查詢數據
       

Sql語句裏的遞歸查詢 SqlServer2005和Oracle 兩個版本

 

以前使用Oracle,覺得它的遞歸查詢很好用,就研究了一下SqlServer,發現它也支持在Sql裏遞歸查詢
舉例說明:
SqlServer2005版本的Sql如下:
比如一個表,有id和pId字段,id是主鍵,pid表示它的上級節點,表結構和數據:
CREATE TABLE [aaa](
 [id] [int] NULL,
 [pid] [int] NULL,
 [name] [nchar](10)
)
GO
INSERT INTO aaa VALUES(1,0,'a')
INSERT INTO aaa VALUES(2,0,'b')
INSERT INTO aaa VALUES(3,1,'c')
INSERT INTO aaa VALUES(4,1,'d')
INSERT INTO aaa VALUES(5,2,'e')
INSERT INTO aaa VALUES(6,3,'f')
INSERT INTO aaa VALUES(7,3,'g')
INSERT INTO aaa VALUES(8,4,'h')
GO

--下面的Sql是查詢出1結點的所有子結點
with my1 as(select * from aaa where id = 1
 union all select aaa.* from my1, aaa where my1.id = aaa.pid
)
select * from my1 --結果包含1這條記錄,如果不想包含,可以在最後加上:where id <> 1

--下面的Sql是查詢出8結點的所有父結點
with my1 as(select * from aaa where id = 8
 union all select aaa.* from my1, aaa where my1.pid = aaa.id
)
select * from my1;

 --下面是遞歸刪除1結點和所有子結點的語句:
with my1 as(select * from aaa where id = 1
   union all select aaa.* from my1, aaa where my1.id = aaa.pid
)
delete from aaa where exists (select id from my1 where my1.id = aaa.id) 

Oracle版本的Sql如下:
比如一個表,有id和pId字段,id是主鍵,pid表示它的上級節點,表結構和數據請參考SqlServer2005的,Sql如下:
--下面的Sql是查詢出1結點的所有子結點
 SELECT * FROM aaa
  START WITH id = 1
CONNECT BY pid = PRIOR id

--下面的Sql是查詢出8結點的所有父結點
 SELECT * FROM aaa
  START WITH id = 8
CONNECT BY PRIOR pid = id

今天幫別人做了一個有點意思的sql,也是用遞歸實現,具體如下:
假設有個銷售表如下:
CREATE TABLE [tb](
    [qj] [int] NULL,    -- 月份,本測試假設從1月份開始,並且數據都是連續的月份,中間沒有隔斷
    [je] [int] NULL,    -- 本月銷售實際金額
    [rwe] [int] NULL,    -- 本月銷售任務額
    [fld] [float] NULL    -- 本月金額大於任務額時的返利點,返利額爲je*fld
) ON [PRIMARY]
現在要求計算每個月的返利金額,規則如下:
1月份銷售金額大於任務額  返利額=金額*返利點
2月份銷售金額大於任務額  返利額=(金額-1月份返利額)*返利點
3月份銷售金額大於任務額  返利額=(金額-1,2月份返利額)*返利點
以後月份依次類推,銷售額小於任務額時,返利爲0
具體的Sql如下:
WITH my1 AS (
                SELECT *,
                       CASE
                            WHEN je > rwe THEN (je * fld)
                            ELSE 0
                       END fle,
                       CAST(0 AS FLOAT) tmp
                FROM   tb
                WHERE  qj = 1
                UNION ALL
                SELECT tb.*,
                       CASE
                            WHEN tb.je > tb.rwe THEN (tb.je - my1.fle -my1.tmp)
                                 * tb.fld
                            ELSE 0
                       END fle,
                       my1.fle + my1.tmp tmp -- 用於累加前面月份的返利
                FROM   my1,
                       tb
                WHERE  tb.qj = my1.qj + 1
            )
SELECT *
FROM   my1

 

SQLserver2008使用表達式遞歸查詢

--由父項遞歸下級
with cte(id,parentid,text)
as
(--父項
select id,parentid,text from treeview where parentid = 450
union all
--遞歸結果集中的下級
select t.id,t.parentid,t.text from treeview as t
inner join cte as c on t.parentid = c.id
)
select id,parentid,text from cte

---------------------

--由子級遞歸父項
with cte(id,parentid,text)
as
(--下級父項
select id,parentid,text from treeview where id = 450
union all
--遞歸結果集中的父項
select t.id,t.parentid,t.text from treeview as t
inner join cte as c on t.id = c.parentid
)
select id,parentid,text from cte

發佈了9 篇原創文章 · 獲贊 13 · 訪問量 28萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章