plpgsql

Inserting DEFAULT value into a column when a parameter is NULL

拥有回忆 提交于 2019-12-08 19:46:26
问题 I would like to write a stored procedure like this: CREATE OR REPLACE FUNCTION my_function(param_1 text, param_2 text DEFAULT NULL::text) RETURNS bigint AS $$ DECLARE ret bigint; BEGIN INSERT INTO my_table(val_1, val_2) VALUES (param_1, param_2); -- do some more stuff RETURN ret; END; $$ LANGUAGE plpgsql; However, I would like to use val_2 column's DEFAULT value instead of NULL - if NULL is provided as the param_2 value. Something like this: INSERT INTO my_table(val_1, val_2) VALUES (param_1,

PL/pgSQL SELECT into an array

让人想犯罪 __ 提交于 2019-12-08 17:36:17
问题 Here's my function declaration and part of the body: CREATE OR REPLACE FUNCTION access_update() RETURNS void AS $$ DECLARE team_ids bigint[]; BEGIN SELECT INTO team_ids "team_id" FROM "tmp_team_list"; UPDATE "team_prsnl" SET "updt_dt_tm" = NOW(), "last_access_dt_tm" = NOW() WHERE "team_id" IN team_ids; END; $$ LANGUAGE plpgsql; I want team_ids to be an array of ints that I can then use in the UPDATE statement. This function give me errors like this: psql:functions.sql:62: ERROR: syntax error

Postgres return null values on function error/failure when casting

不羁岁月 提交于 2019-12-08 17:08:00
问题 I am attempting to convert text values to timestamp values. For the following table called a : id | c1 ----+-------------------- 1 | 03-03-2000 2 | 01-01-2000 3 | 12/4/1990 4 | 12 Sept 2011 5 | 12-1-1999 12:33:12 6 | 24-04-89 2:33 am I am attempting to perform a select with a cast as follows: select id, c1, c1::timestamp as c2 from a; This works correctly if there were only the first 5 rows, but for the 6th row where c1 is 24-04-89 2:33 am it throws the following error: ERROR: date/time field

How do I write a postgres stored procedure that doesn't return anything?

£可爱£侵袭症+ 提交于 2019-12-08 16:22:37
问题 How do I write a simple stored procedure in postgres that doesn't return a value at all? Even with a void return type when I call the stored procedure I get a single row back. CREATE FUNCTION somefunc(in_id bigint) RETURNS void AS $$ BEGIN DELETE from test_table where id = in_id; END; $$ LANGUAGE plpgsql; 回答1: It's not the function that returns value, it's the SELECT you used to call it. If it doesn't return any rows, it doesn't run your function. 回答2: You can achieve "nothing returned" by

PL/pgSQL for all-in-one dynamic query

ε祈祈猫儿з 提交于 2019-12-08 13:17:17
问题 I am using PostigreSQL10. I have an all-in-one query where a user can pick a non-standard combination of category style, event, area to search for a constructions. Keep in mind that category style, event, area are in different tables. I want to avoid the multiple IF s and JOIN s. I also want to avoid the query planner caching the wrong plan for the wrong parameter combination and the sequel recompilation of the query every single time. So I have to use dynamic SQL. To get dynamic SQL in

PL/Perl send mail in Postgresql [duplicate]

吃可爱长大的小学妹 提交于 2019-12-08 12:46:43
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: psql trigger send email I using PL/Perl language to send mail in Postgresql. When dataset table upload_status change to published, and it will send mail to the author's email address in dataset table. And in letter contains some records in this author in dataset. The dataset PK is identifier. like send from xx@mail.com to (dataset.email)@mail.com Dear Dr. (dataset.author) your...... (dataset.product) have

Porting Oracle procedure to PostgreSQL (exception codes from orafce for utl_file module)

若如初见. 提交于 2019-12-08 12:22:50
问题 I am in the middle of a database migration from Oracle to PostgreSQL. We are using ora2pg to make the conversion the plus automatic possible and the orafce plugin for PostgreSQL for functions compatibility. We have just started and a lot of work is to be done. Just now I am with stored procedures (output from ora2pg script) and after resolving the different syntax errors I does not know how to solve the last one. In particular the problematic code is the following: select utl_file.put_line

PLpgsql BEFORE UPDATE with select from same table

限于喜欢 提交于 2019-12-08 09:16:25
问题 I'm trying to create a PL/PGSQL trigger function to check a new row's date range to ensure there are no other rows within the table for which the date ranges overlap (for the same product_id) . I have successully created the function and set it as a BEFORE INSERT trigger, but I'm trying to figure out how to also set it as a BEFORE UPDATE trigger, since the SELECT statement inside the trigger is certain to throw an exception because it fits the criteria of overlapping the date of an updated

PL/PgSQL: No function matches the given name and argument types. You might need to add explicit type casts

假如想象 提交于 2019-12-08 08:32:56
问题 I am trying to write a dateadd() function using PL/PgSQL. I want to be able to add anything from seconds, right up to years to a date/timestamp. have cobbled together a function (from snippets etc obtained online), and have come up with this "implementation": CREATE OR REPLACE FUNCTION dateadd(diffType VARCHAR(15), incrementValue int, inputDate timestamp) RETURNS timestamp AS $$ DECLARE YEAR_CONST Char(15) := 'year'; MONTH_CONST Char(15) := 'month'; DAY_CONST Char(15) := 'day'; HOUR_CONST

SELECT dynamic columns without functions in PostgreSQL

走远了吗. 提交于 2019-12-08 07:08:56
问题 I need to select rows from two and more tables ("A", "B"). They have differences columns and I don't use inheritance for it. So. For example: SELECT * FROM "A" UNION SELECT * FROM "B" ERROR: each UNION query must have the same number of columns I can understand why. I try get intersected columns from root schema in root table: SELECT column_name FROM information_schema.columns WHERE table_schema = 'client_root' AND table_name ='conditions' It's ok! But I don't use query: SELECT (SELECT column