Dynamic statement in a trigger - sql

I want to run a trigger on Oracle to update several fields of a table with data coming from another table, after an update event. I want to use dynamic SQL statements. Both tables have a lot of fields in common, different by a prefix. The use of "execute immediate" works only if the field I'm updating is explicit. As soon as I use a variable for the field name, it doesn't work. Any idea?
Here is the code :
create or replace TRIGGER AF_UPDATE_PRODUCT_REQUEST
AFTER UPDATE ON PRODUCT_REQUEST
REFERENCING OLD AS OLD NEW AS NEW
FOR EACH ROW
WHEN (old.PREQCOMPLETE=1)
DECLARE
product_fieldname varchar2(100);
counter number(1);
tata number(3);
old_value VARCHAR2(500);
new_value VARCHAR2(500);
BEGIN
tata:=0;
FOR c1 in (SELECT column_name from user_tab_columns WHERE table_name='PRODUCT_REQUEST')
LOOP
old_value:=to_char(:old.PREQDESC2);
new_value:=to_char(:new.PREQDESC2);
IF old_value<>new_value THEN
product_fieldname:=replace(c1.column_name,'PREQ','PU');
select count(*) into counter from user_tab_columns WHERE table_name='PRODUCT' and column_name=product_fieldname;
IF counter=1 THEN
tata:=tata+1;
/*execute immediate 'update product set '|| product_fieldname ||'=:new.'|| c1.column_name ||' where pupname=:old.preqpname';*/
/*execute immediate 'update product set pushelflife=16 where pupname=:d3' using :old.preqpname;*/
IF product_fieldname='PUSHELFLIFE' THEN
/*execute immediate 'update product set pushelflife=:d2 where pupname=:d3' using 15,:old.preqpname;*/
execute immediate 'update product set :d1=:d2 where pupname=:d3' using product_fieldname,15,:old.preqpname;
END IF;
END IF;
END IF;
END LOOP;
EXCEPTION
WHEN OTHERS THEN NULL;
END;

You cannot pass in object or column names as bind variables to a dynamic SQL statement. You would have to construct the dynamic SQL statement using those column names. Something like
execute immediate 'update product ' ||
' set ' || product_fieldname || ' = :val '
' where pupname = :key'
using 15, :old.preqpname

Related

Oracle stored procedure: FOR LOOP over a column from a variable

I want to save a list of column values into the variable input_cols and then loop over the values
CREATE OR REPLACE PROCEDURE map_values (mapping_table VARCHAR2(64)) AS
TYPE col_names IS TABLE OF VARCHAR2(64);
input_cols col_names;
BEGIN
EXECUTE IMMEDIATE
'SELECT COLUMN_NAME FROM SYS.DBA_TAB_COLUMNS
WHERE TABLE_NAME = '' ' || mapping_table || ' '' '
BULK COLLECT INTO (input_cols);
FOR in_col IN input_cols
LOOP
dbms_output.put_line ('test');
END LOOP;
END;
I am getting the error
PLS-00103: Encountered the symbol "LOOP" when expecting one of the following: * & - + / at mod remainder rem .. <an exponent (**)> ||
Although you can construct dynamic queries by concatenating values, it's generally better to use bind variables where possible, for example:
execute immediate
'select column_name from user_tab_columns where table_name = :b1'
bulk collect into input_cols
using p_table;
I recommend getting into the habit of anchoring types in your code to the corresponding database object, when there is one. For example, this:
mapping_table dba_tab_columns.table_name%type
instructs the compiler to look up the type of dba_tab_columns.table_name and use that. However, I would generally avoid the dba_ views in procedures like this and stick to user_ views, e.g. user_tab_columns, to limit them to objects you own. If you must use dba_ views, you should also include the table owner, as there may be more than one table with the same name.
I also prefer to name my parameters in a way that separates them from column names etc. There are various conventions (camelCase, prefixing with i_ for in or p_ for parameter, prefixing with the procedure name e.g. map_values.mapping_table), so pick one you like.
Putting that together, you get something like this:
create or replace procedure map_values
( p_table user_tab_columns.table_name%type )
as
type col_names is table of user_tab_columns.column_name%type;
input_cols col_names;
begin
execute immediate
'select column_name from user_tab_columns where table_name = :b1 order by column_id'
bulk collect into input_cols
using p_table;
for i in 1..input_cols.count loop
dbms_output.put_line(input_cols(i));
end loop;
end map_values;
Or, if you don't specifically need a collection and just want to loop through a result set:
create or replace procedure map_values
( p_table user_tab_columns.column_name%type )
as
columns_cur sys_refcursor;
colname user_tab_columns.column_name%type;
begin
open columns_cur for
'select column_name from user_tab_columns where table_name = :b1 order by column_id'
using p_table;
loop
fetch columns_cur into colname;
exit when columns_cur%notfound;
dbms_output.put_line(colname);
end loop;
close columns_cur;
end;
As Koen pointed out in the comments, though, there is no need for dynamic SQL in this example, so a much simpler version could be just:
create or replace procedure map_values
( p_table user_tab_columns.column_name%type )
as
begin
for r in (
select column_name from user_tab_columns
where table_name = p_table
order by column_id
)
loop
dbms_output.put_line(r.column_name);
end loop;
end map_values;
Word of advice: use a tool like SQL Developer to create your procedures. They show the compilation errors in a much clearer way. If you're new to PL/SQL, start with the very basics (empty procedure), compile, fix error if any and add code. There are 3 blocking issues in your code - debugging that is pretty hard.
I added a comment for each of the errors
create or replace PROCEDURE map_values
(mapping_table VARCHAR /* just define the datatype, not the precision */
)
AS
TYPE col_names IS TABLE OF VARCHAR2(64) INDEX BY BINARY_INTEGER;
input_cols col_names;
BEGIN
EXECUTE IMMEDIATE
'SELECT COLUMN_NAME FROM SYS.DBA_TAB_COLUMNS
WHERE TABLE_NAME = ''' ||mapping_table|| ''' '
BULK COLLECT INTO input_cols; /* no brackets needed */
dbms_output.put_line ('test:');
FOR in_col IN 1 .. input_cols.COUNT /* this is not a implicit cursor but a collection - you need to iterate over it.*/
LOOP
dbms_output.put_line ('test:'||input_cols(in_col));
END LOOP;
END;
/
Please do not use dynamic SQL for this use case, static SQL is sufficient and will be more efficient.
For looping, you have to use indexes and get the element from collection by index. There is no functionality that will enable you to directly assign collection element to a variable (like you are trying).
create or replace procedure map_values(mapping_table sys.dba_tab_columns%table_name)
as
type col_names is table of sys.dba_tab_columns.column_name%type;
input_cols col_names;
begin
select column_name
bulk collect into input_cols
from sys.dba_tab_columns
where table_name = mapping_table;
for i in 1 .. input_cols.count
loop
dbms_output.put_line(input_cols(i));
end loop;
end;

Using variables in oracle as table names for insert command oracle

Trying to create a procedure that will either insert or update a certain table that its name is stored in another table with more info.
CREATE OR REPLACE PROCEDURE LIMPAR_TAB_proc IS
--stmt VARCHAR2(1000);
n_tab sii_bck_cfg_tab.nome_tab%type;
prefix sii_bck_cfg_tab.pref_tab_bck%type;
max_reg sii_bck_cfg_tab.max_reg_bck%type;
id_fk sii_bck_cfg_tab.id_bck_cfg_tab%type;
n_tab2 sii_bck_tab.nome_tab%type;
testes VARCHAR2(500);
CURSOR c1 IS
SELECT ID_BCK_CFG_TAB,Nome_tab, pref_tab_bck, max_reg_bck FROM
sii_bck_cfg_tab WHERE desativado_em IS NULL OR desativado_em<=SYSDATE AND
n_dias_reten>0 ORDER BY criado_em;
CURSOR c2 IS
SELECT sii_bck_tab.ID_BCK_CFG_TAB , sii_bck_tab.nome_tab from
sii_bck_tab,sii_bck_cfg_tab WHERE
sii_bck_cfg_tab.id_bck_cfg_tab=sii_bck_tab.id_bck_cfg_tab and dt_fecho is
NULL ;
BEGIN
OPEN c1;
LOOP
FETCH c1 INTO id_fk,n_tab,prefix,max_reg;
EXIT WHEN c1%NOTFOUND;
DBMS_OUTPUT.PUT_LINE('Nome Tabela = ' || id_fk ||' '|| n_tab ||' '|| prefix
||' '|| max_reg);
OPEN c2;
LOOP
FETCH c2 INTO id_fk, n_tab2;
EXIT WHEN c2%NOTFOUND;
DBMS_OUTPUT.PUT_LINE('chave aqui = ' || id_fk || n_tab2);
IF c2%FOUND THEN
testes:= 'INSERT INTO ' || n_tab2 || 'select * from ' || n_tab;
EXECUTE IMMEDIATE testes;
END IF;
END LOOP;
CLOSE c2;
END LOOP;
CLOSE c1;
so i will try to explain my final objective, i want to go through my cursor1 and when i find a table that passes through the verification then i go into my cursor2. During my second loop i will want to verify if there is a table associated with a table on my cursor1 (not implemented ), then if i find one that is not associated i will need to create one with the same fields as the original(which is why im trying to save the table names in a variable). In case it exists and its dt_fim(date end) is null then i will need to insert all the data from the table from cursor1 (n_tab) into the table found on cursor2(n_tab2).
I will try to explain any doubts further, its still confusing to me, just getting started.
Thank you for any advice/help.
This is the right syntax, but not recommended for such a simple operation
testes:= 'INSERT INTO ' || n_tab2 ||' SELECT * FROM ' || n_tab;
EXECUTE IMMEDIATE testes;
Because It is preferable(and safer) to explicitly specify the column names in an insert, for which you need extra blocks if you want to do it dynamically.
INSERT INTO tab2(col1,col2,col3) SELECT col1,col2,col3 FROM tab;
By the way, any reason why you've put table names in variable instead of doing a direct insert?
Try this below block to pass tablename as variables:
declare
table_1 varchar2(10):='N_tab';
table_2 varchar2(10):='N_tab2';
test varchar2(1000);
begin
test:= 'INSERT all into ' || table_2 || ' SELECT * FROM ' ||table_1;
EXECUTE IMMEDIATE test;
dbms_output.put_line (test);
end;

oracle , sql , trigger

CREATE TRIGGER "CPI"."TRGPRODOFFRPRICE"
AFTER UPDATE or INSERT ON CPI_LOADER_EXEMPTIONS
FOR EACH ROW
DECLARE
table_name varchar(32);
BEGIN
if substr(:new.SID_ID,1,3) = 'POP' then
table_name := 'PROD_OFFR_PRICE_CHARGE';
else
table_name := 'PROD_OFFR_PRICE_ALTERATION';
end if;
execute immediate 'UPDATE :1 set CPI_EXEMPTION=:2 WHERE SID_ID=:3' using table_name, :new.FLAG, :new.SID_ID;
END;
Based on comments i edited into this form, which looks better, but when trying to insert value into table which launches trigger i am getting error 'invalid table name', any idea what is causing this ? wrong tab var declaration possibly?
Try this (real PL/SQL):
CREATE TRIGGER trgSample
AFTER UPDATE or INSERT ON EXEMPTIONS
FOR EACH ROW
DECLARE
sid varchar(10);
sidVal varchar(100);
table_name varchar(32);
flag int;
BEGIN
sid := substr(:new.SID_ID,1,3);
flag := :new.FLAG;
sidVal := :new.SID_ID;
if sid = 'POP' then
table_name := 'PROD_VAL';
else
table_name := 'PROD_VAL2';
end if;
execute immediate 'UPDATE ' || table_name
|| ' set EXEMPTION=sidVal WHERE SID_ID=:x' using sidVal;
END;
AFter all only these changes were neccessary:
'change var to varchar2 for table_name:'
only slight update on execute statement:
'execute immediate 'UPDATE :1 set CPI_EXEMPTION=:2 WHERE SID_ID=:3' using table_name, :new.FLAG, :new.SID_ID;'
thanks for all comments, they ´ve helped me a lot. And need to say a i learn a lot.

plsql: execute query: invalid identifier

Why can't I execute the stmt query?
It shows tabb is invalid identifier.
TYPE tab_row IS TABLE OF tab_name%ROWTYPE;
tabb tab_row;
TYPE cur_ref Is Ref Cursor;
c cur_ref;
stmt_string Varchar2(1000);
stmt Varchar2(1000);
Begin
stmt_string := 'Select * from ' || tab1 || ' mft
Where mft.mfmt_id IS NOT NULL
and mft.MFMT_FLAG IS NULL';
Open c For stmt_string;
Fetch c bulk collect into tabb;
Close c;
stmt:= 'Update '|| tab || ' m
Set m.mfmt_mat_bnr = tabb(i).mfmt_mat_bnr,
m.mfmt_mat_type = tabb(i).mfmt_mat_type,
m.mfmt_be_seg = tabb(i).mfmt_be_seg
Where m.mfmt_id = tabb(i).mfmt_id';
For i in 1..tabb.count loop
Execute Immediate stmt;
End loop;
End;
I have not seen where is tabb being declared. As you mentioned in your comment, it is a collection.
From your code I can understand you are looping through the values of this collection. This means these values are variables i.e. changing values every iteration. Therefore, you need to use them as such.
You current dynamic statement, this values are static string. This Set m.mfmt_mat_bnr = tabb(i).mfmt_mat_bnr, will result in the same string every iteration. Hence, you need to change that to Set m.mfmt_mat_bnr = '|| tabb(i).mfmt_mat_bnr||', ...etc
I can rewrite your code as follows:
TYPE tab_row IS TABLE OF tab_name%ROWTYPE;
tabb tab_row;
TYPE cur_ref Is Ref Cursor;
c cur_ref;
stmt_string Varchar2(1000);
stmt Varchar2(1000);
Begin
stmt_string := 'Select * from ' || tab1 || ' mft
Where mft.mfmt_id IS NOT NULL
and mft.MFMT_FLAG IS NULL';
Open c For stmt_string;
Fetch c bulk collect into tabb;
Close c;
stmt:= 'Update '|| tab || ' m
Set m.mfmt_mat_bnr = '||tabb(i).mfmt_mat_bnr||',
m.mfmt_mat_type = '||tabb(i).mfmt_mat_type||',
m.mfmt_be_seg = '||tabb(i).mfmt_be_seg||'
Where m.mfmt_id = '||tabb(i).mfmt_id;
For i in 1..tabb.count loop
Execute Immediate stmt;
End loop;
End;
Since this code is not tested, one thing I strongly recommend to confirm first. Is that tabb is correctly declared within the scope.
If the problem is in your loop where you execute your statement, maybe try using EXECUTE IMMEDIATE .. USING statement?
The syntax is:
EXECUTE IMMEDIATE <SQL Command>
[INTO <variable list>]
[USING <bind variable list>];
You might try the code below, then:
DECLARE
-- your tab1 may contain some insignificant columns with values (especially huge ones e.g. CLOB/BLOB)
-- which will extremely affect procedure's efficiency, so it's a better practice to select only those values that we need
TYPE t_tab_rec IS RECORD (
mfmt_mat_bnr tab_name.mfmt_mat_bnr%TYPE
,mfmt_mat_type tab_name.mfmt_mat_type%TYPE
,mfmt_be_seg tab_name.mfmt_be_seg%TYPE
,mfmt_id tab_name.mfmt_id%TYPE
);
TYPE t_tab_arr IS TABLE OF t_tab_rec;
tabb t_tab_arr := NEW t_tab_arr();
TYPE ref_cur IS REF CURSOR RETURN t_tab_rec; -- to assure cursor is returning t_tab_rec type records
c cur_ref;
stmt_select VARCHAR2(1000);
stmt_upadte VARCHAR2(1000);
BEGIN
stmt_select :=
'SELECT
mft.mfmt_mat_bnr
,mft.mfmt_mat_type
,mft.mfmt_be_seg
,mft.mfmt_id
FROM
'||tab1||' mft
WHERE
mft.mfmt_id IS NOT NULL
AND mft.mfmt_flag IS NOT NULL';
OPEN c FOR stmt_select;
FETCH c BULK COLLECT INTO tabb;
CLOSE c;
stmt_upadte :=
'UPDATE '||tab||'
SET mfmt_mat_bnr = :p_mfmt_mat_bnr
,mfmt_mat_type = :p_mfmt_mat_type
,mfmt_be_seg = :p_mfmt_be_seg
WHERE
mfmt_id = :p_mfmt_id';
FOR idx IN tabb.FIRST .. tabb.LAST
LOOP
EXECUTE IMMEDIATE stmt_update USING tabb(idx).mfmt_mat_bnr, tabb(idx).mfmt_mat_type, tabb(idx).mfmt_be_seg, tabb(idx).mfmt_id;
END LOOP;
END;
/

Oracle SQL: Update with variable table name and fixed column name

I am a beginner with SQL and have a problem:
I have a DB with a big number of tables. Some of the tables have a column with the name "lab".
In this colums are values I need to be changed.
So I managed to get the names of the tables via
SELECT CNAME,TNAME FROM SYSTEM.COL WHERE CNAME = 'LAB';
And I know my update command
update TNAME set LAB='VALUE' WHERE LAB='OLDVALUE'
But I can not manage to connect both statements via a variable TNAME or something. I tried to use execute immediate, but that did me no good.
If its Oracle, something like this should do it:
BEGIN
FOR cur_tabs_cols IN ( SELECT CNAME,TNAME FROM SYSTEM.COL WHERE CNAME = 'LAB'; )
LOOP
EXECUTE IMMEDIATE 'UPDATE ' || cur_tabs_cols.TNAME || ' SET LAB = ''VALUE'' WHERE LAB = ''OLDVALUE''';
END LOOP;
COMMIT;
END;
You would need to write pl/sql for this.
The first thing is, please don't use SYSTEM.COL. Instead use the data dictionary view USER_TAB_COLS or USER_TAB_COLUMNS. (or ALL_TAB_COLS if in other schema)
EXECUTE IMMEDIATE would be what you want here.
BEGIN
FOR i IN (SELECT table_name
FROM user_tab_cols
WHERE column_name = 'LAB')
LOOP
EXECUTE IMMEDIATE
'UPDATE ' || i.table_name || ' set LAB = :value where LAB = :oldvalue'
USING 'value', 'oldvalue';
END LOOP;
END;
You can (and should) use a bind variable for value and oldvalue, just not for the table name.