plpgsql

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

梦想与她 提交于 2019-12-01 07:09:15
问题 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

Trigger with dynamic field name

坚强是说给别人听的谎言 提交于 2019-12-01 06:43:15
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? You can implement that rather conveniently with the hstore operator #= : Make sure the additional module is installed properly ( once per database), in a schema that's included in your search_path : How to use % operator from the extension pg_trgm? Best way to install

Is it possible to dynamically loop through a table's columns?

天大地大妈咪最大 提交于 2019-12-01 06:19:31
I have a trigger function for a table test which has the following code snippet: IF TG_OP='UPDATE' THEN IF OLD.locked > 0 AND ( OLD.org_id <> NEW.org_id OR OLD.document_code <> NEW.document_code OR -- other columns ... ) THEN RAISE EXCEPTION 'Message'; -- more code So I am statically checking all the column's new value with its previous value to ensure integrity. Now every time my business logic changes and I have to add new columns into that table, I will have to modify this trigger each time. I thought it would be better if somehow I could dynamically check all the columns of that table,

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

北城余情 提交于 2019-12-01 05:31:46
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. 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 Well... Select position('#' in '2010-####-3434'); will give you the first. If you want the last, just run

EXECUTE of SELECT … INTO is not implemented

老子叫甜甜 提交于 2019-12-01 05:11:26
I am trying to run this function in PostrgeSQL: CREATE OR REPLACE FUNCTION create_partition_and_insert() RETURNS trigger AS $BODY$ DECLARE partition VARCHAR(25); _date text; BEGIN EXECUTE 'SELECT REPLACE(' || quote_literal(NEW.date) || ',''-'',''_'') into _date'; partition := TG_RELNAME || '_' || _date || ‘p’; IF NOT EXISTS(SELECT relname FROM pg_class WHERE relname=partition) THEN RAISE NOTICE 'A partition has been created %',partition; EXECUTE 'CREATE TABLE ' || partition || ' (check (date = ''' || NEW.date || ''')) INHERITS (' || TG_RELNAME || ');'; END IF; EXECUTE 'INSERT INTO ' ||

PostgreSQL RDS avoid hard coding the connection password when using dblink_connect()

烈酒焚心 提交于 2019-12-01 04:48:34
问题 I have an AWS RDS instance of PostgreSQL in which I need to execute an SQL statement within a function using dblink_connect(text) and dblink_exec(text) while logged in with the postgres role (that I created). CREATE OR REPLACE FUNCTION application.create_tenant_schemas(first integer, last integer) RETURNS void AS DECLARE tenant VARCHAR; sql VARCHAR; BEGIN FOR index IN first..last LOOP tenant := 'tenant_' || to_char(index, 'FM00000'); sql := 'CREATE SCHEMA ' || quote_ident(tenant); RAISE

PostgreSQL date difference

末鹿安然 提交于 2019-12-01 04:14:40
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 subtracted directly then difference is calculated. But in my case dates are present in variables as

Detecting column changes in a postgres update trigger

爷,独闯天下 提交于 2019-12-01 04:13:29
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; works just fine. I attach it to the table like so: CREATE TRIGGER document_update_body AFTER UPDATE ON

How to insert multiple rows using a function in PostgreSQL

不想你离开。 提交于 2019-12-01 03:56:45
问题 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

Is it possible to dynamically loop through a table's columns?

孤街醉人 提交于 2019-12-01 03:07:56
问题 I have a trigger function for a table test which has the following code snippet: IF TG_OP='UPDATE' THEN IF OLD.locked > 0 AND ( OLD.org_id <> NEW.org_id OR OLD.document_code <> NEW.document_code OR -- other columns ... ) THEN RAISE EXCEPTION 'Message'; -- more code So I am statically checking all the column's new value with its previous value to ensure integrity. Now every time my business logic changes and I have to add new columns into that table, I will have to modify this trigger each