Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declare variable set = select

Tags:

This probably sounds like a really stupid question, but how do I declare a variable for used in a PostgreSQL 9.3 query?

CREATE or replace FUNCTION public.test()   returns int4 AS $BODY$ DECLARE         cod_process bigint :=30001;     cod_instance bigint ;     utc_log timestamp without time zone := localtimestamp;     cod_log_type varchar(100) :='information ';     txt_log_text varchar(100):= 'start process';     txt_log varchar(100):= txt_log_text||'_'||cod_process;     set cod_instance= select max(cod_instance) as cod_instance from public.instance where public.instance.cod_process=cod_process;     BEGIN       INSERT INTO public.log (cod_process, cod_instance, utc_log,cod_log_type,txt_log)     VALUES (cod_process, cod_instance, utc_log,cod_log_type,txt_log );     RETURN 11; END; $BODY$ LANGUAGE 'plpgsql'; 
ERROR: type "cod_instance" does not exist SQL state: 42704 Character: 383 
like image 701
ASA Avatar asked Dec 11 '13 11:12

ASA


People also ask

How do you set a variable in a SELECT query?

To assign a value to a variable, use the SET statement. This is the preferred method of assigning a value to a variable. A variable can also have a value assigned by being referenced in the select list of a SELECT statement.

Can you DECLARE a variable in a SELECT statement?

Variables in SQL procedures are defined by using the DECLARE statement. Values can be assigned to variables using the SET statement or the SELECT INTO statement or as a default value when the variable is declared. Literals, expressions, the result of a query, and special register values can be assigned to variables.

How do you DECLARE a variable in SQL?

Declaring a variable The DECLARE statement initializes a variable by assigning it a name and a data type. The variable name must start with the @ sign. In this example, the data type of the @model_year variable is SMALLINT . By default, when a variable is declared, its value is set to NULL .

What is SELECT variable?

SELECT @local_variable is typically used to return a single value into the variable. However, when expression is the name of a column, it can return multiple values. If the SELECT statement returns more than one value, the variable is assigned the last value that is returned.


1 Answers

You need to run the select using the into clause inside the actual code block, not in the declare block:

begin    select max(cod_instance)        into cod_instance    from public.instance     where public.instance.cod_process=cod_process;     .... end; 

It's usually not such a good idea to give variables (or parameters) the same name as columns in the table. There are certain cases where this can confuse the parser. To avoid any potential problems, try to use different names for your variables, e.g. by prefixing them (e.g. l_cod_process instead of cod_process or l_cod_instance instead of cod_instance)

More details on variable assignment can be found in the manual: http://www.postgresql.org/docs/current/static/plpgsql-statements.html

like image 112
a_horse_with_no_name Avatar answered Sep 30 '22 21:09

a_horse_with_no_name