plpgsql

Passing the table as a parameter

萝らか妹 提交于 2019-12-02 10:05:47
I have to convert from lat and long to geom to use PostGIS. My problem, I have various tables from different locations and I want to pass the table as a parameter to the function. I'm trying this: CREATE or REPLACE FUNCTION convert_from_lon_lat(float,float,character varying) RETURNS integer AS $$ select id from $3 as vertices order by vertices.geom <-> ST_SetSrid(ST_MakePoint($1,$2),4326) LIMIT 1; $$ LANGUAGE SQL; but I get a syntax error. EDIT1: So I changed the previous code to this: CREATE or REPLACE FUNCTION convert_from_lon_lat(long float, lat float, _table character varying) RETURNS

PostgreSQL: How to pass table/view name as a parameter to function in PostgreSQL?

笑着哭i 提交于 2019-12-02 10:05:40
For example: I have a VIEW called "view1" which contains 'name' and 'slno' columns, now i want it to be display using FUNCTION called "f1" as shown below: --Function create or replace function f1(viewname varchar) returns table (name varchar,slno integer) as $body$ begin return query select * from viewname; end; $body$ language plpgsql; This is dynamic SQL, so you need EXECUTE . RETURN QUERY EXECUTE format('SELECT * FROM %I', "name"); Separately, that's a weird thing to want to do. 来源: https://stackoverflow.com/questions/22753868/postgresql-how-to-pass-table-view-name-as-a-parameter-to

Function taking forever to run for large number of records

…衆ロ難τιáo~ 提交于 2019-12-02 09:21:59
I have created the following function in Postgres 9.3.5: CREATE OR REPLACE FUNCTION get_result(val1 text, val2 text) RETURNS text AS $BODY $Declare result text; BEGIN select min(id) into result from table where id_used is null and id_type = val2; update table set id_used = 'Y', col1 = val1, id_used_date = now() where id_type = val2 and id = result; RETURN result; END; $BODY$ LANGUAGE plpgsql VOLATILE COST 100; When I run this function in a loop of over a 1000 or more records it just does freezing and just says "query is running". When I check my table nothing is being updated. When I run it

Plpgsql: How can i assign value to variable at declaration section?

≡放荡痞女 提交于 2019-12-02 09:20:58
For example. --Assigning value to variable in function as a parameter. create or replace function f1(number int :=1) --This method is not working to me. or --Assigning values to variables at declaration section. declare number int :=1; -- Here i need to assign the value to number but its not working. name varchar :='xyz'; Here is how you can do it: create or replace function f1(my_number int default 1) or declare my_number int :=1; Look at declaration documentation Assign value, general note (about assign value to variable out of declaration section) The language PLpgSQL syntax have many ways

Syntax error in dynamic SQL in pl/pgsql function

限于喜欢 提交于 2019-12-02 08:56:33
问题 I am using pl/pgsql in PostgreSQL 10, to create complex queries. I am testing a query with a couple of JOIN s and AND s. This is what I have so far: DROP FUNCTION IF EXISTS search_person(name text); CREATE FUNCTION search_person(name text) RETURNS TABLE(address_id integer, address_geom text, event_name text) AS $$ --DECLARE BEGIN RETURN QUERY EXECUTE 'SELECT address.id, event.name, address.geom FROM event JOIN person JOIN address JOIN person_address JOIN event_person WHERE person_address

How do I change the formatting for my return values in this function?

限于喜欢 提交于 2019-12-02 08:53:25
I have a function I created like this: create or replace function core.nrt_summary_by_param_id(int) returns table (reason_type_id int, reason_id int, descr varchar(256), num int, pct numeric, cause_rank int) as $$ begin return query with r3 as ( //A bunch of queries ) select r3.reason_type_id, r3.reason_id, r3.desc, r3.num, r3.pct, r3.cause_rank from r3; where r3.param_id = $1 return; end $$ language plpgsql; When I call the function like so: select nrt_summary_by_param_id(5); The results are like this: nrt_summary_by_param_id ---------------------------------------------------- (1,2001,"A",14

PL/pgSQL control structures for lists / arrays

若如初见. 提交于 2019-12-02 08:32:02
Is it possible to use something like this in Postgres? This is the example from PL/SQL what I want to do: PROCEDURE CREATE_PAYMENT(P_AMOUNT IN NUMBER, P_INVOICE_LIST IN SIMPLEARRAYTYPE, P_AMOUNT_LIST IN NUMBER_TABLE -- pass list of amounts . . .) s_chk_amnt NUMBER; invoice_list SIMPLEARRAYTYPE; amount_list NUMBER_TABLE; BEGIN -- check if amount list is null or contains zeros IF p_amount_list IS NOT NULL AND p_amount_list.COUNT <> 0 THEN FOR r IN p_amount_list.FIRST..p_amount_list.LAST LOOP s_chk_amnt := s_chk_amnt + p_amount_list(r); END LOOP; END IF; Can I declare a list of characters and

How to get the value of a dynamically generated field name in PL/pgSQL

守給你的承諾、 提交于 2019-12-02 08:11:03
问题 Sample code trimmed down the the bare essentials to demonstrate question: CREATE OR REPLACE FUNCTION mytest4() RETURNS TEXT AS $$ DECLARE wc_row wc_files%ROWTYPE; fieldName TEXT; BEGIN SELECT * INTO wc_row FROM wc_files WHERE "fileNumber" = 17117; -- RETURN wc_row."fileTitle"; -- This works. I get the contents of the field. fieldName := 'fileTitle'; -- RETURN format('wc_row.%I',fieldName); -- This returns 'wc_row."fileTitle"' -- but I need the value of it instead. RETURN EXECUTE format('wc

Oracle DBMS_LOB.WRITEAPPEND to Postgres Conversion

流过昼夜 提交于 2019-12-02 07:54:12
Can someone please let me know how to convert the below Oracle Code to Postgresql IF prodNum = 1 THEN DBMS_LOB.WRITEAPPEND(pkgFilterNode, LENGTH(pkgFilter_tab || '<PackageFilters isNewFormat="Y" > '||l_crlf), pkgFilter_tab || '<PackageFilters isNewFormat="Y" > '||l_crlf); END IF; Appreciate your time! It depends on a size of your large object. When your large objects are less about 500MB, then you don't need to use LOBs (PostgreSQL uses term LO), and you can use a text or varchar type - the work is similar like with varchar . After this size you should to use LO API. CREATE OR REPLACE FUNCTION

Saving the output of a dynamic query that uses refcursor into a table

落爺英雄遲暮 提交于 2019-12-02 07:37:45
问题 In continuation to a previous case, in which a dynamic SELECT query that uses refcursor was created and then was executed - I would like to ask the following: The desired output that we got from the indicated procedure was output into the DataOutput. I would like to find a way to store the data into a new table in the db. Instead of the straight forward command: CREATE TABLE mydaughtertable AS SELECT enrich_d_dkj_p_k27ac,enrich_lr_dkj_p_k27ac,enrich_r_dkj_p_k27ac FROM dkj_p_k27ac The idea is