Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass a SELECT result as an argument to postgreSQL function

Tags:

I have a table "UserState" with following fields: id, userid, ctime, state, endtime. I have a simple query:

SELECT userid FROM "UserState" WHERE ctime>'2014-07-14'::timestamp 

AND I have a plpgsql function, which must take the result of this query as an argument:

get_timeinstate(SELECT userid FROM "UserState" WHERE ctime>'2014-07-14'::timestamp); 

How to create function correctly to pass a query result as parametr there? It's necessery to understand, that the function returns another SQL result and I need to use there "IN" condition:

$func$ BEGIN  RETURN QUERY  SELECT  ...myanotherquery...  WHERE "UserState".userid IN (HERE I NEED TO INSERT MY QUERY RESULT) END; $func$ 
like image 657
user3824666 Avatar asked Jul 22 '14 08:07

user3824666


People also ask

How do you pass parameters in PostgreSQL query?

The get_sum() function accepts two parameters: a, and b, and returns a numeric. The data types of the two parameters are NUMERIC. By default, the parameter's type of any parameter in PostgreSQL is IN parameter. You can pass the IN parameters to the function but you cannot get them back as a part of the result.

How do you assign a selected value to a variable in PostgreSQL?

In PostgreSQL, the select into statement to select data from the database and assign it to a variable. Syntax: select select_list into variable_name from table_expression; In this syntax, one can place the variable after the into keyword.

How do I return a query in PostgreSQL?

To return a table from the function, you use RETURNS TABLE syntax and specify the columns of the table. Each column is separated by a comma (, ). In the function, we return a query that is a result of a SELECT statement.

What is $$ in Postgres?

In PostgreSQL, the dollar-quoted string constants ($$) is used in user-defined functions and stored procedures. In PostgreSQL, you use single quotes for a string constant like this: select 'String constant'; When a string constant contains a single quote ('), you need to escape it by doubling up the single quote.


2 Answers

Pass the returned user_id set as array. Create the function to accept an integer array

create function get_timeinstate (     user_id_set integer[],     another_param... 

Then call it passing the array generated by array_agg

get_timeinstate(     (         select array_agg(userid)         from "UserState"         where ctime>'2014-07-14'::timestamp     ),     another_param ); 

Inside the function:

where "UserState".userid = any (user_id_set) 

BTW if you are using plpgsql you can place the query inside the function and pass just the date:

create function get_timeinstate (     p_ctime timestamp,     another_param... $func$ declare     user_id_set integer[] := (         select array_agg(userid)         from "UserState"         where ctime > p_ctime     ); begin     return query     select      ...myanotherquery...     where "UserState".userid = any (user_id_set) end; $func$ 
like image 93
Clodoaldo Neto Avatar answered Oct 06 '22 00:10

Clodoaldo Neto


Just enclose in round brackets:

get_timeinstate(   (     SELECT userid FROM "UserState" WHERE ctime>'2014-07-14'::timestamp   ) ); 
like image 22
MrR Avatar answered Oct 06 '22 01:10

MrR