最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

深入sql oracle遞歸查詢

 更新時間:2013年05月30日 10:17:46   作者:  
本篇文章是對sql oracle 遞歸查詢進行了詳細的分析介紹,需要的朋友參考下
☆ 獲取數(shù)據(jù)庫所有表名,表的所有列名
   select name from sysobjects where xtype='u'
   select name from syscolumns where id=(select max(id) from sysobjects where xtype='u' and name='表名')

☆ 遞歸查詢數(shù)據(jù)
Sql語句里的遞歸查詢 SqlServer2005和Oracle 兩個版本
以前使用Oracle,覺得它的遞歸查詢很好用,就研究了一下SqlServer,發(fā)現(xiàn)它也支持在Sql里遞歸查詢
舉例說明:
SqlServer2005版本的Sql如下:
比如一個表,有id和pId字段,id是主鍵,pid表示它的上級節(jié)點,表結構和數(shù)據(jù):
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表示它的上級節(jié)點,表結構和數(shù)據(jù)請參考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,也是用遞歸實現(xiàn),具體如下:
假設有個銷售表如下:
CREATE TABLE [tb](
    [qj] [int] NULL,    -- 月份,本測試假設從1月份開始,并且數(shù)據(jù)都是連續(xù)的月份,中間沒有隔斷
    [je] [int] NULL,    -- 本月銷售實際金額
    [rwe] [int] NULL,    -- 本月銷售任務額
    [fld] [float] NULL    -- 本月金額大于任務額時的返利點,返利額為je*fld
) ON [PRIMARY]
現(xiàn)在要求計算每個月的返利金額,規(guī)則如下:
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

相關文章

最新評論

娱乐| 广南县| 临邑县| 临沧市| 林口县| 龙泉市| 滨海县| 新竹县| 安化县| 庄浪县| 元江| 鹿邑县| 高陵县| 深水埗区| 都兰县| 武川县| 钦州市| 崇仁县| 叶城县| 乌审旗| 太康县| 大余县| 茂名市| 寿宁县| 巴林右旗| 大化| 个旧市| 桐柏县| 从化市| 江永县| 柘城县| 绍兴市| 凌源市| 镇沅| 潼南县| 湟源县| 弥渡县| 莎车县| 长岭县| 浑源县| 南部县|