plpgsql

Declare and return a custom type in PostgreSQL function

不问归期 提交于 2019-12-10 19:13:02
问题 I found this article: http://wiki.postgresql.org/wiki/Return_more_than_one_row_of_data_from_PL/pgSQL_functions and I'm trying to use it as an example for my function. I am selecting different columns from different tables, and trying to return a set of records. Here's my code: CREATE OR REPLACE FUNCTION get_details_for_widget(widgetid integer) RETURNS SETOF widgetdetails AS $BODY$ DECLARE rec widgetdetails %rowtype; BEGIN FOR rec IN ( SELECT widget_details.id, widget_details.contact_id,

Check if trigger exists

ⅰ亾dé卋堺 提交于 2019-12-10 18:37:02
问题 I have the following query to triggers on all tables in schema public: SELECT 'CREATE TRIGGER ' || tab_name|| '_if_modified_trg INSERT OR UPDATE OR DELETE ON ' || tab_name|| ' FOR EACH ROW EXECUTE PROCEDURE audit.if_modified_func(); ' AS trigger_creation_query FROM ( SELECT quote_ident(table_schema) || '.' || quote_ident(table_name) as tab_name FROM information_schema.tables WHERE table_schema='public' ) AS foo; And I know how to check if a trigger exists: SELECT tgname from pg_trigger where

How to return multiple rows from PL/pgSQL function?

馋奶兔 提交于 2019-12-10 17:29:24
问题 I have spent good amount of time trying to figure it out and I haven't been able to resolve it. So, I need your help please. I am trying to write a PL/pgSQL function that returns multiple rows. The function I wrote is shown below. But it is not working. CREATE OR REPLACE FUNCTION get_object_fields() RETURNS SETOF RECORD AS $$ DECLARE result_record keyMetrics; BEGIN return QUERY SELECT department_id into result_record.visits from fact_department_daily where report_date='2013-06-07'; --return

How to pass a record as parameter for PL/pgSQL function?

一个人想着一个人 提交于 2019-12-10 17:07:25
问题 I keep looking for this answer online but I cannot find it. I am trying to pass one record over a PL/pgSQL function. I tried it in two ways. Fist way : CREATE OR REPLACE FUNCTION translateToReadableDate(mRecord dim_date%ROWTYPE) RETURNS void AS $$ That is the ouput : psql:requestExample.sql:21: ERROR: syntax error at or near "%" LINE 1: ... FUNCTION translateToReadableDate(mRecord dim_date%ROWTYPE) ... ^ Second way : CREATE OR REPLACE FUNCTION translateToReadableDate(mRecord RECORD) RETURNS

PostgreSQL error: query string argument of EXECUTE is null

孤者浪人 提交于 2019-12-10 15:48:06
问题 I have a table called evidence with a trigger which calls a stored procedure which basically does table partitioning by month. However I get an obscure error when I start inserting lots of rows under load: Npgsql.NpgsqlException: query string argument of EXECUTE is null Severity: ERROR Code: 22004 at Npgsql.NpgsqlState.<ProcessBackendResponses_Ver_3>d__a.MoveNext() in c:\C#Apps\github.npgsql.Npgsql.stock\src\Npgsql\NpgsqlState.cs:line890 at Npgsql.ForwardsOnlyDataReader.GetNextResponseObject(

Return multiple columns and rows from a function PostgreSQL instead of record

余生颓废 提交于 2019-12-10 15:17:57
问题 I was reading online about function on PostgreSQL and returns results In this links: SQL function return-type: TABLE vs SETOF records How do I reference named parameters in Postgres sql functions? http://www.postgresqltutorial.com/plpgsql-function-returns-a-table/ I have written this Function: create or replace function brand_hierarchy(account_value int) RETURNS table (topID INTEGER, accountId INTEGER, liveRowCount bigint,archiveRowCount bigint) AS $BODY$ SELECT * FROM my_client_numbers where

Psycopg2 callproc and sql parameters

拟墨画扇 提交于 2019-12-10 14:57:55
问题 I got some SQL function CREATE OR REPLACE FUNCTION tools.update_company(IN company_id integer, OUT value integer) RETURNS integer AS $BODY$ BEGIN select * into value from function_making_int(company_id) END;$BODY$ and from Psycopg2 (its inside Django if that matters) I do c = connection.cursor() c.callproc('tools.update_company', [1, ]) but function returns exactly the same input sequence as I gave, ignoring results and OUT parameter. Change to IN OUT and passing some foo value changes

Postgres creating a local temp table (on commit drop) from a dynamic sql string

橙三吉。 提交于 2019-12-10 14:12:24
问题 I have a query string generated in a postgres UDF, i'd like to put it's result in a temp table to perform joins against (I'm using LIMIT and OFFSET , and I don't want to join against other ttables only to end up choping the data off at the end --i.e., the LIMIT operator in the query plan). I attempt to create the temp table with the following statement. CREATE LOCAL TEMP TABLE query_result ON COMMIT DROP AS EXECUTE query_string_; But I get the following error notice : ********** Error *******

Postgres trigger notify with PHP

蹲街弑〆低调 提交于 2019-12-10 12:35:59
问题 I have a problem or misunderstanding with Postgre trigger -> perform notify -> capture into PHP flow. My Platform is PHP(5.6) in centos with Postgres. I have to add trigger with notifications table and whenever a new notification is added to that notifications SMS has to send to that user. So here added trigger like this CREATE FUNCTION xxx_sms_trigger() RETURNS trigger LANGUAGE plpgsql AS $$ DECLARE BEGIN PERFORM pg_notify('sms', NEW.id||'' ); RETURN new; END; and in php the inserting new

PostgreSQL Syntax error in PGAdmin

穿精又带淫゛_ 提交于 2019-12-10 04:10:29
问题 I am new to PostgreSQL and am using the query tool in PGAdmin. I'm trying to run pgsql queries that use variables, but I can't seem to get the syntax right. Here's a sample query that gives a syntax error: DECLARE num INTEGER; BEGIN num := 3; PRINT num; END; Update: Ok, let me try and explain. I come from a SQL server background. In the management studio, I can open a query window and play with (T)-SQL queries. For example, I can write something like this: DECLARE @num INT SET @num = 3 SELECT