Solved

Use Query as table in  Merge Statement

Posted on 2013-06-26
7
397 Views
Last Modified: 2013-06-27
Hi,

I am using merge statemnet 30 times for 30 different destination tables like below

MERGE INTO tab2 t2-- this table name changes according to the parameter in using clause
USING (Query Here with respective parameter for the above table)  t2
on (t1.a = t2.c and t1.b = t2.d)
WHEN MATCHED THEN
      UPDATE
         SET t2.flag = t1.flag
   WHEN NOT MATCHED THEN
      INSERT
      VALUES (t1.a, t1.b, t1.flag);

Qry in using clause is something like below

SELECT c,d, flag  
FROM mytable1 s,
           mytable2 sa,
 WHERE s.key_data_name = <<parameter here>> --need to pass parameter
   and s.p_code=sa.p_code
 ORDER BY s.created_date;--need to use this order

I would like to write this big query "one time" in a function and passing the parameter every time and fetch the data to use in the USING clause of the Merge statement.
So that I can avoid "to write" this query in every USING clause of MERGE statement

Can you please suggest me how can i achieve this?

Thanks in advance.
0
Comment
Question by:GouthamAnand
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 28

Expert Comment

by:Naveen Kumar
ID: 39277450
I think it is not possible to do it that way with a function to return recordset which can be used in the using clause if i am not wrong but wait for other experts opinions as i do not have 11g to test it out.

Also there is one other thing WITH clause which you can google around for syntax/more information and try to see if that can suit you but WITH in general is for aliasing a reusable query which is referred to more than once in the same query with the ailas without rewriting the whole query mutlple times in that main query.
0
 
LVL 20

Accepted Solution

by:
flow01 earned 500 total points
ID: 39277634
Try writing a function that returns the merge-statement you want based on the arguments tablename and key_data_name.

function create_merge_statement(p_table_name varchar2, p_key_data_name varchar2)
return varchar2;

If you succeed in doing that :

function merge_for (p_table_name varchar2, p_key_data_name varchar2)
return number
is
  v_statement varchar2(32767);
begin
  v_statement := create_merge_statement(p_table_name, p_key_data_name);
  execute immediate v_statement;  -- 1)
  return SQL%ROWCOUNT;
end;

1)  the execute immediate usage requires that the executing user as the autorisation on the underlying table

2) i don;t know if the order by in your using clause is used by the merge or even permitted
0
 

Author Closing Comment

by:GouthamAnand
ID: 39278122
Thank you very much.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 32

Expert Comment

by:awking00
ID: 39278982
Does there exist a list of table names and their key data fields?
0
 

Author Comment

by:GouthamAnand
ID: 39280611
Yes. there are list of table names and their key data fields.
I thought returning merge statement through function is possible. But.
It is not possible.
Because it is not only the table name change but their column names also changes. So dynamic merge is not possible...
0
 
LVL 20

Expert Comment

by:flow01
ID: 39281999
If always all columns of for example the  primairy key are involved, you can expand your dynamic build by using the  USER_IND_COLUMNS view
0
 

Author Comment

by:GouthamAnand
ID: 39282354
Thank you. Yes. I could achieve now by expanding dynamic sql.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Repeat query 13 72
Oracle dataguard 5 56
Oracle DB Slows After Datapump Until Next Reboot 27 124
Oracle 12c Default Isolation Level 17 55
Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
Shell script to create broker configuration file using current broker Configuration, solely for purpose of backup on Linux. Script may need to be modified depending on OS-installation. Please deploy and verify the script in a test environment.
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database

761 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question