Đỉnh NGUYỄN

life's a journey not a destination

Oracle PL/SQL – Cách viết đệ quy đơn giản

Leave a comment


Đây là ví dụ đơn giản cách viết đệ quy trong Oracle PL/SQL dùng mệnh đề “WITH”.

/* ===== code ===== */  
                                                         
WITH  
   factorial ( n, val )  as
   (   /*=== The Recursive WITH ===*/
       select  1 as n,  1 as val  
       from  dual
       UNION ALL 
       select  n+1 as n,  (n+1)*val as val
       from  factorial
       where  n < 8
   )
   select  n
      , val
   from  factorial
   order  by  n 
 ;
                                                                          
/* --- Results --- */    
                                                         
--  N   VAL   
--  --  ------ 
--  1   1     
--  2   2     
--  3   6     
--  4   24    
--  5   120   
--  6   720   
--  7   5040  
--  8   40320 

Nguồn tham khảo: http://steve-lyon.blogspot.com/2011/07/recursive-with-simple-factorial.html

Advertisements

Author: dinhnn

Senior software developer, a technical leader. You can be reached at via email to dinhnguyenngoc@gmail.com, via my blog at dinhnguyenngoc.wordpress.com, and on Twitter @dinhnguyenngoc.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s