postgresql 預存程序返回結果集

來源:互聯網
上載者:User

標籤:

最近因為工作需要寫了個登入校正函數,話不多說,貼代碼,因為只是一個簡單的介紹用法,所以核心判斷我就全部去掉了。

//最先要求是只返回一個使用者id,於是簡單的returns integer就可以完成。

 

CREATE OR REPLACE FUNCTION validate_user()RETURNS integer AS$BODY$DECLARE i integer;beginreturn i;end;$BODY$LANGUAGE plpgsql VOLATILECOST 100;

 

-- 後面更改了需求,要求返回表中一行的值,所以我就使用了遊標,不過後擷取遊標的值太過麻煩,所以又修改了一次。

CREATE OR REPLACE FUNCTION validate_user()RETURNS refcursor AS$BODY$declare user_list refcursor;begin open user_list for select * from tb_user where email = usernameTe and state=1;return user_list;end;$BODY$LANGUAGE plpgsql VOLATILECOST 100;

 

 

 //下面是標準的pg/sql的寫法 

 

CREATE OR REPLACE FUNCTION validate_user()RETURNS setof tb_user AS$BODY$declare user_list tb_user;begin for user_list in select * from tb_user where email = usernameTe and userpwd= passwordTe and state=1 loop return next user_list;end loop;end;$BODY$LANGUAGE plpgsql VOLATILECOST 100; 

 

//還有一種寫法是直接select,這裡就不貼出來了,判斷成功之後直接使用select語句就行,無需傳回值。 

 

//後面需要增加一個參數返回判斷情況,所以我這裡使用了out輸出,也是比較靈活。輸出參數首碼加T是因為我所使用的psql版本變數不可以衝突。

--output參數使用

CREATE OR REPLACE FUNCTION validate_user(usernamete text, passwordte text,out Tuserid int,out Tpassword text,out Tusername text,out Tcompid int,out Trole int,out Terrcode int)AS$BODY$begin select userid,userpwd,username,compid,role,0 from tb_user where email = usernamete and state=1 into $3,$4,$5,$6,$7,$8;return;end;$BODY$LANGUAGE plpgsql VOLATILECOST 100; 

 


 

postgresql 預存程序返回結果集

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.