Executing dynamic SQL in Postgres 8.2 - postgresql

I am trying to create a table inside a function using dynamic SQL and immediately copy it into another table.
execute 'create table week_temp as
select w.*, ww.*
from employer_weekly w
left join employer_weekly_' || $1 || '_2 ww
on w.w_employer::int = ww.emp_' || $1 || '::int';
drop table if exists employer_weekly;
create table employer_weekly as select * from week_temp;
I am receiving the following error:
Error : ERROR: relation with OID 9288742 does not exist
CONTEXT: SQL statement "create table employer_weekly as select * from
week_temp"
Checking manually, I can see week_temp and can access it correctly.
Would appreciate any clues!

What #a_horse_with_no_name said.
But - the problem you are seeing is that the non-dynamic statements in your function are being compiled. This means the "FROM week_temp" is looking at an older version of week_temp that no longer exists. Make that statement dynamic SQL too and it should work.

Related

Undefined column error when using the output of a function as the table name

I have a multi-tenant database where each tenant gets their own schema. Each schema has a set of materialized views used in full-text searches.
The following function takes a schema name and a table name and concatenates them into schema.table_name format:
CREATE OR REPLACE FUNCTION create_table_name(_schema text, _tbl text, OUT result text)
AS 'select $1 || ''.'' || $2'
LANGUAGE SQL
It works as expected in PGAdmin:
I'm trying to use this function in a prepared statement, like this:
SELECT p.id AS id,
ts_rank(
p.document, plainto_tsquery(unaccent(?))
) AS rank
FROM create_table_name(?, 'project_search') AS p
WHERE p.document ## plainto_tsquery(unaccent(?))
OR p.name ILIKE ?
However, when I run it, I get the following error:
ERROR 42703 (undefined_column) column p.id does not exist
If I "hard-code" the schema and table name though, it works.
Why am I getting this error?
P.S. I should note that I am aware of the dangers of this approach, but the schema name always comes from inside my application so I'm not worried about SQL injection.
You want to use the function result as table name in a query, but what you are actually doing is using the function as a table function. This “table” has only one row and one column called result, which explains the error message.
You need dynamic SQL for that, for example by using PL/pgSQL code in a DO statement:
DO
$$DECLARE
...
BEGIN
EXECUTE
format(
E'SELECT p.id AS id,\n'
' ts_rank(\n'
' p.document,\n'
' plainto_tsquery(unaccent(?))\n'
' ) AS rank\n'
'FROM %I.project_search AS p\n'
'WHERE p.document ## plainto_tsquery(unaccent($1))\n'
'OR p.name ILIKE $2',
schema_name
)
USING fts_query, like_pattern
INTO var1, ...;
...
$$;
To handle more than one result row, you'd use a FOR loop — this is just a simple example to show the principle.
Note how I use format with the %I pattern to avoid SQL injection. Your function is vulnerable.

DB2 FUNCTION TEST ERROR

HOW to resolve this problem ? :
CREATE OR REPLACE FUNCTION "BADIL_PROD"."FCT_UPDATE_POINTAGE" (id_projet in NUMBER)
return NUMBER
is
Results NUMBER;
CURSOR cur_update IS
SELECT distinct t.ID_TACHE,t.id_collaborateur,t.id_ref_rubrique_activite
FROM TACHES t
where t.ID_PROJET =id_projet ;
begin
dbms_output.enable(200000);
FOR cur in cur_update
LOOP
BEGIN
dbms_output.put_line(cur.ID_TACHE || '--' || cur.ID_COLLABORATEUR || '--' || cur.ID_REF_RUBRIQUE_ACTIVITE);
UPDATE POINTAGES p SET p.ID_TACHE_INCIDENT = cur.ID_TACHE
where p.id_projet_tma=id_projet
and p.ID_COLLABORATEUR = cur.ID_COLLABORATEUR
AND p.ID_REF_RUBRIQUE_ACTIVITE = cur.ID_REF_RUBRIQUE_ACTIVITE;
END;
END LOOP;
results:=0;
return(results);
end fct_update_pointage;
So when I want to test this function I get this error in data studio :
(specific name "FCT_UPDATE_POINTAGE") attempted to modify data but was
not defined as MODIFIES SQL DATA.. SQLCODE=-577,
This worked for me with DB2 V11 on Linux:
db2set DB2_COMPATIBILITY_VECTOR=830
db2stop
db2start
db2 create database mydb
db2 connect to mydb
db2 "create table taches(....)"
db2 "create table pointages(...)"
db2 -tvf FCT_UPDATE_POINTAGE.sql
(where FCT_UPDATE_POINTAGE.sql contains your function definition )
However, note the documented restriction in the documentation "Compiled SQL functions (including PL/SQL functions) that MODIFY SQL DATA can only be used as the only element on the right side of an assignment statement that is within a compound SQL (compiled) statement."
DB2 Version 9.7 allowed the "modifies sql data" clause when defining PL/SQL user defined functions, but IBM removed that ability at DB2 Version 10.1 and higher. Maybe you need to convert to an sproc.

Drop table if exists in PostgreSQL database

I am trying to drop table if it is exists in the present working database of PostgreSQL. For which I am trying the following query.
Example:
var1 := 'IF EXISTS (select * from INFORMATION_SCHEMA.TABLES WHERE name = ''Table_'|| Suffix ||''') then
DROP TABLE Table_'||Suffix||'';
execute var1;
But getting error near IF.
execute executes SQL statements, not PL/pgSQL commands. The IF statement is a PL/pgSQL construct.
In any case you can use
DROP TABLE IF EXISTS ...
(see the manual page for DROP).

Exporting sequences in PostgreSQL

I want to export ONLY the sequences created in a Database created in PostgreSQL.
There is any option to do that?
Thank you!
You could write a query to generate a script that will create your existing sequence objects by querying this information schema view.
select *
from information_schema.sequences;
Something like this.
SELECT 'CREATE SEQUENCE ' || sequence_name || ' START ' || start_value || ';'
from information_schema.sequences;
I know its too old but today I had similar requirement so I tried to solve it the same way by creating a series of "CREATE SEQUENCE" queries which can be used to RE-create sequences on the other DB with bad import (missing sequences)
here is the SQL I used:
SELECT
'CREATE SEQUENCE '||c.relname||
' START '||(select setval(c.relname::text, nextval(c.relname::text)-1))
AS "CREATE SEQUENCE SQLs"
FROM
pg_class c
WHERE
c.relkind = 'S'
Maybe that can be helpful for someone.
Using DBeaver, you can
open a schema
select its sequences
crtl-F to search for the sequences you're interested in
crtl-A to select all of them
Right-click and select Generate SQL -> DDL
You will be given SQL statements to create all of the sequences selected.

ERROR: relation "stg_data_bt_sur" does not exist

I've created a table in postgresql which is OK and I'm able to do select/insert using SQL manager tool or Navicat Lite tool.
But, when I'm trying to make simple select from LINUX(ubuntu) I have following message:
postgres=# select count(*) from stg_data_brest_surgery;
ERROR: relation "stg_data_brest_surgery" does not exist
STATEMENT: select count(*) from stg_data_brest_surgery;
ERROR: relation "stg_data_brest_surgery" does not exist.
I also used table name with double quotes - same result.
Any idea what's the issue?
Chances are the schema isn't in your search path, Try \dn to list namespaces and then you can either add the schema like:
SELECT * from "schema"."table";
Or you can set your search path:
SET search_path="schema";
SELECT * FROM "table";
RESET search_path;