PostgreSQL遊標使用舉例

來源:互聯網
上載者:User

1、下面的代碼會建立一個top100cur()函數,該函數返回一個匿名遊標

  1. --drop function top100cur();   
  2.   
  3. create function top100cur() returns refcursor as $$  
  4. declare   
  5.     abc refcursor;  
  6. begin  
  7.     open abc for select * from person limit 100;  
  8.     return abc;  
  9. end  
  10. $$language plpgsql;  
  11.   
  12. SELECT top100cur();--返回匿名遊標   
  13. --fetch all from abc;--匿名遊標,所以此行錯誤  
2、下面的代碼會建立的函數會返回一個名字為abc的遊標(能發現差別吧?是的,是否匿名,取決於open遊標時,該遊標變數是否已經綁定)
  1. --drop function top100cur();   
  2.   
  3. create function top100cur() returns refcursor as $$  
  4. declare   
  5.     abc cursor for select * from person limit 100;  
  6. begin  
  7.     open abc;  
  8.     return abc;  
  9. end  
  10. $$language plpgsql;  
  11.   
  12. SELECT top100cur();--返回名字為abc的遊標   
  13. fetch all from abc;--此行正確,執行時,記得把此行與select行以前拖黑,然後F5  
3、返回由調用者命名的遊標
  1. --drop function top100cur(refcursor);   
  2. create function top100cur(refcursor) returns refcursor as $$  
  3. begin  
  4.     open $1 for select * from person limit 100;  
  5.     return $1;  
  6. end  
  7. $$language plpgsql;  
  8.   
  9. SELECT top100cur('abc');  
  10. fetch all from abc;  
4、返回一個名字叫$1的遊標
  1. --drop function top100cur(refcursor);   
  2. create function top100cur(refcursor) returns refcursor as $$  
  3. declare   
  4.     $1 cursor for select * from person limit 100;  
  5. begin  
  6.     open $1;--不open的話,返回的名字為"$1"遊標不能使用!   
  7.     return $1;  
  8. end  
  9. $$language plpgsql;  
  10.   
  11. SELECT top100cur('abc');  
  12. fetch all from "$1";  
5、返回一個匿名的遊標
  1. --drop function top100cur(refcursor);   
  2. create function top100cur(refcursor) returns refcursor as $$  
  3. declare   
  4.     $1 refcursor;  
  5. begin  
  6.     open $1 for select * from person limit 100;  
  7.     return $1;  
  8. end  
  9. $$language plpgsql;  
  10.   
  11. SELECT top100cur('abc');  
  12. --fetch all from "$1";--匿名遊標,所以此行錯誤  
6、返回由調用者指定的遊標(知道跟4和5的不同吧?4、5中,declare聲明的遊標變數名稱太特殊了,把匿名參數的名字隱藏了……)
  1. --drop function top100cur(refcursor);   
  2. create function top100cur(refcursor) returns refcursor as $$  
  3. declare   
  4.     abcdef refcursor;  
  5. begin  
  6.     open $1 for select * from person limit 100;  
  7.     return $1;  
  8. end  
  9. $$language plpgsql;  
  10.   
  11. SELECT top100cur('abc');  
  12. fetch all from "abc";  

7、直接open一個沒有被declare,也沒有被傳入的遊標變數

  1. --drop function top100cur(refcursor);   
  2. create function top100cur(refcursor) returns refcursor as $$  
  3. declare   
  4.     abcdef refcursor;  
  5. begin  
  6.     --open defg for select * from person limit 100;--錯誤:  "defg" 不是一個已知變數   
  7.     --return defg;-----------LINE 5:  open defg for select * from person limit 100;   
  8. end  
  9. $$language plpgsql;  

總結:

1、declare的變數,會導致函數參數被隱藏(如,declare $1後,則第一個匿名參數就被隱藏了);

2、declare只是聲明一個遊標,不會open遊標,而沒有open的遊標是不能用的哦~

3、declare之後再open遊標時,如果這個遊標是個未綁定的(declare時沒有cursor for XXX),那麼open後得到的是一個匿名遊標;

4、open操作的遊標變數,要麼是declare的,要麼是當做參數傳入的字串;除此之外,報錯!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.