i am trying to create table in talend using below code i see no error but in database this table is not getting created
do $$ declare begin execute 'DROP TABLE IF EXISTS tname'; execute 'CREATE TABLE IF NOT EXISTS tname (ACTIVITY VARCHAR(32))'; end $$ ;
Please help me i am new in Talend
Should be something like this
DO
$$
DECLARE BEGIN
EXECUTE 'DROP TABLE IF EXISTS tname';
EXECUTE 'CREATE TABLE IF NOT EXISTS tname (ACTIVITY VARCHAR(32))';
END;
$$
You will have to just use this component tDBRow
And a very important thing is to to use tDBCommit after the tDBRow if not the table would not be created in your Postgres Database
or just tick the commit (in advanced settings if you are using tDBConnection component)
Related
In postgresql 13, I am inserting data of a table into temporary table at run time. Query works fine when executed. But when I try to create store procedure on top of the query it fails with error: ERROR: "temp" is not a known variable
Can someone please help me to understand what am I missing?
DB FIDDLE
CREATE OR REPLACE PROCEDURE dbo.<proc-name>()
LANGUAGE plpgsql
AS $$
BEGIN
DROP TABLE IF EXISTS child;
SELECT Id, Name
into TEMP TABLE child
FROM dbo.master;
COMMIT;
END;
$$;
Thanks
SELECT ... INTO is PL/pgSQL syntax to store a query result in a variable. You should use CREATE TABLE ... AS SELECT.
I have an event trigger that executes on create, alter and drop table.
create event trigger CustomizeTable
on ddl_command_end
when tag in ( 'create table', 'alter table', 'drop table' )
execute procedure CustomizeTable();
Within the procedure I want to create a trigger on the newly created table.
create or replace function CustomizeTable() returns event_trigger as
$$
begin
EXECUTE 'create trigger DoAudit after update on XXXXXX...
end;
$$
language plpgsql;
How can I get the table name within the event trigger?
I tried using TG_TABLE_NAME as explained here but it seems that this only works on non-event-triggers.
You'll have to use the event trigger information functions described in the documentation.
For example, to get the name a newly created table, use
SELECT objid::regclass
FROM pg_event_trigger_ddl_commands();
I'm trying to create a PostgreSQL event trigger, that fires whenever a user creates anything (table, view, function, sequence...) and sets the owner of this newly created thing to a certain role.
So far I have the even trigger itself, that fires on any CREATE command:
CREATE EVENT TRIGGER setOwnerToMyRole ON ddl_command_end
WHEN tg_tag LIKE 'CREATE%'
EXECUTE PROCEDURE setOwnerToMyRole();
But I am having problems with the function itself:
CREATE FUNCTION setOwnerToMyRole() RETURNS event_trigger LANGUAGE plpgsql
AS $$ BEGIN
ALTER <type> <name> OWNER TO myRole
END; $$;
How do I get the type (as in table, view, etc.) and how do i get the name of the newly created thing?
edit:
Looking at this question and this question and of course CREATE EVENT TRIGGER and Trigger Procedures, this is currently not really possible :(
This works pretty well for me, although it's a little broader net than needed. It is based on code at https://blog.hagander.net/setting-owner-at-create-table-237/
CREATE OR REPLACE FUNCTION trg_create_set_owner()
RETURNS event_trigger
LANGUAGE plpgsql
AS $$
DECLARE
obj record;
BEGIN
-- postgresql 9.5 or later:
-- FOR obj in SELECT table_name FROM pg_event_trigger_ddl_commands() WHERE command_tag='CREATE TABLE'
FOR obj IN SELECT tablename FROM pg_tables WHERE tableowner = current_user LOOP
EXECUTE format('ALTER TABLE %s OWNER TO my_group', obj.tablename);
END LOOP;
END;
$$;
CREATE EVENT TRIGGER trg_create_set_owner
ON ddl_command_end
WHEN tag IN ('CREATE TABLE', 'CREATE TABLE AS')
EXECUTE PROCEDURE trg_create_set_owner();
Guess, I should answer this question properly and not just in an edit:
What I want is currently not possible. Perhaps a future update to Postgres will add more functionality to eventtriggers.
Sources:
http://www.postgresql.org/docs/current/static/sql-createeventtrigger.html
http://www.postgresql.org/docs/current/static/plpgsql-trigger.html
How to get the name of the altered table in a Postgres event trigger?
How to get SQL text from Postgres event trigger
I am going to update the table of huge record from one DB to another DB.
Here I'm using a cursor function:
CREATE OR REPLACE FUNCTION Drug("Drug" text)
RETURNS void AS
$BODY$
DECLARE
curs refcursor;
rec record;
BEGIN
OPEN curs FOR EXECUTE 'SELECT * FROM ' || quote_ident("Drug") FOR UPDATE;
LOOP
FETCH NEXT FROM curs INTO rec;
EXIT WHEN rec IS NULL;
RAISE NOTICE '%', rec."Id";
EXECUTE format('update statement with dblink', tbl)
USING rec.ctid;
END LOOP;
END
$BODY$ LANGUAGE plpgsql;
Is this correct?... or any other...
Please suggest...
What I know dblink doesn't support cursors - cursors cannot be parameter of query (cursors in PostgreSQL are very careful, usually limited to transaction). So this idea is wrong. You can generate row updates - but it will be slow.
What you can do. You can use FDW API (foreign data wrappers API) and create permanent link (foreign table to second database). Then you can send a usual UPDATE statement to second database.
I have two databases db1 and db2. db1 is source database, db2 is target database:
-- all is executed on db2
CREATE EXTENSION postgres_fdw;
CREATE SERVER db1 FOREIGN DATA WRAPPER postgres_fdw OPTIONS (dbname 'db1');
CREATE USER MAPPING FOR pavel
SERVER db1 OPTIONS (user 'pavel');
CREATE FOREIGN TABLE db1_source(a int, b int)
SERVER db1 OPTIONS (table_name 'source');
Now I have a FDW table db1_source and I can do UPDATE:
db2=# UPDATE target SET b = db1_source.b
FROM db1_source WHERE target.a = db1_source.a;
This is most effective way how to do UPDATE based on data in other database
I am trying to drop table if exists else proceed for next step in function using PostgreSQL 9.3 version.
Example:
Create or replace function test(tablename varchar)
returns setof record as
$$
Declare
var1 varchar :='table_';
Begin
var1 :=var1 ||tablename;
/* Here need to drop table if exists */
drop table if exist var1;
/* else proceed for next step */
....
....
end;
$$
language plpgsql;
Try using EXECUTE
EXECUTE 'DROP TABLE IF EXISTS ' || var1;
You need to run the DROP TABLE command as below using execute (As #FuzzyTree already pointed much before me)
execute 'drop table ' || var1;
(OR)
execute 'DROP VIEW ' || var1;
Also another pointer, DROP TABLE is not allowed in a non-volatile function. So you may have to change the last line of your function to be
LANGUAGE 'plpgsql' VOLATILE;
Check this post, Will get you better idea How to delete table *or* view from PostgreSQL database?