plpgsql

Insert PostGIS Object (e.g. ST_GeomFromText) from row variables in plpgsql script

故事扮演 提交于 2019-12-11 00:54:00
问题 I have two tables src_pos and dest_pos. The src_pos stores positions with longitude, latitude and altitude, while the dest_pos stores PosGIS Geometry object. Now I want to move a bunch of data from src_pos to dest_pos with following plpgsql script. But it failed, because row vaiable (e.g. row_data.longitude) cannot be interpreted correctly. How can I overcome this problem!? --create language 'plpgsql'; drop function createPosition(); create function createPosition() returns integer AS $$

Return dynamic set of columns

▼魔方 西西 提交于 2019-12-10 23:59:13
问题 I have created the following function to return set of columns based on parameters of that function: CREATE OR REPLACE FUNCTION getColumns(IN _column1 text, IN _column2 text, IN _column3 text, IN _column4 text, IN _table text) RETURNS TABLE(cmf1 text, cmf2 text, cmf3 text, cmf4 text) AS $BODY$ BEGIN RETURN QUERY EXECUTE 'SELECT ' || case when _column1 = 'None' then quote_literal('None') else quote_ident(_column1) end || '::text as cmf1,' || case when _column2 = 'None' then quote_literal('None

Demonstrate SQL injection in PL/pgSQL

ε祈祈猫儿з 提交于 2019-12-10 23:49:35
问题 I have this function in plpgsql: CREATE OR REPLACE function login_v(em varchar, passwd varchar) RETURNS users AS $$ DECLARE cu users; BEGIN SELECT * into cu FROM users where email = em AND encrypted_password = crypt(passwd, encrypted_password); return cu; END $$ LANGUAGE plpgsql; When I provide an input like this: select login_v('test@test.com'' OR 1=1;--','la la la'); , I think my method should return the user with email test@test.com . What Am I doing wrong? Performing SQL injection is

Passing table names in an array

杀马特。学长 韩版系。学妹 提交于 2019-12-10 23:17:24
问题 I need to do the same deletion or purge operation (based on several conditions) on a set of tables. For that I am trying to pass the table names in an array to a function. I am not sure if I am doing it right. Or is there a better way? I am pasting just a sample example this is not the real function I have written but the basic is same as below: CREATE OR REPLACE FUNCTION test (tablename text[]) RETURNS int AS $func$ BEGIN execute 'delete * from '||tablename; RETURN 1; END $func$ LANGUAGE

Creating user with password from variables in anonymous block

落花浮王杯 提交于 2019-12-10 22:42:06
问题 I want to create a script that will have variables of _user and _pass to create the user in the Postgres database only if such login does not exist yet. I was thinking this would work, but i cant tell what is the issue: DO $DO$ DECLARE _user TEXT := 'myuser'; _pass TEXT := 'user!pass'; BEGIN IF NOT EXISTS ( SELECT 1 FROM pg_catalog.pg_roles WHERE rolname = _user) THEN RAISE NOTICE 'Creating user % ...',_user; CREATE USER _user WITH LOGIN NOSUPERUSER CREATEDB CREATEROLE NOREPLICATION PASSWORD

Easy way to have return type be SETOF table plus additional fields?

不羁的心 提交于 2019-12-10 22:32:00
问题 I'm writing a PL/pgSQL stored procedure that will return a set of records; each record contains all the fields of an existing table (call it Retailer, which has two fields: retailer_key and retailer_name). This, of course, works: CREATE FUNCTION proc_Find_retailers (IN p_Store_key INT) RETURNS SETOF Retailer AS $$ ...` Now I want to update the sp so that it returns an additional two fields to the 'end' of each returned record. I can do something such as: CREATE FUNCTION proc_Find_store (IN p

How to save query errors in plpgsql to a table?

蹲街弑〆低调 提交于 2019-12-10 21:40:58
问题 I need to save in a table the error code ( SQLSTATE ) and the error message ( SQLERRM ) returned by an INSERT or an UPDATE. My procedure must execute an INSERT, and if an error occurs, it must be saved into an apposite table. But the problem is that if I use an EXCEPTION block, when an error occurs the transaction is aborted and any command after cannot execute. How can I save the error returned by a query in a table using PLPGSQL? 回答1: There are two possible solutions: use a CSV format of

Sanitize user input with the USING keyword in PL/pgSQL

纵饮孤独 提交于 2019-12-10 21:15:06
问题 This is how I create my search_term : IF char_length(search_term) > 0 THEN order_by := 'ts_rank_cd(textsearchable_index_col, to_tsquery(''' || search_term || ':*''))+GREATEST(0,(-1*EXTRACT(epoch FROM age(last_edited)/86400))+60)/60 DESC'; search_term := 'to_tsquery(''' || search_term || ':*'') @@ textsearchable_index_col'; ELSE search_term := 'true'; END IF; I am having some trouble with a PLPGSQL function: RETURN QUERY EXECUTE ' SELECT * FROM articles WHERE $1 AND ' || publication_date_query

VARIADIC parameter must be the last input parameter

我是研究僧i 提交于 2019-12-10 21:02:01
问题 How to create two VARIADIC parameters. Look at my code and correct me. CREATE OR REPLACE FUNCTION ip_source_test(text,text,date,date,VARIADIC int[],VARIADIC text[]) RETURNS TABLE (no_documents int, "Report By" text, "Report_By" text) AS $$ BEGIN IF 'Source Member' = $1 THEN RETURN QUERY SELECT..... ELSEIF 'company' = $1 THEN RETURN QUERY SELECT..... ELSE RAISE NOTICE 'Not Worked'; END IF; RETURN; END; $$ LANGUAGE plpgsql; Error: VARIADIC parameter must be the last input parameter. In SQL code

Dynamic ORDER BY and ASC / DESC in a plpgsql function

不羁的心 提交于 2019-12-10 19:56:39
问题 Following the approach mentioned in this link, I want to pass ORDER BY and sorting order to a function dynamically. ORDER BY is working fine but I am not able to pass sorting order ( ASC / DESC ). What I have now: CREATE OR REPLACE FUNCTION list(_limit integer,_offset integer,sort_by varchar(100), _order varchar(100),_category varchar(100)) RETURNS TABLE( id INTEGER, name VARCHAR, clientname VARCHAR, totalcount BIGINT ) AS $$ DECLARE empty text := ''; BEGIN RETURN Query EXECUTE 'SELECT d.id,