create or replace function exc_using(int, text) returns int as
$$
declare i int;
begin
for i in execute 'select * from generate_series(1,$1) ' using $1+1 loop
raise notice '% ', i;
raise notice 'i + i *3: % ', i + i * 3;
end loop;
execute 'select $2 + $2 * 3 + length($1) '
into i using $2, $1;
return i;
end
$$
language plpgsql;
seems A
execute 'select $2 + $2 * 3 + length($1) '
into i using $2, $1;
return i;
is the same as:
B
execute 'select $1 + $1 * 3 + length($2) '
into i using $1, $2;
return i;
My preception is that $2 Absolutely refer to function second argument that data type is text. but seems plpgsql $[position] is relative. But I cannot get manual reference for this edge case.
Of course they are relative. What else would they be? But what are they relative to?
Inside the EXECUTE string, the $1 and $2 are relative to the USING. Outside, they are relative to the function parameters.
Related
Created this Postgres Function which is working fine, but the actual requirement is to pass the input parameter in the function to the Cursor which uses the dynamic SQL as follows,
The below is the Function
CREATE OR REPLACE FUNCTION ssp2_pcat.find_shift_dates (date_to_find date)
RETURNS void
LANGUAGE 'plpgsql'
COST 100
VOLATILE
AS $BODY$
DECLARE
C1 CURSOR FOR
SELECT TABLE_NAME, 'SELECT COUNT(*) FROM ' || TABLE_NAME || ' WHERE ' ||
COLUMN_NAME || ' = '||
'CASE WHEN ' || COLUMN_NAME || ' LIKE ' || '''%START%'''||' THEN
date_to_find ELSE date_to_find-1 END;' SQL_TEXT
FROM (
SELECT TABLE_NAME, COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME IN (SELECT TABLE_NAME FROM RESET_DATES WHERE RESET_IT =
'Y') AND
UPPER(DATA_TYPE) = 'DATE'
AND (COLUMN_NAME LIKE '%START%' OR COLUMN_NAME LIKE '%END%')
AND (COLUMN_NAME NOT LIKE '%TEST%'
AND COLUMN_NAME NOT LIKE '%PCAT%'
AND COLUMN_NAME NOT LIKE '%ORDER%'
AND COLUMN_NAME NOT LIKE '%SEASON%'
AND COLUMN_NAME NOT LIKE '%_AT')
ORDER BY 1, 2) A;
END_COUNT INTEGER := 0;
START_COUNT INTEGER := 0;
TABLENAME VARCHAR(32) := 'ALFU';
l_start TIMESTAMP;
l_end TIMESTAMP;
Time_Taken VARCHAR(20);
BEGIN
l_start := clock_timestamp();
DELETE FROM SHIFT_DATES_COUNT;
FOR I IN C1 LOOP
IF I.TABLE_NAME <> TABLENAME THEN
INSERT INTO SHIFT_DATES_COUNT VALUES (TABLENAME, START_COUNT,
END_COUNT, current_timestamp::timestamp(0));
TABLENAME := I.TABLE_NAME;
END_COUNT := 0;
START_COUNT := 0;
END IF;
IF STRPOS(I.SQL_TEXT, 'END') > 0 THEN
EXECUTE I.SQL_TEXT INTO END_COUNT;
RAISE NOTICE '% ', ('END: ' || I.SQL_TEXT);
ELSE
EXECUTE I.SQL_TEXT INTO START_COUNT;
RAISE NOTICE '% ', ('START: ' || I.SQL_TEXT);
END IF;
END LOOP;
INSERT INTO SHIFT_DATES_COUNT VALUES (TABLENAME, START_COUNT, END_COUNT,
current_timestamp::timestamp(0));
RAISE NOTICE '% ', ('INSERT INTO SHIFT_DATES_COUNT Done...');
l_end := clock_timestamp();
Time_Taken := (l_end-l_start);
RAISE NOTICE '% ', ('FIND_SHIFT_DATES Took: ' || Time_Taken );
END;
$BODY$;
Please let me know how can I use the date_to_find input parameter in the Dynamic SQL in the Cursor in the above Function.
You can use unbound cursor, clause fetch to get data from cursor, and exit when not found to finish, like:
CREATE OR REPLACE FUNCTION example (p_name text) RETURNS void LANGUAGE 'plpgsql' AS $$
DECLARE
C1 refcursor;
res record;
BEGIN
OPEN c1 FOR EXECUTE 'SELECT * FROM pg_database WHERE datname like ''%'||p_name||'%''';
LOOP
FETCH c1 INTO res;
EXIT WHEN not found;
raise notice 'value datname: %',res.datname;
END LOOP;
CLOSE c1;
RETURN;
END; $$;
--in my case
select example ('test')
NOTICE: value datname: test
NOTICE: value datname: test_msmov
NOTICE: value datname: test_resources
NOTICE: value datname: test_load_table
NOTICE: value datname: test_resources2
Total query runtime: 63 msec
1 row retrieved.
You can use EXECUTE clause for open cursor, see the documentation of PostgreSQL
https://www.postgresql.org/docs/10/plpgsql-cursors.html#PLPGSQL-CURSOR-OPENING
Example:
OPEN curs1 FOR EXECUTE format('SELECT * FROM %I WHERE col1 = $1',tabname) USING keyvalue;
I have a function in which I want to loop throw each array's item. I get a string in input like 'tab1#tab2#tab3'...Each item of the string must be splitted (by #) in order to obtain tab1, tab2, tab3 into myArray. My function is:
CREATE OR REPLACE FUNCTION funcA(
myUid integer,
mytable_name varchar,
state varchar)
RETURNS void AS
$BODY$
declare
TABarray varchar[];
indx int;
BEGIN
select REGEXP_REPLACE('{'||myTABLE_NAME||'}','#','','g') into TABarray;
for indx in 1..array_length(TABarray, 1) loop
execute 'update ' || TABarray(indx) || ' set CODE_STATO = ''' || state || ''' where uid = ' || myUid || 'and CODE_STATO <> ''N'' ';
raise notice 'i: %', TABarray[ indx ];
end loop;
END; $BODY$
LANGUAGE plpgsql stable
As a result I expect 3 splitted string such as:
-tab1
-tab2
-tab3
Right now myFunction print {tab1tab2tab3}.
select oms_write_stato (10, 'tab1#tab2#tab3', '')
What I am doing wrong?
Thank you in advance!
You could use string_to_array to split the string into array. Also, you were using () to refer to index elements instead of []
CREATE OR replace FUNCTION funca( myuid integer, mytable_name varchar, state varchar)
returns void AS
$BODY$
DECLARE
tabarray VARCHAR[];
indx int;
BEGIN
SELECT string_to_array(mytable_name ,'#')
INTO tabarray;
for indx IN 1..array_length(tabarray, 1)
LOOP
--check the o/p of this notice below to see if update statement is correct
--raise notice '%', 'update ' || tabarray[indx] || ' set CODE_STATO = ''' || state || ''' where uid = ' || myuid || 'and CODE_STATO <> ''N'' ';
execute 'update ' || tabarray[indx] || ' set CODE_STATO = ''' || state || ''' where uid = ' || myUid || ' and CODE_STATO <> ''N'' ';
raise notice 'i: %', tabarray[ indx ];
END LOOP;
END;
$BODY$ language plpgsql stable;
PL/pgSQL has FOREACH IN ARRAY statement for this purpose:
You task can be written some like:
-- Don't use case mixed identifiers (prohibit camel notation)
create or replace function funca(uid integer,
tablenames varchar,
state varchar)
returns void as $$
declare tablename text;
begin
foreach tablename in array string_to_array(tablenames, '#')
loop
execute format('update %I set code_stato = $1 where uid = $2 and code_state <>'N',
tablename)
using state, uid;
end loop;
end;
$$ language plpgsql;
Notes:
don't mix upper lower chars in identifiers
don't mix upper / lower keywords - there are some variants - keywords by upper cases, or all by lower cases, but mix is bad for reading
when you use dynamic SQL, then sanitize your data before you use it in dynamic query - use quote_ident,quote_literal functions, or function format with secure placeholders and when it is possible, pass with USING clause.
postgres has array types - using str1#str2#str3#str4 is little bit obscure in Postgres - use native arrays like ARRAY['str1','str2','str3','str4'].
How to change "EXECUTE sql_string" to "RETURN QUERY" in a function declaration, but there is a where array which must be used.
In other words, how to change this
p_sql := 'SELECT *
FROM tbl
WHERE ' || array_to_string(where_arr, ' AND ');
FOR item IN EXECUTE p_sql
LOOP
RETURN NEXT item;
END LOOP;
to this
RETURN QUERY
SELECT *
FROM tbl
WHERE || array_to_string(where_arr, ' AND '); -- this is the place
I want my editors (phpstorm, notepad++) to see SQL as SQL and not as string.
https://www.postgresql.org/docs/current/static/plpgsql-control-structures.html#PLPGSQL-STATEMENTS-RETURNING
return query execute $$
select *
from tbl
where $$ || array_to_string(where_arr, ' and ');
Can I make this function simple, using only sql in Postgres? (without delete, forget it)
I need only select and insert queries.
I have tried with "with recursive" no success.
CREATE OR REPLACE FUNCTION mt_CriarRotaExecutada(id_rota integer)
RETURNS void AS
$$
DECLARE
searchsql text := '';
searchsqlId text := '';
var_match RECORD;
BEGIN
EXECUTE('delete from rota_executada where id_rota = ' || CAST( id_rota As text));
searchsql := 'select (ST_DumpPoints(the_geom)).geom as the_geom,
id_destino
from (select id, the_geom, id_destino
from rota_data
where num =' || CAST( id_rota As text) ||
' order by id) a';
FOR var_match IN EXECUTE(searchsql)
LOOP
EXECUTE 'insert into rota_executada(id_rota, id_rua, id_destino, the_geom, visitado)
select $1, id, $3, $2, $4
from ruas r
ORDER BY r.the_geom <#> $2
LIMIT 1'
USING id_rota, var_match.the_geom, var_match.id_destino, 'N';
END LOOP;
END;
$$
LANGUAGE 'plpgsql';
Thanks.
You dont need EXECUTE just do the sql statement
DELETE from rota_executada where id_rota = id_rota::text;
AND
INSERT INTO rota_executada(id_rota, id_rua, id_destino, the_geom, visitado)
SELECT id_rota, id, var_match.id_destino, var_match.the_geom, 'N'
FROM ruas r
ORDER BY r.the_geom <#> var_match.the_geom
LIMIT 1
Can any one of you tell me how to approach this:
CREATE OR REPLACE FUNCTION name()
RETURNS ????? AS
$func$
BEGIN
SELECT * FROM tbl_a a;
SELECT * FROM tbl_b b;
END
$func$ LANGUAGE plpgsql;
Both tables have different structures.
You can use cursors but I can hardly imagine why you need such a function.
CREATE OR REPLACE FUNCTION my_multiselect(refcursor, refcursor) RETURNS VOID AS
$func$
BEGIN
OPEN $1 FOR SELECT * FROM information_schema.routines;
OPEN $2 FOR SELECT * FROM information_schema.sequences;
END
$func$ LANGUAGE plpgsql;
BEGIN;
SELECT my_multiselect('first_cursor_to_routines', 'second_cursor_to_sequences');
FETCH ALL IN first_cursor_to_routines;
FETCH ALL IN second_cursor_to_sequences;
COMMIT;
I'm not really sure what you're doing with this, but it sounds like you just want to return a union of these distinct result sets. You can do this with a dynamic query. I'm using Postgres 9.4.
CREATE OR REPLACE FUNCTION make_query(IN p_tables text[])
RETURNS void AS
$BODY$
DECLARE
v_qry text;
v_cols text;
v_types text;
v_as text;
BEGIN
EXECUTE format('
WITH sub AS (
SELECT
table_name,
column_name,
data_type
FROM
information_schema.columns
WHERE
table_name = ANY(%L)
ORDER BY
table_name,
ordinal_position)
,sub2 AS(
SELECT
DISTINCT ON (column_name, data_type)
column_name || '' '' || data_type AS def
FROM
sub
)
SELECT
string_agg(def, '','')
FROM
sub2;
',
p_tables
) INTO v_types;
v_qry := '
CREATE OR REPLACE FUNCTION name()
RETURNS TABLE(' || v_types || ') AS
$func$';
FOR i IN 1..array_upper(p_tables, 1)
LOOP
v_as := 'tbl' || i;
EXECUTE format('
WITH sub AS (
SELECT
table_name,
column_name,
data_type
FROM
information_schema.columns
WHERE
table_name = ANY(%L)
ORDER BY
table_name,
ordinal_position)
,sub2 AS(
SELECT
DISTINCT ON (column_name, data_type)
CASE WHEN table_name = ''%I''
THEN %L || ''.'' || column_name
ELSE ''NULL::'' || data_type
END AS cols
FROM
sub
)
SELECT
string_agg(cols, '','')
FROM
sub2;
',
p_tables,
p_tables[i],
v_as
) INTO v_cols;
IF i > 1 THEN
v_qry := v_qry || '
UNION ALL';
END IF;
v_qry := v_qry || '
SELECT ' || v_cols || ' FROM ' || p_tables[i] || ' AS ' || v_as;
IF i = array_upper(p_tables, 1) THEN
v_qry := v_qry || ';';
END IF;
END LOOP;
v_qry := v_qry || '
$func$ LANGUAGE sql;
';
EXECUTE v_qry;
END;
$BODY$
LANGUAGE plpgsql VOLATILE;
Sorry it looks ugly here, but this formatting helps the final product look nicer. If you're shy about executing a dynamic query like this off the bat, just replace EXECUTE v_qry; with RAISE INFO 'v_qry: %', v_qry; and it will simply print the dynamic query out in a message without executing it, so you can review what it will do once executed.
Then execute make_query() with a list of tables you want to display like this:
SELECT make_query(ARRAY['tbl_a', 'tbl_b']);
The result is that you will now have a function called name() which you can call in order to see the results of both tables at the same time, with all the union details already sorted out:
SELECT * FROM name();