plpgsql

Postgresql - INSERT RETURNING INTO ambiguous column reference

社会主义新天地 提交于 2019-12-01 14:20:52
Can someone politely explain this crazyness? INSERT INTO "dbo"."UserProfile" ("FirstName") VALUES('John') RETURNING "UserProfileId" INTO _UserProfileId; throws an ambiguous reference error, however this correctly executes: INSERT INTO "dbo"."UserProfile" ("FirstName") VALUES('John') RETURNING "dbo"."UserProfile"."UserProfileId" INTO _UserProfileId; _UserProfileId is a declared integer variable. I couldn't find any references to this syntax in the manual or why on earth this would be ambiguous in any way. Erwin Brandstetter IN and OUT parameters (including columns in RETURNS TABLE ) are visible

To ignore result in BEFORE TRIGGER of PostgreSQL?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-01 11:07:05
This thread is a part challenge of this thread to which I am searching a better solution for one part by BEFORE TRIGGER. I just want to launch a trigger to convert to correct brackets. I am thinking whether I should return from the trigger NULL or something else in before trigger. Code CREATE OR REPLACE FUNCTION insbef_events_function() RETURNS TRIGGER AS $func$ DECLARE m int[]; BEGIN FOREACH m SLICE 1 IN ARRAY TG_ARGV[0]::int[] LOOP INSERT INTO events (measurement_id, event_index_start, event_index_end) SELECT NEW.measurement_id, m[1], m[2]; -- Postgres array starts with 1 ! END LOOP; -- do

How to exclude PL/pgSQL functions in export?

只愿长相守 提交于 2019-12-01 09:49:14
问题 I use following command to dump some structures from server' database to be able to create sample of data on my local hard drive. pg_dump -h myserver.com -U product_user -s -f ./data/base.structure.postgresql.sql -F p -v -T public.* -T first_product.* -T second_product.* -T another_product.locales mydatabase I need to exclude some schemas otherwise it would ended up on permissions or other errors. Even that I exclude schema public, it dumps all functions in that schema, like this: REVOKE ALL

user defined type as input parameters in PostgreSQL function

雨燕双飞 提交于 2019-12-01 09:25:38
Hi I am creating a procedure for insertion of meta data. I created types and I included 1 type in another type and in procedure I am iterating it to get the value. Since I am new to PostgreSQL Can anyone help me on how to call the procedure. The input parameter is type Create Type Form_details as( formName character varying(100), submittedBy numeric, createdDate date, updatedBy numeric, updatedDate date, comments character varying(500), Sections Section[] ) create type Section as ( sectionName character varying(100), sectionLabel character varying(100), sectionOrder numeric ) and the procedure

How to return a value from a function if no value is found

馋奶兔 提交于 2019-12-01 08:36:26
I'm attempting to return 0.0 if the following function does not return anything: CREATE OR REPLACE FUNCTION get_height(firstn VARCHAR, lastn VARCHAR) RETURNS FLOAT AS $$ DECLARE height FLOAT = 0.0; BEGIN SELECT into height AVG(((p.h_feet * 12) + p.h_inches) * 2.54) FROM player p WHERE p.firstname = firstn AND p.lastname = lastn; RETURN height; END; $$ LANGUAGE plpgsql; I've tried searching for it and found that COALESCE does not work. Does anyone have any ideas how to solve this? Table structure: create table player( firstname text ,lastname text ,h_feet INT ,h_inches INT ); Example data:

Use of custom return types in a FOR loop in plpgsql

蓝咒 提交于 2019-12-01 08:02:07
The following code that I use returns an integer 1 : CREATE TYPE my_test AS ( foo Integer ); CREATE FUNCTION foo_out() RETURNS SETOF Integer AS $$ BEGIN RETURN QUERY SELECT 1 as foo; END $$ LANGUAGE plpgsql; CREATE FUNCTION foo1() RETURNS SETOF my_test AS $$ DECLARE x my_test; BEGIN FOR x IN SELECT foo_out() LOOP RETURN NEXT x; END LOOP; END; $$ LANGUAGE 'plpgsql'; select * from foo1(); But why does the same code return: ERROR: invalid input syntax for integer: (1) if I change the return type to: CREATE FUNCTION foo_out() RETURNS SETOF my_test Which also should be an integer !? It could be the

To ignore result in BEFORE TRIGGER of PostgreSQL?

依然范特西╮ 提交于 2019-12-01 07:44:29
问题 This thread is a part challenge of this thread to which I am searching a better solution for one part by BEFORE TRIGGER. I just want to launch a trigger to convert to correct brackets. I am thinking whether I should return from the trigger NULL or something else in before trigger. Code CREATE OR REPLACE FUNCTION insbef_events_function() RETURNS TRIGGER AS $func$ DECLARE m int[]; BEGIN FOREACH m SLICE 1 IN ARRAY TG_ARGV[0]::int[] LOOP INSERT INTO events (measurement_id, event_index_start,

How to insert multiple rows using a function in PostgreSQL

大城市里の小女人 提交于 2019-12-01 07:37:59
I want to insert more than one row in a table with function in PostgreSQL. This is my table CREATE TABLE mahasiswa ( nim CHAR(10), nama VACHAR(40) CONSTRAINT pk_nim PRIMARY KEY (nim) ) ; and this is the function I created CREATE FUNCTION insertdata(CHAR(10),varchar(40)) RETURNS VOID AS $$ INSERT INTO mahasiswa VALUES ($1,$2); $$ LANGUAGE 'sql'; When I call the function like this SELECT insertdata ('1234567890','Nahrun'), ('0987654321','Hartono'); only one row is inserted. How can I modify my function to insert more than one row at a time? Erwin Brandstetter The function you have should rather

Check if key exists in a JSON with PL/pgSQL?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-01 07:34:19
I'm trying to check if a key exists in a JSON sent as parameter in a PL/pgSQL function. Here is the function. CREATE FUNCTION sp_update_user(user_info json) RETURNS json LANGUAGE plpgsql AS $$ BEGIN PERFORM user_info->>'lastname'; IF FOUND THEN UPDATE users SET lastname = user_info->>'lastname' WHERE id = sp_update_user.user_id; END IF; PERFORM user_info->>'firstname'; IF FOUND THEN UPDATE users SET firstname = user_info->>'firstname' WHERE id = sp_update_user.user_id; END IF; RETURN row_to_json(row) FROM (SELECT true AS success) row; END;$$; I tried with the PERFORM -clause, but even if the

user defined type as input parameters in PostgreSQL function

你。 提交于 2019-12-01 07:19:47
问题 Hi I am creating a procedure for insertion of meta data. I created types and I included 1 type in another type and in procedure I am iterating it to get the value. Since I am new to PostgreSQL Can anyone help me on how to call the procedure. The input parameter is type Create Type Form_details as( formName character varying(100), submittedBy numeric, createdDate date, updatedBy numeric, updatedDate date, comments character varying(500), Sections Section[] ) create type Section as (