PostgreSQL stored procedure returns result set

Source: Internet
Author: User
Tags postgresql

Recently because the work needs to write a login check function, words not much to say, paste code, because it is just a simple introduction to use, so the core judgment I have all removed.

The first requirement is to return only one user ID, so a simple returns integer can be completed.

CREATE OR REPLACE FUNCTION Validate_user () RETURNS integer  as $BODY $ DECLARE integer ; begin return i; End  ;

--Changed the requirements later, asked to return the value of a row in the table, so I used the cursor, but after the value of the tour cursor is too cumbersome, so modified again.

CREATE OR REPLACE FUNCTIONValidate_user ()RETURNSRefcursor as$BODY $Declareuser_list refcursor;begin OpenUser_list for Select *  fromTb_userwhereEmail=Usernamete andState=1;returnuser_list;End; $BODY $language plpgsql volatilecost -;

The following is the standard notation for Pg/sql

CREATE OR REPLACE FUNCTIONValidate_user ()RETURNSSetof Tb_user as$BODY $Declareuser_list Tb_user;begin  forUser_listinch Select *  fromTb_userwhereEmail=Usernamete andUserpwd=Passwordte andState=1Loopreturn Nextuser_list;EndLoop;End; $BODY $language plpgsql volatilecost -; 

There is also a direct select, which is not posted here, after the success of the decision to directly use the SELECT statement on the line, no return value.

We need to add a parameter back to the judging situation, so I use the out output here, is also more flexible. The output parameter prefix plus T is because the PSQL version variable I'm using is not conflicting.

--output parameter usage

CREATE OR REPLACE FUNCTIONValidate_user (Usernametetext, Passwordtetext, Out Tuseridint, Out Tpasswordtext, Out Tusernametext, Out Tcompidint, Out Troleint, Out Terrcodeint) as$BODY $begin SelectUserid,userpwd,username,compid,role,0  fromTb_userwhereEmail=Usernamete andState=1  into$3,$4,$5,$6,$7,$8;return;End; $BODY $language plpgsql volatilecost -;


PostgreSQL stored procedure returns result set

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.