plpgsql

How to run SELECT queries in PL/pgSQL IF statements

◇◆丶佛笑我妖孽 提交于 2020-01-04 06:10:31
问题 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

Insert using a function that returns two values per row

前提是你 提交于 2020-01-04 04:19:26
问题 This function: CREATE OR REPLACE FUNCTION fn_test1() RETURNS SETOF date AS $BODY$ declare i int; begin i:=0; while i<5 loop return next '2001-01-02'::date; i:=i+1; end loop; end $BODY$ LANGUAGE plpgsql VOLATILE COST 100 ROWS 1000; This table: CREATE TABLE teste1 ( teste1_id serial NOT NULL, num integer, fn_date date) An INSERT like this works just fine (inserting 5 rows): Insert into teste1(num,fn_date) select 1, fn_test1(); But if I want to have a function that returns two dates in a row,

How to call Postgres function returning SETOF record?

依然范特西╮ 提交于 2020-01-03 21:39:14
问题 I have written the following function: -- Gets stats for all markets CREATE OR REPLACE FUNCTION GetMarketStats ( ) RETURNS SETOF record AS $$ BEGIN SELECT 'R approved offer' AS Metric, SUM(CASE WHEN M.MarketName = 'A+' AND M.Term = 24 THEN LO.Amount ELSE 0 end) AS MarketAPlus24, SUM(CASE WHEN M.MarketName = 'A+' AND M.Term = 36 THEN LO.Amount ELSE 0 end) AS MarketAPlus36, SUM(CASE WHEN M.MarketName = 'A' AND M.Term = 24 THEN LO.Amount ELSE 0 end) AS MarketA24, SUM(CASE WHEN M.MarketName = 'A'

How to call Postgres function returning SETOF record?

家住魔仙堡 提交于 2020-01-03 21:33:09
问题 I have written the following function: -- Gets stats for all markets CREATE OR REPLACE FUNCTION GetMarketStats ( ) RETURNS SETOF record AS $$ BEGIN SELECT 'R approved offer' AS Metric, SUM(CASE WHEN M.MarketName = 'A+' AND M.Term = 24 THEN LO.Amount ELSE 0 end) AS MarketAPlus24, SUM(CASE WHEN M.MarketName = 'A+' AND M.Term = 36 THEN LO.Amount ELSE 0 end) AS MarketAPlus36, SUM(CASE WHEN M.MarketName = 'A' AND M.Term = 24 THEN LO.Amount ELSE 0 end) AS MarketA24, SUM(CASE WHEN M.MarketName = 'A'

How to return updated rows from function

蓝咒 提交于 2020-01-03 17:36:10
问题 i'm quite new to postgres. i want to create a function (like stored procedure) that updates multiple rows and selects affected rows. here is my statement: CREATE or replace FUNCTION set_val( _val character varying(100) ) --5 RETURNS setof "table_test" AS $body$ declare results "table_test"%rowtype; begin update "table_test" set "value" = $1 where "gender" = 'm' returning * into results; if not found then insert into "table_test"("value") values($1) returning * into results; end if; return

What is the proper syntax for PostgreSQL stored procedures (functions)?

风格不统一 提交于 2020-01-03 15:32:50
问题 I'm trying to write two types of stored procedures in PostgreSQL. From what I understand Postgre only has functions. I was wondering if someone can take a look at my code and offer pointers. Also, I'm am not familiar whether with the spacing/new lines of commands. The first function needs to take input from user and add it onto a table. Suppose we have a table name "Car" with attributes "model" and "year". Will this be a correct stored function to add a new car to the table? CREATE OR REPLACE

plv8 disadvantages or limitations?

一曲冷凌霜 提交于 2020-01-03 09:09:12
问题 I'm playing around with PLV8 to write trigger and stored procedures for PostgreSQL. So far I don't really see disadvantages compared to PLPGSQL. Especially if working with JSON it seems even smarter then PLPGSQL. Are there known disadvantages or limitations if using PLV8? Can PLV8 be a full replacement for PLPGSQL? It would be great if somebody could share his experience on this. 回答1: The advantages and disadvantages of PLV8 are same as advantages and disadvantages of PLPerl, PLPython and

Dynamically define returning row types based on a passed given table in plpgsql?

冷暖自知 提交于 2020-01-03 03:32:06
问题 I'm dynamically building a query in a plpgsql function that accepts a source table as an incoming variable. I want to return the results of the built SELECT statement, which performs aggregations on the given table, and returns results of that table. However, at the moment I'm getting the following error: ********** Error ********** ERROR: a column definition list is required for functions returning "record" SQL state: 42601 So it looks like I need to define column types of the record rows I

Passing an array to stored to postgres

天大地大妈咪最大 提交于 2020-01-03 03:20:08
问题 Like advised I am linking to my previous question: PL/pgSQL control structures for lists / arrays I want to pass a list of numbers and a list of characters into a Postgres stored procedure. After that I want to do something with those two arrays. For number list you already helped me. I used CREATE OR REPLACE FUNCTION my_proc(p_amount_list numeric[]) as input parameter, and when I called that procedure I used SELECT my_proc('{2,2,2}'); . Now I want to know how to pass a list of characters and

Using dynamic query + user defined datatype in Postgres

扶醉桌前 提交于 2020-01-02 10:26:11
问题 I need a function to normalize my input table features values. My features table has 9 columns out of which x1,x2...x6 are the input columns I need to scale. I'm able to do it by using a static query: create or replace function scale_function() returns void as $$ declare tav1 features%rowtype; rang1 features%rowtype; begin select avg(n),avg(x0),avg(x1),avg(x2),avg(x3),avg(x4),avg(x5),avg(x6),avg(y) into tav1 from features; select max(n)-min(n),max(x0)-min(x0),max(x1)-min(x1),max(x2)-min(x2)