plpgsql

Table name as variable parameter

元气小坏坏 提交于 2019-12-19 12:49:11
问题 I made a function in pgadmin create or replace function get_source2(a text) returns integer as $$ declare a text; geom geometry; begin select get_source(geom) from a; end; $$ language plpgsql; I want input a by table name How can I do? I try to like this select get_source2('postgis.center') but I get: ERROR: relation "a" does not exist LINE 2: from a help me 回答1: try this: create or replace function get_source2(a text) returns integer as $$ declare geom geometry; begin execute 'select get

Postgresql - INSERT RETURNING INTO ambiguous column reference

£可爱£侵袭症+ 提交于 2019-12-19 11:55:09
问题 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

PostgreSQL plpgsql get current procedures oid

橙三吉。 提交于 2019-12-19 08:59:23
问题 Is it possible to get the current OID within a function? Like: CREATE FUNCTION foo() RETURNS numeric LANGUAGE plpgsql AS ' BEGIN return THIS_FUNCTIONS_OID; END '; I need this, because I created function foo within different schemas so the functions name is not helpful here. 回答1: I guess you are looking smth like return select oid from pg_proc where proname='$0'; I doubt you can get it as variable. You can get the name from current_query() , but it will be very not reliable... Unless you

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

一世执手 提交于 2019-12-19 08:15:15
问题 This question was migrated from Database Administrators Stack Exchange because it can be answered on Stack Overflow. Migrated 4 years ago . 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-

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

我只是一个虾纸丫 提交于 2019-12-19 08:14:10
问题 This question was migrated from Database Administrators Stack Exchange because it can be answered on Stack Overflow. Migrated 4 years ago . 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-

Trigger with dynamic field name

大兔子大兔子 提交于 2019-12-19 07:43:24
问题 I have a problem on creating PostgreSQL (9.3) trigger on update table. I want set new values in the loop as EXECUTE 'NEW.'|| fieldName || ':=''some prepend data'' || NEW.' || fieldName || ';'; where fieldName is set dynamically. But this string raise error ERROR: syntax error at or near "NEW" How do I go about achieving that? 回答1: You can implement that rather conveniently with the hstore operator #= : Make sure the additional module is installed properly ( once per database), in a schema

Trigger with dynamic field name

可紊 提交于 2019-12-19 07:41:51
问题 I have a problem on creating PostgreSQL (9.3) trigger on update table. I want set new values in the loop as EXECUTE 'NEW.'|| fieldName || ':=''some prepend data'' || NEW.' || fieldName || ';'; where fieldName is set dynamically. But this string raise error ERROR: syntax error at or near "NEW" How do I go about achieving that? 回答1: You can implement that rather conveniently with the hstore operator #= : Make sure the additional module is installed properly ( once per database), in a schema

Trigger with dynamic field name

♀尐吖头ヾ 提交于 2019-12-19 07:41:08
问题 I have a problem on creating PostgreSQL (9.3) trigger on update table. I want set new values in the loop as EXECUTE 'NEW.'|| fieldName || ':=''some prepend data'' || NEW.' || fieldName || ';'; where fieldName is set dynamically. But this string raise error ERROR: syntax error at or near "NEW" How do I go about achieving that? 回答1: You can implement that rather conveniently with the hstore operator #= : Make sure the additional module is installed properly ( once per database), in a schema

How to find the first and last occurrences of a specific character inside a string in PostgreSQL

杀马特。学长 韩版系。学妹 提交于 2019-12-19 07:09:10
问题 I want to find the first and the last occurrences of a specific character inside a string. As an example, consider a string named "2010-####-3434", and suppose the character to be searched for is "#". The first occurrence of hash inside the string is at 6-th position, and the last occurrence is at 9-th position. 回答1: I do not know how to do that, but the regular expression functions like regexp_matches, regexp_replace, regexp_split_to_array may be an alternative route to soling your problem

Detecting column changes in a postgres update trigger

◇◆丶佛笑我妖孽 提交于 2019-12-19 06:08:07
问题 I have a postgres database with several tables that I want to watch for updates on, and if there's any updates, I want to fire a "hey, something changed" update. This works in the basic case, but now it's time to improve things. CREATE FUNCTION notify_update() RETURNS trigger AS $notifyfunction$ BEGIN PERFORM pg_notify('update_watchers', $${"event":"update", "type": "$$ || TG_TABLE_NAME || $$", "payload": {"id": $$ || new.id || $$}}$$); RETURN new; END; $notifyfunction$ LANGUAGE plpgsql;