plpgsql

Loop in function does not work as expected

六月ゝ 毕业季﹏ 提交于 2020-01-05 21:05:14
问题 Using PostgreSQL 9.0.4 Below is a very similar structure of my table: CREATE TABLE departamento ( id bigserial NOT NULL, master_fk bigint, nome character varying(100) NOT NULL CONSTRAINT departamento_pkey PRIMARY KEY (id), CONSTRAINT departamento_master_fk_fkey FOREIGN KEY (master_fk) REFERENCES departamento (id) MATCH SIMPLE ON UPDATE NO ACTION ON DELETE NO ACTION ) And the function I created: CREATE OR REPLACE FUNCTION fn_retornar_dptos_ate_raiz(bigint[]) RETURNS bigint[] AS $BODY$ DECLARE

if-statement with string containing the condition

 ̄綄美尐妖づ 提交于 2020-01-05 14:14:12
问题 This question is about Postgresql 8.3 . I've got a table with a field containing conditions like 'lastcontact is null'. In code, I want to loop through this table and for each record, I want to check 'if condition then', like in this example: FOR myrec IN SELECT * FROM tabel ORDER BY colorlevel, volgnummer LOOP if (myrec.conditie) then raise notice 'Condition % is true', myrec.conditie; else raise notice 'Condition % is false', myrec.conditie; end if; END LOOP; The table which I have called

if-statement with string containing the condition

空扰寡人 提交于 2020-01-05 14:13:30
问题 This question is about Postgresql 8.3 . I've got a table with a field containing conditions like 'lastcontact is null'. In code, I want to loop through this table and for each record, I want to check 'if condition then', like in this example: FOR myrec IN SELECT * FROM tabel ORDER BY colorlevel, volgnummer LOOP if (myrec.conditie) then raise notice 'Condition % is true', myrec.conditie; else raise notice 'Condition % is false', myrec.conditie; end if; END LOOP; The table which I have called

SQL Query where I get most recent rows from timestamp from another table

北城以北 提交于 2020-01-05 09:14:12
问题 I've got some sensory information going into a table. I have figured out the query that will tell me exactly when the value at a particular device changes. What I need to know is the status of all of the other sensors at that time. The trick is, the timestamps won't be equal. I could get a data point from sensor 1, then 3 minute later, one from sensor 2, and then 30 seconds later, another from sensor 1. So, here is an example of what I am talking about: --- data_table --- sensor | state |

Dynamically access column value in record

只谈情不闲聊 提交于 2020-01-05 02:48:19
问题 Is it possible to dynamically access a column value from a record by its name? I'm writing a trigger function that executes a dynamic SQL command and I would like to dynamically extract a column value from a NEW record by column name. Here's a simplified example of what I'm trying to do: $$ DECLARE command text := 'UPDATE $1 SET $2 = $3'; myColumn := 'votes' BEGIN EXECUTE command using 'anotherTable', myColumn, NEW.myColumn; END $$ 回答1: That's possible , but the USING clause of EXECUTE can

SELECT multiple rows and columns into a record variable

Deadly 提交于 2020-01-04 12:22:40
问题 In a plpgsql function, how can multiple rows and columns be selected into a record variable? For example, I would like to SELECT multiple instances of two columns ( yearinteger and value ) into a record variable ( yearvalues ). *EDIT - the following code is just part of a longer function, I need the variable yearvalues to contain multiple rows and columns from a table from which I can create further variables from CREATE OR REPLACE FUNCTION fn_function () RETURNS TABLE () AS $$ DECLARE year c

SELECT multiple rows and columns into a record variable

百般思念 提交于 2020-01-04 12:22:17
问题 In a plpgsql function, how can multiple rows and columns be selected into a record variable? For example, I would like to SELECT multiple instances of two columns ( yearinteger and value ) into a record variable ( yearvalues ). *EDIT - the following code is just part of a longer function, I need the variable yearvalues to contain multiple rows and columns from a table from which I can create further variables from CREATE OR REPLACE FUNCTION fn_function () RETURNS TABLE () AS $$ DECLARE year c

Populate multidimensional array

谁说胖子不能爱 提交于 2020-01-04 06:45:41
问题 I'm trying populate a multidimensional array on PostgreSQL, but it not work. Below my code: CREATE OR REPLACE FUNCTION teste() RETURNS void AS $BODY$ DECLARE tarifas NUMERIC[7][24]; a INTEGER; b INTEGER; BEGIN FOR a IN 0..6 LOOP RAISE NOTICE 'TESTE TESTE %', a; FOR b IN 0..23 LOOP RAISE NOTICE 'tarifas[%][%] = 0;', a, b; tarifas[a][b] = 0; END LOOP; END LOOP; END $BODY$ LANGUAGE plpgsql VOLATILE; 回答1: Postgres has a dedicated function for that purpose exactly: array_fill(): returns an array

Populate multidimensional array

烈酒焚心 提交于 2020-01-04 06:45:24
问题 I'm trying populate a multidimensional array on PostgreSQL, but it not work. Below my code: CREATE OR REPLACE FUNCTION teste() RETURNS void AS $BODY$ DECLARE tarifas NUMERIC[7][24]; a INTEGER; b INTEGER; BEGIN FOR a IN 0..6 LOOP RAISE NOTICE 'TESTE TESTE %', a; FOR b IN 0..23 LOOP RAISE NOTICE 'tarifas[%][%] = 0;', a, b; tarifas[a][b] = 0; END LOOP; END LOOP; END $BODY$ LANGUAGE plpgsql VOLATILE; 回答1: Postgres has a dedicated function for that purpose exactly: array_fill(): returns an array

How to run SELECT queries in PL/pgSQL IF statements

对着背影说爱祢 提交于 2020-01-04 06:10:48
问题 I am trying to run SELECT queries in PL/pgSQL IF statements using the code below: DO $do$ DECLARE query_type real; arr real[] := array[1]; BEGIN IF query_type = 1 THEN RETURN QUERY SELECT "Westminster".* FROM "Westminster" WHERE ("Westminster".intersects = false AND "Westminster".area <= 100); ELSE IF query_type = 0 THEN RETURN QUERY SELECT "Westminster".* FROM "Westminster"; END IF; END $do$ However I get the following error, ERROR: cannot use RETURN QUERY in a non-SETOF function . Does