plpgsql

PostgreSQl function return multiple dynamic result sets

我与影子孤独终老i 提交于 2021-01-28 06:26:27
问题 I have an old MSSQL procedure that needs to be ported to a PostgreSQL function. Basically the SQL procedure consist in a CURSOR over a select statement. For each cursor entity i have three select statements based on the current cursor output. FETCH NEXT FROM @cursor INTO @entityId WHILE @@FETCH_STATUS = 0 BEGIN SELECT * FROM table1 WHERE col1 = @entityId SELECT * FROM table2 WHERE col2 = @entityId SELECT * FROM table3 WHERE col3 = @entityId END The tables from the SELECT statements have

Join against the output of an array unnest without creating a temp table

余生长醉 提交于 2021-01-27 06:38:27
问题 I have a query in a UDF (shown below) which unnest() s an array of integers and joins against it, I have created a local temp table in my pgplsql UDF since I know this works. Is it possible to use unnest directly in a query to perform a join instead of having to create a temp table ? CREATE OR REPLACE FUNCTION search_posts( forum_id_ INTEGER, query_ CHARACTER VARYING, offset_ INTEGER DEFAULT NULL, limit_ INTEGER DEFAULT NULL, from_date_ TIMESTAMP WITHOUT TIME ZONE DEFAULT NULL, to_date_

Count the number of attributes that are NULL for a row

柔情痞子 提交于 2021-01-27 04:38:34
问题 I want to add a new column to a table to record the number of attributes whose value are null for each tuple (row). How can I use SQL to get the number? for example, if a tuple is like this: Name | Age | Sex -----+-----+----- Blice| 100 | null I want to update the tuple as this: Name | Age | Sex | nNULL -----+-----+-----+-------- Blice| 100 | null| 1 Also, because I'm writing a PL/pgSQL function and the table name is obtained from argument, I don't know the schema of a table beforehand. That

How to return the result of a postgresql function in c#? Console output empty

大憨熊 提交于 2021-01-19 22:27:32
问题 I have the problem that i have a function in postgresql that calculates two integer and should return the result to the c# (npgsql) conosle and i don't know where my mistake is, because the debugger doesn't say anything to me which is helpful. so first of all the code of c# and of the function. ... cmd.Parameters["x"].Value = 20; cmd.Parameters["y"].Value = 22; connection.Open(); if (connection.State == System.Data.ConnectionState.Open) { //Console.WriteLine(cmd.Parameters["x"].Value);

How to return the result of a postgresql function in c#? Console output empty

不羁的心 提交于 2021-01-19 22:27:15
问题 I have the problem that i have a function in postgresql that calculates two integer and should return the result to the c# (npgsql) conosle and i don't know where my mistake is, because the debugger doesn't say anything to me which is helpful. so first of all the code of c# and of the function. ... cmd.Parameters["x"].Value = 20; cmd.Parameters["y"].Value = 22; connection.Open(); if (connection.State == System.Data.ConnectionState.Open) { //Console.WriteLine(cmd.Parameters["x"].Value);

PL/PostgreSQL how to convert a variable into a table name

我是研究僧i 提交于 2021-01-07 02:52:28
问题 So I have a function in PostgreSQL that dynamically selects columns from a dynamic table. I got this solution from this post and it works great other than one thing. This is inside of a file that is connected to a Node server, and so the $1 and $2 in the second SELECT * FROM represent values passed from there. The issue right now is that I am getting a syntax error that I don't understand (I am newer to SQL so that may be why). $2 represents the name of the table to be selected from as a

PL/PostgreSQL how to convert a variable into a table name

心不动则不痛 提交于 2021-01-07 02:51:18
问题 So I have a function in PostgreSQL that dynamically selects columns from a dynamic table. I got this solution from this post and it works great other than one thing. This is inside of a file that is connected to a Node server, and so the $1 and $2 in the second SELECT * FROM represent values passed from there. The issue right now is that I am getting a syntax error that I don't understand (I am newer to SQL so that may be why). $2 represents the name of the table to be selected from as a

Returning set of rows from plpgsql function.

自古美人都是妖i 提交于 2021-01-02 08:10:13
问题 I would like to return table from plpgsql function. Here is my code. CREATE FUNCTION test() RETURNS my_table AS $BODY$DECLARE q4 my_table; BEGIN q4 := SELECT * FROM my_table; RETURN q4; END;$BODY$ LANGUAGE sql; I am getting following error: Error: ERROR: syntax error at or near "SELECT" LINE 5: q4 := SELECT * FROM my_table; I started from this questions/tutorials. https://dba.stackexchange.com/questions/35721/declare-variable-of-table-type-in-pl-pgsql && http://postgres.cz/wiki/PL/pgSQL_%28en

Returning set of rows from plpgsql function.

寵の児 提交于 2021-01-02 08:09:57
问题 I would like to return table from plpgsql function. Here is my code. CREATE FUNCTION test() RETURNS my_table AS $BODY$DECLARE q4 my_table; BEGIN q4 := SELECT * FROM my_table; RETURN q4; END;$BODY$ LANGUAGE sql; I am getting following error: Error: ERROR: syntax error at or near "SELECT" LINE 5: q4 := SELECT * FROM my_table; I started from this questions/tutorials. https://dba.stackexchange.com/questions/35721/declare-variable-of-table-type-in-pl-pgsql && http://postgres.cz/wiki/PL/pgSQL_%28en

Use a Postgres trigger to record the JSON of only the modified fields

我只是一个虾纸丫 提交于 2020-12-15 07:08:40
问题 Is there a way to get the JSON of the only modified fields? Now I use the following trigger but the entire line is printed in the changelog. Example tables: TABLE tbl_changelog ( tbl TEXT, op TEXT, new JSON, old JSON ); TABLE tbl_items ( f1 TEXT, f2 TEXT ); Trigger: CREATE OR REPLACE FUNCTION changelog_procedure() RETURNS trigger AS $$ BEGIN INSERT INTO tbl_changelog(tbl, op, new, old) VALUES (TG_TABLE_NAME, TG_OP, row_to_json(NEW), row_to_json(OLD)); RETURN NULL; END; $$ LANGUAGE plpgsql