PL SQL Procedure Exercise - stored-procedures

I'm not really sure how to approach this question.. I understand the basic syntax of writing a procedure. This is an exercise for a beginner database class (which seems to be at a level way above beginner)
Create a procedure to place a purchase order for a specified date based on data in the inventory report table.
Name the procedure placeorder [procedure name is important].
The procedure should take one parameter: inputDate (use the datatype of the PODate column in PURCHASEORDERS). The input date format accepted should be: 'DD-MON-YYYY', e.g., 01-JAN-2017.
For each raw material in the Inventory Report table (where: ReportDate matches inputDate), make a separate entry in the PURCHASEORDERS table for a next-day delivery order and a same-day delivery order (each raw material may generate up to 2 inserts).
Corresponding order type should be either next_day or same_day
Only make an entry (insertion) in PURCHASEORDERS if needed, i.e., if
an entry exists for a raw material and report date combination in the
Inventory Report table. If the Inventory Report for a day (e.g.,
30-NOV-2017) has a 0 value for the ordersameday attribute, it means
no same_day order is needed.
If no order is needed for the provided input date (i.e., no order at all across ALL raw materials), raise an application error with a message: “no order needed” (see Triggers and Procedures tutorial on D2L for an example of how to raise this error). You can use any suitable error-number.  Your procedure should leave the “Price” (for the purchase order) empty (i.e., it can remain null). Assume it’ll be populated later.
Thanks!

Sorry this is what i've written so far.. #kara
CREATE OR REPLACE PROCEDURE placeorder (inputDate in DATE)
AS
new_inputDate PURCHASEORDERS.PODate%TYPE;
new_orderType PURCHASEORDERS.ORDERTYPE%TYPE;
c_orderSameDay INVENTORYREPORT.ORDERSAMEDAY%TYPE;
c_orderNextDay INVENTORYREPORT.ORDERNEXTDAY%TYPE;
CURSOR C1 IS
SELECT REPORTDATE INTO inputDate FROM dual;
SELECT ir.itemId, ir.ORDERSAMEDAY, ir.ORDERNEXTDAY FROM INVENTORYREPORT
WHERE
ir.REPORTDATE = inputDate;
BEGIN
OPEN C1;
WHILE C1%FOUND LOOP
FETCH C1 INTO new_inputDate, new_orderType, c_orderSameDay, c_orderNextDay;
IF c_orderSameDay > 0
THEN INSERT INTO PURCHASEORDERS (new_orderType) VALUES (orderSameDay);
ELSE <application error>;
END IF;
IF c_orderNextDay > 0
THEN INSERT INTO PURCHASEORDERS (new_orderType) VALUES (orderNextDay);
ELSE <application error>;
END IF;
END LOOP;
CLOSE C1;
END;
/

#kara I added to the if statements but still getting a couple errors when trying to compile the procedure. It this doing what it's supposed to be doing?
CREATE OR REPLACE PROCEDURE placeorder (inputDate in DATE)
AS
new_inputDate PURCHASEORDERS.PODate%TYPE;
new_orderType PURCHASEORDERS.ORDERTYPE%TYPE;
c_orderSameDay INVENTORYREPORT.ORDERSAMEDAY%TYPE;
c_orderNextDay INVENTORYREPORT.ORDERNEXTDAY%TYPE;
CURSOR C1 IS
SELECT REPORTDATE INTO inputDate FROM dual;
SELECT ir.itemId, ir.ORDERSAMEDAY, ir.ORDERNEXTDAY FROM INVENTORYREPORT
WHERE
ir.REPORTDATE = inputDate;
BEGIN
OPEN C1;
WHILE C1%FOUND LOOP
FETCH C1 INTO new_inputDate, new_orderType, c_orderSameDay, c_orderNextDay;
IF c_orderSameDay > 0
THEN INSERT INTO PURCHASEORDERS (new_orderType) VALUES (orderSameDay);
ELSE INSERT INTO PURCHASEORDERS (new_orderType) VALUES ('no order needed');
END IF;
IF c_orderNextDay > 0
THEN INSERT INTO PURCHASEORDERS (new_orderType) VALUES (orderNextDay);
ELSE INSERT INTO PURCHASEORDERS (new_orderType) VALUES ('no order needed');
END IF;
FETCH C1 INTO new_inputDate, new_orderType, c_orderSameDay, c_orderNextDay;
END LOOP;
CLOSE C1;
COMMIT;
END placeorder;
/

There are a bunch of examples how to create a procedure. Here a small one:
CREATE OR REPLACE PROCEDURE ProcName (paraName IN VARCHAR2)
AS
myStringVariable VARCHAR2 (4000);
myDateVariable DATE;
BEGIN
SELECT orderDateAsString
INTO myStringVariable
FROM orders
WHERE orderId = paraName;
myDateVariable := TO_DATE (myStringVariable, 'dd.mm.yyyy HH24:MI:SS'); -- '13.03.2018 23:59:59'
dbms_output.put_line('My date: ' || myStringVariable);
END;
And some example-code, to call the procedure:
begin
ProcName('1234');
end;
But you should look at your exercise first and think about you tasks. I think, this is what you got to do:
Create a Table.
Create Insert-Statements for the table
Create some PL/SQL-code which performs the insert with conditions.
move the PL/SQL-code to a procedure.
Call the procedure with a PL/SQL-block.
Explaination for you edits
You should build your code step-by-step if. You got multiple errors.
Check you Cursor. The Select-statement is not valid.
You wrote two selects.
You do not use new_inputdate. Why did you define it and what should it do?
Run you SELECT-statement alone without your code. if you like your data you can put it in an cursor.
Example with your code:
CREATE OR REPLACE PROCEDURE placeorder (inputDate IN DATE)
AS
-- new_inputDate PURCHASEORDERS.PODate%TYPE; --you don't use this one
new_orderType PURCHASEORDERS.ORDERTYPE%TYPE;
c_orderSameDay INVENTORYREPORT.ORDERSAMEDAY%TYPE;
c_orderNextDay INVENTORYREPORT.ORDERNEXTDAY%TYPE;
-- you mixed up your cursors. seems like you didn't try the select-statement alone..
CURSOR C2
IS
SELECT ir.itemId, ir.ORDERSAMEDAY, ir.ORDERNEXTDAY
FROM INVENTORYREPORT
WHERE ir.REPORTDATE = inputDate;
BEGIN
OPEN C2;
WHILE C2%FOUND
LOOP
FETCH C2 INTO new_orderType, c_orderSameDay, c_orderNextDay;
NULL; -- Do something
END LOOP;
CLOSE C2;
END;
/
Think about what you want to do in the loop. Build a small skript with the variables and think about what it should do:
Example-Skript of you Loop-content:
DECLARE
-- new_inputDate PURCHASEORDERS.PODate%TYPE; --you don't use this one
new_orderType NUMBER := 0;
c_orderSameDay NUMBER := 1;
c_orderNextDay NUMBER := 2;
BEGIN
-- this is what you're doing in you loop.
IF c_orderSameDay > 0 -- check if c_orderSameDay is greater than 0? In Oracle you use 'NULL' as empty value. Perhaps it should be 'c_orderSameDay IS NOT NULL'
THEN
INSERT INTO PURCHASEORDERS (new_orderType) -- you perform your insert.
VALUES (orderSameDay);
ELSE
RAISE; -- you raise an exception? this means c_orderSameDay has alsway to be set.
END IF;
IF c_orderNextDay > 0
THEN
INSERT INTO PURCHASEORDERS (new_orderType)
VALUES (orderNextDay);
ELSE
RAISE; -- you raise an exception? this means c_orderNextDay has alsway to be set.
END IF;
END;

Related

create columns in existing table and populate data using procedure

I will try to keep the query as short as possible. This involves 2 tables - lets call them staging_data and audit_data. STAGING_DATA has 3 columns:
user_no with data type number,
update_date_time with data type as date in DD-MON-YYYY HH24:MI:SS format
status_code which is varchar(1).
audit_data table also has the same 3 columns. The ask is to add 3 columns to audit_data table
seq_no (which will be unique to every user),
active_from (date type without the time format)
active_to (date type without the time format).
There is a procedure that inserts data from staging_data to audit_data.
Sample of the table audit_data
That data in audit table should look like :
For the next record for user_no 523(lets assume update_date_time is '23-Nov-2020 10:20') seq_no becomes 3, active_from_date becomes '23-Nov-2020', active_to becomes 31-Dec-99 and the active_to of user_no 523 with seq_no 2 becomes '22-Nov-2020'. So the data should look like this :
Highlighted the 3rd record which will be added later in light green.
So here goes my solution : I suggested to use row_number() over(partition by user_no) analytical function to get seq_no for each user. I wanted to create a view based on that but Boss doesn't want a view. He strictly wants to use a procedure. Procedure should check if the user_no exists (in this example 523). If exists then seq_no increases and active_to of the previous record for 523 changes to latest active_from - 1 date. I will be honest - I have no clue how to achieve this in Procedure. I understand I can create a cursor with the query I had in my mind for the view. But to add seq_no and change active_to date is something that has puzzled me. Can anyone please guide me in right direction/s? Also I apologise in advance if I have left out any other details. Its midnight here now and after 8 hours of racking my brain on this I am very hungry!
edit 11th Mar : here is the code for the procedure I wrote to insert data into the audit table for situation when a particular user_no has no record in audit table :
create or replace procedure test_aud IS
user_found_audit number;
lv_user_no AUDIT_DATA.user_no%TYPE;
cursor member_no is select distinct user_no from STAGING_DATA;
begin
open member_no;
loop
fetch member_no into lv_user_no;
exit when member_no%notfound;
select count(*) into user_found_audit from AUDIT_DATA where user_no = lv_user_no;
if user_found_audit = 0 then
insert into AUDIT_DATA(user_no, update_date_time,status_code, seq_no, last_update_date, active_from, active_to)
select user_no, update_date_time,status_code,row_number() over(partition by user_no order by UPDATE_DATE_TIME) as seqno,
to_char(trunc(update_date_time),'DD-MON-YYYY'),
to_char(trunc(update_date_time),'DD-MON-YYYY'),
lead(to_char(trunc(update_date_time)-1,'DD-MON-YYYY'),1,'31-DEC-99') over(PARTITION BY user_no ORDER BY UPDATE_DATE_TIME) from STAGING_DATA where user_no = lv_user_no;
commit;
else
dbms_output.put_line(lv_user_no||' exists in audit table');
-- to code the block when user_no exists, involves an update and insert
end if;
end loop;
close member_no;
end;
/
Well you need to collect a couple things. The latest stage row and the latest audit row. Then it is just a matter of generating the new audit information and updating the previous latest one. The following makes a couple simplifying assumptions:
Only the latest stage data for a given user_no needs processed as
all prior have been processed, However it does not assume the stage
table has been cleared.
The sequencing of 'Y' and 'N' status_codes are properly order in
that manner. In fact it does not even check the value.
It need not concern itself with the inherent race condition. The
condition is derives from seq_no being generated as Max()+1. This
structure virtually guarantees a duplicate will eventually be
created.
The nested procedure "establish_audit" does all the actual work. The rest are just supporting characters, including a couple just for debug purpose. See fiddle.
create or replace
procedure generate_stage_audit(user_no_in staging_data.user_no%type)
as
k_end_of_time constant date := date '9999-12-31';
l_latest_user_stage staging_data%rowtype;
l_latest_user_audit audit_data%rowtype;
procedure establish_audit
is
begin
insert into audit_data(user_no, update_date_time, status_code
,seq_no, active_from, active_to)
select l_latest_user_stage.user_no
, l_latest_user_stage.update_date_time
, l_latest_user_stage.status_code
, coalesce(l_latest_user_audit.seq_no,0) + 1
, trunc(l_latest_user_stage.update_date_time)
, k_end_of_time
from dual;
update audit_data
set active_to = trunc(l_latest_user_stage.update_date_time - 1)
where user_no = l_latest_user_audit.user_no
and seq_no = l_latest_user_audit.seq_no;
end establish_audit;
procedure retrieve_latest_stage
is
begin
select *
into l_latest_user_stage
from staging_data
where (user_no, update_date_time) =
( select user_no, max(update_date_time)
from staging_data
where user_no = user_no_in
group by user_no
);
end retrieve_latest_stage;
procedure retrieve_latest_audit
is
begin
select *
into l_latest_user_audit
from audit_data
where (user_no, seq_no) =
( select user_no, max(seq_no)
from audit_data
where user_no = user_no_in
group by user_no
);
exception
when no_data_found then
null;
end retrieve_latest_audit;
---- for debugging ---
procedure show_stage
is
begin
dbms_output.put_line('-------- Stage Row -------');
dbms_output.put_line(' user_no==>' || to_char(l_latest_user_stage.user_no));
dbms_output.put_line('update_date_time==>' || to_char(l_latest_user_stage.update_date_time));
dbms_output.put_line(' status_code==>' || to_char(l_latest_user_stage.status_code));
end show_stage;
procedure show_audit
is
begin
dbms_output.put_line('-------- Audit Row -------');
dbms_output.put_line(' user_no==>' || to_char(l_latest_user_audit.user_no));
dbms_output.put_line('update_date_time==>' || to_char(l_latest_user_audit.update_date_time));
dbms_output.put_line(' status_code==>' || to_char(l_latest_user_audit.status_code));
dbms_output.put_line(' seq_no==>' || to_char(l_latest_user_audit.seq_no));
dbms_output.put_line(' active_from==>' || to_char(l_latest_user_audit.active_from));
dbms_output.put_line(' active_to==>' || to_char(l_latest_user_audit.active_to));
end show_audit;
begin -- the main event
retrieve_latest_stage;
show_stage;
retrieve_latest_audit;
show_audit;
establish_audit;
end generate_stage_audit;
A couple warnings:
It seems you may be tempted to use string data type for the audit
columns Active_Form and Active_to as you are trying to declare then
"date type without the time". However there is no such data type in
Oracle; time is part of all dates. Do not do so, store them as
standard dates. (Note Dates are not stored in any format, but an
internal structure. Formats are strictly a visual representation).
Just throwaway the time with the format on the query or by setting
nls_date_format.
You may be tempted to convert call this through a trigger. Do not,
it will likely result in an "ORA-04091: Table is mutating"
exception.

provide column data type in DBSMS_SQL.DEFINE_COLUMN oracle 12C

I have to copy data from one table to another with below two conditions
table names will be known at run time
records need to be copied one at a time so that modifications can be done in column values when required
I have created a procedure to to do this through dynamic query. Since the column list is not known already I am not able to declare a rowtype variable. I saw an example of DBMS_SQL where you can define the columns for select clause. Below is the format
DBMS_SQL.DEFINE_COLUMN(cursor_var,position,column_var);
Problem here is that in all the examples I found the column_var were already declared. However in my case I will get to know the no of columns that will be in cursor sql and their data type at run time. so I need to find a way to pass the data type of "column_var" as part of DBMS_SQL.DEFINE_COLUMN. Is there a way to do that? Is there a better way?
Below is just a sample code
CREATE OR REPLACE PROCEDURE pr_test (P_TABLE_NAME IN VARCHAR2)
IS
V_SQL VARCHAR2(500);
SRC_CUR INT;
DEST_CUR INT;
TYPE COL_DTL_TYPE IS RECORD
(
COLUMN_ID INT,
COLUMN_NAME VARCHAR2(250),
DATA_TYPE VARCHAR2(250),
DATA_LENGTH INT
);
COL_DTL_REC COL_DTL_TYPE;
TYPE TBL_COL_LIST_TYPE IS TABLE OF COL_DTL_TYPE;
TBL_COL_LIST TBL_COL_LIST_TYPE;
V_CNT INT := 0;
BEGIN
V_SQL := 'SELECT * FROM ' || P_TABLE_NAME;
SRC_CUR := DBMS_SQL.OPEN_CURSOR;
DBMS_SQL.PARSE(SRC_CUR,V_SQL,DBMS_SQL.NATIVE);
TBL_COL_LIST := TBL_COL_LIST_TYPE();
FOR COL_DTL_REC IN (
SELECT COLUMN_ID,COLUMN_NAME,DATA_TYPE,DATA_LENGTH
FROM ALL_TAB_COLUMNS WHERE TABLE_NAME =P_TABLE_NAME
)
LOOP
V_CNT := V_CNT + 1;
TBL_COL_LIST.EXTEND;
TBL_COL_LIST(V_CNT) := COL_DTL_REC;
-- Here is where I am stuck and not able to give column data type
DBMS_SQL.DEFINE_COLUMN(SRC_CUR,V_CNT,COL_DTL_REC.COLUMN_NAME COL_DTL_REC.DATA_TYPE , COL_DTL_REC.DATA_LENGTH)
END LOOP;
END;
copying to destination table will come later.

Inserting values in to netezza using a stored procedure

I'm working with netezza database and have a requirement to insert a Y flag for stores in California. I wrote the below procedure
CREATE OR REPLACE PROCEDURE MY_NEW_PROCEDURE() RETURNS BOOL
EXECUTE AS OWNER LANGUAGE NZPLSQL AS
BEGIN_PROC
DECLARE
rec RECORD;
BEGIN
FOR rec in SELECT * from test_table
LOOP
if rec.state_code ='CA'
EXECUTE IMMEDIATE 'INSERT INTO test_table (california_stores)' || 'values('y')';
END LOOP;
END;
END_PROC;
when I call the procedure using call MY_NEW_PROCEDURE() I get an error at line EXECUTE IMMEDIATE. I'm not sure what change I need to make here.
Don't know about netezza, but below is the procedure I used to test this (using SQL developer).
Works fine for me, although it would make more sense to update the row to set california_stores to 'Y'rather than insert a new row with california_stores = 'Y' for each calafornia store that you have.....
CREATE OR REPLACE PROCEDURE "MY_NEW_PROCEDURE" as
rec test_table2%rowtype;
BEGIN
FOR rec in (SELECT * from test_table2) LOOP
if rec.state_code = 'CA' then
EXECUTE IMMEDIATE 'INSERT INTO test_table2 (california_stores)' || 'values(''y'')';
end if;
END LOOP;
END;
You didn't post the second error, but it looks to me like your insert statement isn't going to do what you want anyway. If the rec variable contains the attribute state_code and you're inserting a single value to test_table then the record will simply be empty except for a 'Y' in california_stores.
I'm going to guess that you're getting an error now either because of the spacing in the insert statement insert into test_table (california_stores)values('y') or because you didn't terminate the execute statement with a semicolon. The plsql for that line should be
execute immediate 'insert into test_table (california_stores) values (''y'');';

PL/SQL:, How to pass variable into SELECT statent and return all rows of results

I am using an oracle database. I am used to SQL server but not familiar with PL/SQL for the Oracle database.
How do I Set a variable that returns all the rows that contain the value of that variable: I am lost, I tried to understand, but it is not making sense to me. This si a recent attempt I made to to this.
DECLARE date1 varchar(40);
Begin
Select '''07/31/2013_09%''' into :date1 from dual;
End;
/
print date1
Begin
Select * from TABLE1 where start_time LIKE date1;
End;
/
I should get all the rows returned from this.
Thank you for your help.
This might help you get started:
create table table1 (
start_time varchar2(10),
foo number
);
insert into table1 values ('xyz', 1);
insert into table1 values ('wxy', 2);
insert into table1 values ('abc', 3);
create type table1_obj as object (
start_time varchar2(10),
foo number
);
/
create type table1_tab as table of table1_obj;
/
declare
v table1_tab;
begin
select table1_obj(start_time, foo) bulk collect into v
from table1 where start_time like '%x%';
end;
/
You have to create a parametrized cursor and pass that date as parameter to that cursor as below.
CREATE or REPLACE procedure proc1(p_date date)
as
CURSOR C1 (date1 date)is
SELECT * from TABLE1 where start_time LIKE date1;
BEGIN
FOR i in c1(p_dat)
LOOP
.......
END LOOP;
END;
It looks like you're missing the understanding of several basic building blocks:
You need a PL/SQL data structure where you'll save the data set queried from database table: PL/SQL Collections and Records. See especially nested tables and record variables.
You query the database with PL/SQL Static SQL. See especially SELECT INTO statements. In this case there is no need for dynamic SQL.
But of course it is bit harder to get a set of rows out of the database than only one row. Here the keywords are: SELECT INTO Statement with BULK COLLECT Clause. Note that depending on your query and table size bulk collection will potentially exhaust your server's memory (by loading millions of rows).
Here is an example that should give you a kickstart:
create table so26 (
day date,
event varchar(10)
);
insert all
into so26 values(trunc(sysdate - 1), 'foo1')
into so26 values(trunc(sysdate - 1), 'foo2')
into so26 values(trunc(sysdate - 1), 'foo3')
into so26 values(trunc(sysdate ), 'bar')
into so26 values(trunc(sysdate + 1), 'zoo')
select 1 from dual;
select * from so26;
declare
type event_list_t is table of so26%rowtype;
v_events event_list_t := event_list_t();
function get_events(p_day in date default sysdate) return event_list_t as
v_events event_list_t := event_list_t();
begin
select *
bulk collect into v_events
from so26
where day = trunc(p_day);
return v_events;
end;
begin
v_events := get_events(sysdate + 1);
if v_events.first is null then
dbms_output.put_line('no events on that day');
return;
end if;
for i in v_events.first .. v_events.last loop
dbms_output.put_line(i || ': event = ' || v_events(i).event);
end loop;
end;
/
Example output when get_events(sysdate - 1):
1: event = foo1
2: event = foo2
3: event = foo3

How to use procedure parameters in merge statement

i'm creating a procedure to update/insert a table using merge statement(upsert).now i have a problem: using procedure parameters i have to do this upsert.
procedure xyz( a in table.a%type,b in table.b%type,....)
is
some local variables;
begin
merge into target_table
using source_table --instead of the source table, i have to use procedure parameters here
on (condition on primary key in the table)
when matched then
update the table
when not matched then
insert the table ;
end xyz;
so how to use procedure parameters instead of source table in merge statement?? or
suggest me a query to fetch the procedure parameters and use it as source table values.
help me please.
Thanks in advance.
I know that I'm eight years late to the party, but I think that I was trying to do something similar to what you were doing, but trying to Upsert based on parameters passed into a stored procedure that returns an empty string on success and an error on failure back to my VB Code. Below is all of my code along with comments explaining what I did, and why I did it. Let me know if this helps you or anyone else. This is my first time answering a post.
PROCEDURE UpsertTSJobData(ActivitySeq_in IN NUMBER,
Owner_in In VARCHAR2,
NumTrailers_in IN NUMBER,
ReleaseFormReceived_in IN NUMBER,
Response_out OUT VARCHAR2) AS
err_num NUMBER;
err_msg VARCHAR2(4000);
BEGIN
--This top line essentially does a "SELECT *" from the named table
--and looks for a match based on the "ON" statement below
MERGE INTO glob1app.GFS_TS_JOBDATA_TAB tsj
--This select statement is used for the INSERT when no match
--is found and the UPDATE when a match is found.
--It creates a "pseudo-table"
USING (
SELECT ActivitySeq_in AS ActSeq,
Owner_in As Owner,
NumTrailers_in As NumTrailers,
ReleaseFormReceived_in As ReleaseFormReceived
FROM DUAL) input
--This ON statement is what we're doing the match on to find
--matching records. This decides whether it will be an
--INSERT or an UPDATE
ON (tsj.Activity_seq = ActivitySeq_in)
WHEN MATCHED THEN
--Here we UPDATE based on the passed in input table
UPDATE
SET OWNER = input.owner,
NUMTRAILERS = input.NumTrailers,
RELEASEFORMRECEIVED = input.releaseformreceived
WHEN NOT MATCHED THEN
--Here we INSERT based on the passed in input table
INSERT (
ACTIVITY_SEQ,
OWNER,
NUMTRAILERS,
RELEASEFORMRECEIVED
)
VALUES (
input.actseq,
input.owner,
input.numtrailers,
input.releaseformreceived
);
Response_out := '';
EXCEPTION
WHEN OTHERS THEN
err_num := SQLCODE;
err_msg := SUBSTR(SQLERRM, 1, 3900);
Response_out := TO_CHAR (err_num) || ': ' || err_msg;
END;
Maby something like
DECLARE V_EXISTS NUMBER;
BEGIN SELECT COUNT(*) INTO V_EXISTS FROM TARGET_TABLE WHERE PK_ID = :ID;
IF V_EXISTS > 0 THEN
-- UPDATE
ELSE
-- INSERT
END IF;
END;
Also, you may try to use so-called tempotary table (select from DUAL)
CREATE TABLE TEST (N NUMBER(2), NAME VARCHAR2(20), ADRESS VARCHAR2(100));
INSERT INTO TEST VALUES(1, 'Name1', 'Adress1');
INSERT INTO TEST VALUES(2, 'Name2', 'Adress2');
INSERT INTO TEST VALUES(3, 'Name3', 'Adress3');
SELECT * FROM TEST;
-- test update
MERGE INTO TEST trg
USING (SELECT 1 AS N, 'NameUpdated' AS NAME,
'AdressUpdated' AS ADRESS FROM Dual ) src
ON ( src.N = trg.N )
WHEN MATCHED THEN
UPDATE
SET trg.NAME = src.NAME,
trg.ADRESS = src.ADRESS
WHEN NOT MATCHED THEN
INSERT VALUES (src.N, src.NAME, src.ADRESS);
SELECT * FROM TEST;
-- test insert
MERGE INTO TEST trg
USING (SELECT 34 AS N, 'NameInserted' AS NAME,
'AdressInserted' AS ADRESS FROM Dual ) src
ON ( src.N = trg.N )
WHEN MATCHED THEN
UPDATE
SET trg.NAME = src.NAME,
trg.ADRESS = src.ADRESS
WHEN NOT MATCHED THEN
INSERT VALUES (src.N, src.NAME, src.ADRESS);
SELECT * FROM TEST;
DROP TABLE TEST;
see here
Its very difficult to tell from you question exactly what you what, but I gather you want the table that you are merging into ( or on ) to be dynamic. In that case, what you should be using is the DBMS_SQL package to create dynamic SQL

Resources