I have write like this code in PostgreSQL. But value is not inserting into BENMASTHIST table.
please help to solve this error.
EXECUTE 'INSERT INTO BENMASTHIST SELECT BENMAST_ENTITY_CODE, BENMAST_CUSTOMER_CODE, BENMAST_BEN_CODE,
FN_GETCD(' ||
P_ENTITY_CODE || '),' || W_BEN_HIST_SL ||
', BENMAST_NAME, BENMAST_MOBILE_NO,
BENMAST_EMAIL_ID, BENMAST_PHOTO, BENMAST_SOURCE, BENMAST_CR_BY, BENMAST_CR_ON,
BENMAST_MO_BY, BENMAST_MO_ON, BENMAST_AU_BY, BENMAST_AU_ON, TBA_KEY, BENMAST_REMARKS,
BENMAST_ADDR,BENMAST_CITY,BENMAST_STATE
FROM BENMAST WHERE BENMAST_ENTITY_CODE =$1 AND BENMAST_CUSTOMER_CODE =$2 AND BENMAST_BEN_CODE =$3'
USING P_ENTITY_CODE, P_CUST_CODE, P_BEN_CODE;
EXCEPTION
WHEN OTHERS THEN
P_SUC_FLAG := 'F';
P_ERROR := 'BENREG013'; --'Error While Inserting Into Benmast';
ROLLBACK;
END;
Related
Here is the code in SAS, It finds the numeric columns with blank and replace with 0's
DATA dummy_table;
SET dummy_table;
ARRAY DUMMY _NUMERIC_;
DO OVER DUMMY;
IF DUMMY=. THEN DUMMY=0;
END;
RUN;
I am trying to replicate this in Redshift, here is what I tried
create or replace procedure sp_replace_null_to_zero(IN tbl_nm varchar) as $$
Begin
Execute 'declare ' ||
'tot_cnt int := (select count(*) from information_schema.columns where table_name = ' || tbl_nm || ');' ||
'init_loop int := 0; ' ||
'cn_nm varchar; '
Begin
While init_loop <= tot_cnt
Loop
Raise info 'init_loop = %', Init_loop;
Raise info 'tot_cnt = %', tot_cnt;
Execute 'Select column_name into cn_nm from information_schema.columns ' ||
'where table_name ='|| tbl_nm || ' and ordinal_position = init_loop ' ||
'and data_type not in (''character varying'',''date'',''text''); '
Raise info 'cn_nm = %', cn_nm;
if cn_nm is not null then
Execute 'Update ' || tbl_nm ||
'Set ' || cn_nm = 0 ||
'Where ' || cn_nm is null or cn_nm =' ';
end if;
init_loop = init_loop + 1;
end loop;
End;
End;
$$ language plpgsql;
Issues I am facing
When I pass the Input parameter here, I am getting 0 count
tot_cnt int := (select count(*) from information_schema.columns where table_name = ' || tbl_nm || ');'
For testing purpose I tried hardcode the table name inside proc, I am getting the error amazon invalid operation: value for domain information_schema.cardinal_number violates check constraint "cardinal_number_domain_check"
Is this even possible in redshift, How can I do this logic or any other workaround.
Need Expertise advise here!!
You can simply run an UPDATE over the table(s) using the NVL(cn_nm,0) function
UPDATE tbl_raw
SET col2 = NVL(col2,0);
However UPDATE is a fairly expensive operation. Consider just using a view over your table that wraps the columns in NVL(cn_nm,0)
CREATE VIEW tbl_clean
AS
SELECT col1
, NVL(col2,0) col2
FROM tbl_raw;
The goal, аutopartitioning for 7 days. And after 14 days to delete the old partitions. In this example, everything works. But, when I try to write data of the form :
insert into history_str (itemid, clock, ns, value) values (40,151,3722, '3.0.3');
I get an error
ERROR: syntax error at or near ".3"
LINE 1: ... istory_str_2018_02_07 values (40,151,3.0.3,3722 ...
^
QUERY: INSERT INTO history_str_2018_02_07 values (40,151,3.0.3,3722);
CONTEXT: PL / pgSQL function create_partition_other () line 37 at EXECUTE
Here is the actual code example
CREATE OR REPLACE FUNCTION create_partition() RETURNS trigger AS
$BODY$
DECLARE
partition_name TEXT;
partition_week TEXT;
partitions_names TEXT;
date_search TEXT;
sql_search TEXT;
var_data TEXT;
typeof BOOL;
BEGIN
partition_week := to_char(to_timestamp(NEW.clock),'IW');
RAISE INFO 'Week now: %',partition_week;
partition_name := TG_TABLE_NAME || '_' || to_char(to_timestamp(NEW.clock),'YYYY_MM') || '_' || partition_week;
RAISE INFO 'Master Table: %',TG_TABLE_NAME;
RAISE INFO 'Partit. name: %',partition_name;
IF NOT EXISTS(SELECT relname FROM pg_class WHERE relname = partition_name) THEN
RAISE INFO 'Create table';
EXECUTE 'CREATE TABLE ' || partition_name || ' (check (clock >= ' || quote_literal(NEW.clock) || ' AND clock < ' || quote_literal(NEW.clock + integer '7' * integer '86400') || ')) INHERITS (' || TG_TABLE_NAME || ');';
EXECUTE 'INSERT INTO create_tables_date (name,date) values (' || quote_literal(partition_name) || ',' || quote_literal(to_timestamp(NEW.clock)) || ');';
date_search := quote_literal(date (to_char(to_timestamp(NEW.clock),'YYYY_MM_DD'))-integer '7');
RAISE INFO 'Search data: %',date_search;
sql_search := 'SELECT name FROM create_tables_date WHERE date < ' || date_search || ';';
for partitions_names in EXECUTE sql_search LOOP
IF partitions_names IS NOT NULL THEN
RAISE INFO 'DROP, DELETE: %',partitions_names;
EXECUTE 'DROP TABLE ' || partitions_names || ';';
EXECUTE 'DELETE FROM create_tables_date WHERE name=' || quote_literal(partitions_names) || ';';
END IF;
END LOOP;
END IF;
RAISE INFO 'Value: %',NEW.value;
var_data := 'INSERT INTO ' || partition_name || ' values ' || NEW || ';';
RAISE INFO 'SQL: %',var_data;
EXECUTE var_data;
RETURN NULL;
END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
I found out that the problem when writing the values of being in NEW.value.And after replacing the characters [(), \] with _, the problem was solved.
That is, I redefine before an insert NEW.value
NEW.value := quote_literal(regexp_replace(NEW.value,'[(),\ ]','_','g'));
But this is the case if I try to write to a table with a column value, and if there is no other table, I have to write many identical functions for each table. What is bad.
Can you know why this situation arises with these symbols?
PostgreSQL 9.5.9
You could try USING and expand row with asterisk:
var_data := 'INSERT INTO ' || partition_name || ' values ($1.*);';
RAISE INFO 'SQL: %',var_data;
EXECUTE var_data using new;
Im trying to migrate oracle app to postgresql.
In one of the oracle`s functions i have the next code :
V_Step := 3;
command := 'declare
type tab_data is table of ' || tab_name ||
'%ROWTYPE;
CURSOR raw_data is SELECT * FROM ' || tab_name ||
'_vw;
mydata tab_data;
V_COUNTER integer := 0;
BEGIN
open raw_data;
LOOP
V_COUNTER := V_COUNTER + 1;
FETCH raw_data BULK COLLECT INTO mydata LIMIT ' ||
to_char(current_setting('Gaps.BATCH_SIZE')::bigint) || ';
FORALL i IN 1..mydata.COUNT
INSERT INTO ' || tab_name ||
' VALUES mydata(i);
EXIT WHEN raw_data%NOTFOUND;
END LOOP;
RAISE NOTICE ''V_COUNTER = '%', V_COUNTER;
--commit;
close raw_data;
END;';
V_Step := 4;
RAISE NOTICE '%', command;
V_Step := 5;
EXECUTE command;
We are trying to load into a local postgresql table huge amount of data from remote oracle table (I have link). In oracle using bulk make it faster than regular insert into x select * from. Is there any function in postgresql that is similar to oracle`s bulk ?
code:
CREATE OR REPLACE FUNCTION dp_insert_trigger()
RETURNS trigger AS $dp_insert_trigger$
DECLARE
tablename_1 text;
tablename_2 text;
BEGIN
RAISE WARNING 'Insert into process.';
tablename_1='raw_kafka_input_dt2_1_prt_' || NEW.customer;
tablename_2='raw_kafka_input_dt2_1_prt_' || NEW.customer || '_2_prt_' || NEW.method;
IF not exists(select * from pg_class where relname = tablename_1) THEN
EXECUTE 'CREATE TABLE ' || tablename_1 || '(CHECK (customer=' || NEW.customer || ')) INHERITS (raw_kafka_input_dt2)';
END IF;
IF not exists(select * from pg_class where relname = tablename_2) THEN
EXECUTE 'CREATE TABLE ' || tablename_2 || '(CHECK (customer=' || NEW.customer || ' and method=' || NEW.method || ')) INHERITS (raw_kafka_input_dt2_1_prt_' || NEW.customer || ')';
END IF;
EXECUTE 'INSERT INTO ' || tablename_2 || ' VALUES (($1).*)';
RETURN NULL;
END;
$dp_insert_trigger$ language plpgsql;
CREATE TRIGGER dp_insert_trigger
BEFORE INSERT ON raw_kafka_input_dt2
FOR EACH ROW EXECUTE PROCEDURE dp_insert_trigger();
i try to print some info like:
RAISE WARNING 'Insert into process.';
but nothing output but:
eqbase=# insert into raw_kafka_input_dt2 select * from raw_kafka_input_dt0 limit 1;
INSERT 0 1
eqbase=#
So,i am confused did trigger fired when i insert ?
my pg is 8.2 any suggestion is grateful.
There are many things wrong here with the first being Greenplum doesn't support triggers. Greenplum is an analytics and data warehousing database so you shouldn't be trying to use OLTP features.
I've coded a simple Function using Postgres but keep getting the following:
ERROR: syntax error at or near "$2".
The underlying database is ParAccel and I'm new to both Postgres and ParAccel. I'm using TOAD Data Point as the IDE:
CREATE OR REPLACE FUNCTION GET_NEXT_SURR_KEY(I_SCHEMA_NM VARCHAR, I_TABLE_NM VARCHAR,I_COLUMN_NM VARCHAR,I_POSNEG_FLAG VARCHAR)
RETURNS BIGINT
LANGUAGE PLPGSQL
AS $body$
DECLARE
O_RET_VALUE BIGINT := 0;
V_DYN_SQL VARCHAR(2000) := '';
BEGIN
IF I_POSNEG_FLAG = 'P' THEN
V_DYN_SQL := 'SELECT MAX(' || I_COLUMN_NM || ') + 1 FROM ' || I_SCHEMA_NM || '.' || I_TABLE_NM;
ELSE
V_DYN_SQL := 'SELECT MIN(' || I_COLUMN_NM || ') - 1 FROM ' || I_SCHEMA_NM || '.' || I_TABLE_NM;
END IF;
EXECUTE V_DYN_SQL INTO O_RET_VALUE;
RETURN O_RET_VALUE;
END $body$
I'm using the following example command to execute the Function:
{CALL GET_NEXT_SURR_KEY('some_schema_name','some_table_name','some_column_name','P')};
Can anyone please let me know where I'm messing up?
Thanks in advance.
ParAccel has the concept of IDENTITY fields, not sure why you are not using them.
But in any case, here is how to solve your problem.
BTW, I believe the code you wrote would work on PostgreSQL 9 or above, but ParAccel is using version 7.02 (If I'm not mistaken) which doesn't support SELECT INTO a variable so you need to capture the result with a record and extract the value using a loop (I didn't re-wrote all your function, just the main part)
CREATE OR REPLACE FUNCTION GET_NEXT_SURR_KEY(I_SCHEMA_NM VARCHAR, I_TABLE_NM VARCHAR,I_COLUMN_NM VARCHAR,I_POSNEG_FLAG VARCHAR)
RETURNS BIGINT
LANGUAGE PLPGSQL
AS $body$
DECLARE
O_RET_VALUE BIGINT default 0;
V_DYN_SQL VARCHAR(2000) := '';
_ret_rec record;
BEGIN
V_DYN_SQL := 'SELECT MAX(' || I_COLUMN_NM || ') + 1 as new_id FROM ' || I_SCHEMA_NM || '.' || I_TABLE_NM;
FOR _ret_rec IN EXECUTE V_DYN_SQL
LOOP
O_RET_VALUE := _ret_rec.new_id;
END LOOP;
RETURN O_RET_VALUE;
END $body$
Trust the horse, use sequences - because you are in an OLAP environment you most likely will not get uniqueness violations but if this would be a normal website you would get the same id twice very often. As for your function it works perfectly well - tested it on a random table in my database and no error was given so look for the fault in TOAD.