PostgreSQL 存儲過程 通過設定條件,返回指定的數據表記錄


PL/pgSQL是 PostgreSQL 數據庫系統的一個可裝載的過程語言。

PL/pgSQL的設計目標是創建一種可裝載的過程語言,可以可用於創建函數和觸發器過程, 在SQL語言中添加控制結構功能, 能夠進行復雜的計算, 繼承所有用戶自定義類型,函數和操作符, 能夠定義被服務器信任(的語言), 容易使用。

用PL/pgSQL創建的函數可以用在內置函數用的任何地方,例如,可以創建復雜的計算函數,並之后用它們來定義操作符或者在索引表達式中使用它們。

(PL/pgSQL - SQL存儲過程語言:https://wiki.postgresql.org/wiki/9.1%E7%AC%AC%E4%B8%89%E5%8D%81%E4%B9%9D%E7%AB%A0)

 

示例1.

 1 --通過唯一ID,獲取數據表記錄
 2 CREATE OR REPLACE FUNCTION getRealUsers()
 3     RETURNS SETOF t_user
 4     LANGUAGE 'plpgsql'
 5 AS $BODY$
 6 declare
 7     mysql text;
 8     idx integer;
 9     
10     js_id text;
11     
12     user_info t_user;
13     
14     user_refcursor refcursor;  --多游標
15 begin
16     idx := 0;
17     mysql:='SELECT * FROM public.t_user ORDER BY js_id';
18     
19     open user_refcursor for execute mysql;  --打開游標
20     loop  --開始循環
21         fetch user_refcursor into user_info;  --將游標指定的值賦值給變量
22            
23         if found then  --處理邏輯
24             --raise notice 'delete_flag: %',user_info.delete_flag;
25             --raise notice 'js_id: %',user_info.js_id;
26             if idx = 0 then 
27                 js_id := user_info.js_id;
28                 if user_info.delete_flag = 'f' then 
29                     return next user_info;
30                 end if;
31             else 
32                 if js_id = user_info.js_id then
33                     if user_info.delete_flag = 'f' then 
34                         return next user_info;
35                     end if;
36                 else 
37                     js_id := user_info.js_id;
38                     if user_info.delete_flag = 'f' then 
39                         return next user_info;
40                     end if;
41                 end if;
42             end if;
43             idx := idx + 1;
44         else 
45             exit; 
46         end if; 
47     end loop;  --結束循環
48     close user_refcursor;  --關閉游標
49 end;
50 $BODY$;

 

示例2.

 1 --通過最新時間,獲取數據表記錄
 2 CREATE OR REPLACE FUNCTION getRealUsers()
 3     RETURNS SETOF t_user
 4     LANGUAGE 'plpgsql'
 5 AS $BODY$
 6 declare
 7     mysql text;  --SQL
 8     js_id text;  --ID
 9     user_info t_user;  --數據表對象
10     user_refcursor refcursor;  --多游標
11 begin
12     js_id := '';
13     mysql:='SELECT * FROM public.t_user ORDER BY js_id ASC, update_datetime DESC';
14     
15     open user_refcursor for execute mysql;  --打開游標
16     loop  -- Start
17         fetch user_refcursor into user_info;  --把游標的記錄設定到用戶情報
18         
19         if found then
20             --把最新的ID記錄留下
21             if js_id != user_info.js_id then
22                 js_id := user_info.js_id;
23                 return next user_info;
24             end if;
25         else 
26             exit; 
27         end if; 
28     end loop;  -- End
29     close user_refcursor;  --關閉游標
30     
31 end;
32 $BODY$;

 

調用:

1 -- select * from t_user
2 select * from getRealUsers()  --跟數據表一樣使用

 

End


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM