3

Trying to get into PostgreSQL. I'm drawing an function that will check credentials

Here is it:

CREATE or REPLACE  FUNCTION CkeckUser (Login varchar(50), Password varchar(50)) 
RETURNS TABLE(ID int, IDParent int, Text varchar(50), Image bytea, DLLName varchar(50), MethodName varchar(50), Parameters varchar(250) )
 AS $$
DECLARE
  idu INT;
BEGIN

idu := null;

 select idu = u."ID"
 FROM  "Users" u
 where (u."Login" = $1) and (u."Password" = $2);

  select 
utm."IDMenuItem" as ID,
utm."IDParentMenuItem" as IDParent,
m."Text",
m."Image",
m."DLLName",
m."MethodName",
m."Parameters"
    from        "Users" u
            join "UserTypes" ut on u."Type" = ut."ID"
            join "UserTypeMenu" utm on u."Type" = utm."IDUserType"
            join "Menu" m on utm."IDMenuItem" = m."ID";
where u."ID" = IDU;
order by m."ID";

END; $$
 LANGUAGE 'plpgsql';

Logics is pretty simple but on executing it

 select CkeckUser(N'admin', N'test');

or

  select * from CkeckUser(N'admin', N'test');

I got

  ERROR:  query has no destination for result data

where I'm wrong?

1
  • found!!! before last select statement should be added RETURN QUERY Commented Feb 17, 2012 at 15:44

1 Answer 1

9

In PL/pgSQL, you can't just run a query; you have to put the results somewhere. I take it that you want the function to return the query result?

You can put RETURN QUERY before the query, which will append its result-set to the function's return-value, but in your case, if that's the entirety of your stored procedure, it's probably easier to just change your stored-procedure to be pure SQL rather than PL/pgSQL:

CREATE or REPLACE FUNCTION CkeckUser (Login varchar(50), Password varchar(50)) 
RETURNS TABLE(ID int, IDParent int, Text varchar(50), Image bytea, DLLName varchar(50), MethodName varchar(50), Parameters varchar(250) )
  AS
  $$
    SELECT utm."IDMenuItem" AS ID,
           utm."IDParentMenuItem" as IDParent,
           m."Text",
           m."Image",
           m."DLLName",
           m."MethodName",
           m."Parameters"
      FROM "Users" u
      JOIN "UserTypes" ut
        ON u."Type" = ut."ID"
      JOIN "UserTypeMenu" utm
        ON u."Type" = utm."IDUserType"
      JOIN "Menu" m
        ON utm."IDMenuItem" = m."ID"
     WHERE u."ID" = ( SELECT u."ID"
                        FROM "Users" u
                       WHERE u."Login" = $1
                         AND u."Password" = $2
                    )
     ORDER
        BY m."ID"
  $$
  LANGUAGE 'SQL'
;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.