plpgsql

Postgres function returning one record while I have many records?

元气小坏坏 提交于 2019-12-04 03:54:38
问题 I have many records which my simple query returning but when i use function it just gives me first record, firstly i create my own data type using, CREATE TYPE my_type (usr_id integer , name varchar(30)); and my function is, CREATE OR REPLACE function test() returns my_type as $$ declare rd varchar := '21'; declare personphone varchar := NULL; declare result my_type; declare SQL VARCHAR(300):=null; DECLARE radiophone_clause text = ''; BEGIN IF rd IS NOT NULL then radiophone_clause = 'and pp

Session based global variable in Postgresql stored procedure?

左心房为你撑大大i 提交于 2019-12-04 03:48:08
In Oracle's PL/SQL I can create a session based global variable with the package definition. With Postgresql's PLpg/SQL, it doesn't seem possible since there are no packages, only independent procedures and functions. Here is the syntax for PL/SQL to declare g_spool_key as a global... CREATE OR REPLACE PACKAGE tox IS g_spool_key spool.key%TYPE := NULL; TYPE t_spool IS REF CURSOR RETURN spool%ROWTYPE; PROCEDURE begin_spool; PROCEDURE into_spool ( in_txt IN spool.txt%TYPE ); PROCEDURE reset_spool; FUNCTION end_spool RETURN t_spool; FUNCTION timestamp RETURN VARCHAR2; END tox; How would I

Generate a random number of non duplicated random number in [0, 1001] through a loop

痴心易碎 提交于 2019-12-04 03:39:21
I need to generate a random number of non duplicated random number in plpgsql. The non duplicated number shall fall in the range of [1,1001]. However, the code generates number exceeding 1001. directed2number := trunc(Random()*7+1); counter := directed2number while counter > 0 loop to_point := trunc((random() * 1/directed2number - counter/directed2number + 1) * 1001 +1); ... ... counter := counter - 1; end loop; If I understand right You need a random number ( 1 to 8 ) of random numbers. The random numbers span 1 to 1001 . The random numbers need to be unique . None shall appear more than once

PostgreSQL date difference

吃可爱长大的小学妹 提交于 2019-12-04 00:40:14
问题 I have a PostgreSQL function which calculates date difference: CREATE OR REPLACE FUNCTION testDateDiff () RETURNS int AS $BODY$ DECLARE startDate TIMESTAMP; DECLARE endDate TIMESTAMP; DECLARE diffDatePart int ; BEGIN Select evt_start_date From events Where evt_id = 5 INTO startDate ; Select evt_start_date From events Where evt_id = 6 INTO endDate ; SELECT EXTRACT(day FROM TIMESTAMP startDate - endDate) INTO diffDatePart; RETURN diffDatePart; END; $BODY$ LANGUAGE plpgsql COST 100 If dates are

ERROR: must be owner of language plpgsql

橙三吉。 提交于 2019-12-03 22:16:22
I'm using PostgreSQL v9.0.1 with Rails (and it's deps) @ v2.3.8 , owing to the use of the fulltext capability of postgres, I have a table which is defined as: CREATE TABLE affiliate_products ( id integer NOT NULL, name character varying(255), model character varying(255), description text, price numeric(9,2), created_at timestamp without time zone, updated_at timestamp without time zone, textsearch_vector tsvector, ); Note the last line, this ensures that active record isn't able to process it with the standard schema dumper, so I have to set config.active_record.schema_format = :sql in .

How to use `RETURN NEXT`in PL/pgSQL correctly?

拈花ヽ惹草 提交于 2019-12-03 20:33:06
I am trying to write a loop using PL/pgSQL (PostgreSQL 9.3) function that returns a table. I used RETURN NEXT; with no parameters after each query in the loop, following examples found like plpgsql error "RETURN NEXT cannot have a parameter in function with OUT parameters" in table-returning function , and elsewhere. However, I am still getting an error that says: ERROR: query has no destination for result data HINT: If you want to discard the results of a SELECT, use PERFORM instead. A minimal code example to reproduce the problem is below. Can anyone please help explain how to fix the test

How to join table with dynamic identifier in postgres?

左心房为你撑大大i 提交于 2019-12-03 16:28:59
I have a table name table containing two columns foreign_table_name , and foreign_key . Is it possible to write a SELECT query that would JOIN values of this table and the table which name is specified in the column foreign_table_name ? For instance, if we know that all possible targetted foreign tables have a name field, I would like to know if I could write something that would: SELECT table.foo, table.bar, foreign_table.name FROM table JOIN $foreign_table AS foreign_table ON (foreign_table.id = table.foreign_key $foreign_table = table.foreign_table); Any solution using PlpgSQL is of course

PostgreSQL Exception Handling

我与影子孤独终老i 提交于 2019-12-03 14:28:00
问题 I am new to PostgreSQL. Could anybody please correct this query. BEGIN TRANSACTION; BEGIN; CREATE TABLE "Logs"."Events" ( EventId BIGSERIAL NOT NULL PRIMARY KEY, PrimaryKeyId bigint NOT NULL, EventDateTime date NOT NULL DEFAULT(now()), Action varchar(12) NOT NULL, UserId integer NOT NULL REFERENCES "Office"."Users"(UserId), PrincipalUserId varchar(50) NOT NULL DEFAULT(user) ); CREATE TABLE "Logs"."EventDetails" ( EventDetailId BIGSERIAL NOT NULL PRIMARY KEY, EventId bigint NOT NULL REFERENCES

PostgreSQL vs Oracle: “compile-time” checking of PL/pgSQL

只谈情不闲聊 提交于 2019-12-03 13:28:56
Executive summary: PostgreSQL is amazing, but we are facing many issues at work due to the fact that it postpones many checks on PL/pgSQL code until runtime. Is there a way to make it more like Oracle's PL/SQL in this respect ? For example... Try executing this in any Oracle DB: create function foo return number as begin select a from dual; return a; end; Oracle will immediately (i.e. at compile-time !) respond with: [Error] ORA-00904: invalid identifier Now try the semantically equivalent thing in PostgreSQL: CREATE OR REPLACE FUNCTION public.foo () RETURNS integer AS $body$ BEGIN select a;

How to execute PostgreSQL RAISE command dynamically

元气小坏坏 提交于 2019-12-03 12:33:19
How to raise error from PostgreSQL SQL statement if some condition is met? I tried code below but got error. CREATE OR REPLACE FUNCTION "exec"(text) RETURNS text AS $BODY$ BEGIN EXECUTE $1; RETURN $1; END; $BODY$ LANGUAGE plpgsql VOLATILE; -- ERROR: syntax error at or near "raise" -- LINE 1: raise 'test' SELECT exec('raise ''test'' ') WHERE TRUE In real application TRUE is replaced by some condition. Update I tried to extend answer to pass exception message parameters. Tried code below but got syntax error. How to pass message parameters ? CREATE OR REPLACE FUNCTION exec(text, variadic )