Question on MERGE in inside a stored procedure?

Hello people,
I have this procedure who did a MERGE command inside the procedure and, basically, all this is it inserts all the data from the temporary table when there is no match on the Bill_ID.
Here, my requirement is on a daily basis than the Bill_ID that can be assigned to a different Event_ID. If for example the Bill_ID has changed his event tomorrow the procedure should update the row with the new Event_ID. But in my procedure this is not kept up-to-date because the Bill_ID has not changed and it is the same thing (coz uses MERGE insert when not matched). How to achieve on a daily basis. What is the cornerstone of the code to do this. Am a bit new to programming and am trying to learn.

CREATE OR REPLACE PROCEDURE load_fadm_staging_area_test (
  p_data_load_date DATE
) IS
  v_start_date   DATE;
  v_end_date     DATE;
BEGIN
  SELECT NVL (p_data_load_date, SYSDATE) - 7,
         NVL (p_data_load_date, SYSDATE) - 1
  INTO   v_start_date,
         v_end_date
  FROM   DUAL;
 
  MERGE INTO stg_fadm_hri_stage_bill_test b
  USING      (SELECT *
              FROM   stage_bill
              WHERE  created_date BETWEEN v_start_date AND v_end_date) a
  ON         (b.bill_id = a.bill_id,)
  WHEN NOT MATCHED THEN
    INSERT     (batch_id,
                beginning_service_date,
                bill_id,
                bill_method,
                bill_number,
                bill_received_date,
                bill_status,
                bill_type,
                change_oltp_by,
                change_oltp_date,
                client_datafeed_code,
                client_id,
                created_date,
                date_of_incident,
                date_paid,
                deleted_oltp_by,
                deleted_oltp_date,
                duplicate_bill,
                ending_service_date,
                event_case_id,
                event_id,
                from_oltp_by,
                oltp_bill_status,
                review_status,
                row_effective_date,
                row_end_date
               )
    VALUES     (a.batch_id,
                a.beginning_service_date,
                a.bill_id,
                a.bill_method,
                a.bill_number,
                a.bill_received_date,
                a.bill_status,
                a.bill_type,
                a.change_oltp_by,
                a.change_oltp_date,
                a.client_datafeed_code,
                a.client_id,
                a.created_date,
                a.date_of_incident,
                a.date_paid,
                a.deleted_oltp_by,
                a.deleted_oltp_date,
                a.duplicate_bill,
                a.ending_service_date,
                a.event_case_id,
                a.event_id,
                a.from_oltp_by,
                a.oltp_bill_status,
                a.review_status,
                v_start_date,
                v_end_date
               );
END load_fadm_staging_area_test;
Published by: user11961230 on January 26, 2011 09:36

user11961230 wrote:
Hi John,.
Sorry for the confusion I think I got your point of NVL. Last question would be, now the Row_Effective_Date and Row_End_Date columns are out in the v_start_date and the v_end_date is to say sysdate-7 and sysdate-1.
If the procedure is executed on a daily basis, then I want to update these columns Row_Effective_Date, Row_End_Date with the dates of every day. How to get there? How to include this code? Thanks for all the help.

Just add them to the list updated.

WHEN MATCHED THEN UPDATE
   SET b.event_id = a.event_id,
       b.row_effective_date = v_start_date,
       b.row_end_date = v_end_date,
   WHERE b.event_id != a.event_id;

If you want to update all the (in other words, each corresponding line), then remove the predicate event_id. If you only want to update those that have changed and then leave it in. You could also update them to stage_bill.create_date by using this field instead of variables.

John

Tags: Database

Similar Questions

  • Unexpected token: the error that occur inside a stored procedure

    I get an unexpected token error when you try to compile the following stored procedure. The error occurs on the run pk_proof.pr_ProofAssets inside the stored procedure exec statement. No reason?
    CREATE OR REPLACE PROCEDURE MONTHLY_ASSET (ln_business_dt_num IN NUMBER DEFAULT NULL,
                                               missing_tbl_name OUT NOCOPY VARCHAR2) 
    IS
       ln_business_dt_num NUMBER;
       missing_tbl_name VARCHAR2;
       no_tbl_name_found EXCEPTION;
       bad_date_value EXCEPTION;
       counts_not_matched EXCEPTION;
     BEGIN
       IF ln_business_dt_num < 0 THEN
       RAISE bad_date_value;
       ELSE
         Select MAX(business_dt_num) into ln_business_dt_num
         FROM sasor.dp_ca_proof;
          
       if missing_tbl_name IS NOT NULL then
          raise no_tbl_name_found;
       end if;
       
       exec pk_proof.pr_ProofAssets('SLH_DST_ASSET', ln_business_dt_num, 'sasor_batch');
     

    Remove 'exec '.

  • Question about handling apostrophe in the stored procedure parameter

    I was wondering how people have dealt with this problem.  In my process I use the operation to execute stored procedure (sp) to write data to a database, and it is by the way the field values to the sp as parameters.  Works fine when everything is normal but as soon as an apostrophe is added in one of the fields, ka-boom.  It breaks .  So I have been messing around with the means to fight against this problem and did not really come wih an appropriate option.  I know that I can go on the form and write a few RegEx and change the single apostrophe double that would work, but I have to write in several events since the form is sent to someone else and I can't have them seeing "clerk"s"instead of"clerk".  Right now I'm writing an xpath statement that would replace the single apostrophe with a double bed with no luck.  I was wondering if anyone else has had this problem and found an easy solution.  Thank you

    Mike

    Why not use the parameterized query option.

    In this way, your settings are replaced by some? and you don't get messed up with the '

    Your select statement would look like: select * from table where column1 =? and column2 =?

    Then you can map the? s in the process variables.

    Jasmine

  • By using the parameter as a column in a select statement inside the stored procedure.

    I'm doing a variation of what follows. Can someone tell me how to use the parameter passed in the IN clause correctly? Thank you
    drop table test1;
    drop table test2;
    
    CREATE TABLE TEST1
    (
      COL1  NUMBER
    );
    
    CREATE TABLE TEST2
    (
      COL2  NUMBER
    );
    
    insert into test1 values (1);
    insert into test2 values (1);
    
    commit;
    
    create or replace  procedure test_sp (col_name varchar2)
    as
    
    var1 number;
    begin
    
    select col1 into  var1 from test1 where col1 in (select col_name from test2);
    
    end;
    
    exec test_sp ('COL2');
    Deleted table.
    Deleted table.
    Table created.
    Table created.
    1 line of creation.
    1 line of creation.
    Validation complete.
    Created procedure.
    BEGIN test_sp ("COL2"); END;
    Error on line 29
    ORA-01722: invalid number
    ORA-06512: on-site ".» TEST_SP', line 7
    ORA-06512: at line 1

    You must use dynamic sql statements:

    SQL> drop table test1;
    
    Table dropped.
    
    SQL> drop table test2;
    
    Table dropped.
    
    SQL> CREATE TABLE TEST1
      2  (
      3    COL1  NUMBER
      4  );
    
    Table created.
    
    SQL> CREATE TABLE TEST2
      2  (
      3    COL2  NUMBER
      4  );
    
    Table created.
    
    SQL> insert into test1 values (1);
    
    1 row created.
    
    SQL> insert into test2 values (1);
    
    1 row created.
    
    SQL> commit;
    
    Commit complete.
    
    SQL> create or replace  procedure test_sp (col_name varchar2)
      2  as
      3  var1 number;
      4  begin
      5  execute immediate 'select col1 from test1 where col1 in (select ' || col_name || ' from test2)' into var1;
      6  end;
      7  /
    
    Procedure created.
    
    SQL> exec test_sp ('COL2');
    
    PL/SQL procedure successfully completed.
    
    SQL> 
    

    SY.

  • Can't access Table inside the stored procedure

    Pls Me suggest good way

    The problem that I face that I created a user user1 and create a global temporary table under that user. Then I "select" Developer sql query this time under another user (User0) as select * from user1.gtt_table; successfully, it runs and return results. also can also insert data into user1.gtt_table.

    But if I try to use Insert or select user1.gtt_table and error

    • Error (798,29): PL/SQL: ORA-00942: table or view does not exist

    am I something wrong or missing any step?

    Connect as user1 for this.

  • stored procedures, newbie question

    I am at the point with my Web site where I need to think about the optimization of the query and have started looking at stored procedures for the first time.

    1. am I right in thinking the benefit of the execution of stored procedures is that it is a pre-compiled query?

    2. I'm trying to use the query cache as my 'normal' queries go, but is it possible to cache the results of a stored procedure?

    In practice, using 1 & 2 would provide a significant increase in performance.

    Personally, I use it only for large heavy updates or deletions. For example, I have an Oracle package with a bunch of procedures that run when a person completes an order - this process involves many tables, deletions, updates and a relatively simple logic. I think that the ideal candidate for a stored procedure.

    I use CF as an alternative if, for example, I have to repeat a load of complex business logic in a stored procedure that I have already written in a CFC.

    Keep in mind the benefits of a stored procedure - basically precompiled code. Don't forget that if you are using well trained cfqueries with cfqueryparams, execution plan gets compiled to the top in the first round * anyway *, so if you call a stored procedure or run your query really there is no difference.

    And Yes - do not underestimate the additional time it takes for a developer to do everything in the stored procedures rather than ColdFusion, they don't call it rapid development of applications for anything

    Don't forget as well as someone can write a stored procedure to poorly coded as well as they can write a wrong encoded query. At least if the developer has its application in front of him, he can see instantly what he needs of the index and locate errors, you could go years before realizing a person made a mistake inside a stored procedure, as no one can see the code.

    Regarding security Yes - if you really want to limit security then sprocs will do that for you. However in most of the applications I've seen the right developer has the possibility to write a query, as they see fit.

    Once I use stored procedures while I think of it - if you have more than one application that needs to do the same thing: this should * definitely * be a stored procedure.

    Basically, the same rules apply to them as any other aspect of programming; security of the duplication of code, efficiency, ease of maintenance - it's all a matter of their maximum weight and decide what is best.

  • How to create a table in a stored procedure

    Please help me to answer the following question...

    How to create a table inside the stored procedure... I want a detailed explanation... Please help me

    Please see this link:
    http://tinyurl.com/cmq5vo

  • Problem with stored procedure and validation

    I have the following stored procedure:

    create or replace PROCEDURE SOME_PROC)

    /*

    Some settings

    */

    ) AS

    NUMBER of errors

    BEGIN

    errors: = FN_CHECK_BUSINESS_RULE_1 (/ * some args * /);

    if(Errors > 0) then

    raise_application_error (ERR_CONSTANTS. SOME_ERROR_NUMBER, ERR_CONSTANTS. SOME_ERROR_MESSAGE);

    end if;

    INSERT INTO une_table (/ * columns * /) VALUES (/ * values * /);

    END SOME_PROC;

    Because the business rule 1 is placed inside the stored procedure I can't check it out without calling the stored procedure.

    I need to call the stored procedure 10 times with a different set of parameters and validation of the changes only after all calls to the stored procedure

    are successful. I want to show the user all the errors that occurred during the stored procedure calls. If for a first example of stored procedure call

    succeeds and a second failure no data has to be stored in a database.

    How to prevent the stored procedure for insert lines until I call the method commit of ApplicationModule?

    Thanks in advance.

    No, other users only see the lines until you commit. The search term is the transaction isolation level. Tom Kite write a paper on this here ask Tom: on transaction isolation levels. This article gives some samples, according to theory, and you should read it.

    Timo

  • Create or replace Stored procedure

    Hi all

    11.2.0.1

    I have the HR schema/user, who is the owner of all tables in the app.

    Then, all his paintings are also granted to BATCH - HR user with corresponding synonyms.

    This batch user will be used by computer operators to run reports of generations.

    For security reasons, they are not allowed to CONNECT to HR, but only to the BATCH - RH.

    My question is, can I create a stored procedure to the BATCH - RH which has only synonymous all tables?

    Or is it a good design to install it on human resources?

    Stored procedures recommend to operate only on base tables?

    Thank you

    Petra k.

    You can use everywhere in PL/SQL or external programs, because synonym is nothing more than a name new/more existing object; i.e. scott.emp table can have synonym1, synonym2, synonym3, etc.

    For more information: CREATE SYNONYM

    Concerning

    Girish Sharma

  • call a packed stored procedure from within another stored procedure

    I have a stored procedure (STROKEQC) on a scheme that is not in a package. I want to be able to call a procedure stored that IS inside a package on the same scheme of STROKEQC. When I try the syntax below, I get an error "no function with name 'PARTICIPANTSPECIFICEDITS' exists in this area." That the procedure exists.
    /*Generate admin field data*/
        execute immediate QCPROCEDURES.PARTICIPANTSPECIFICEDITS('ALL');        
        execute immediate QCPROCEDURES.GENERATE_SURGYEAR_ERRFLAG;
    Is not the way to do inside a stored procedure?

    Thank you!
    Eva

    Hi, Eva,.

    You need not EXECUTE IMMEDIATE to call a stored procedure (including a procedure in a package) on the other. Just use the name and the arguments (if any):

    QCPROCEDURES.PARTICIPANTSPECIFICEDITS('ALL');
    QCPROCEDURES.GENERATE_SURGYEAR_ERRFLAG;
    

    I guess qcprocedures is the package name, and participantspecificedits and generate_surgyear_errflag are procedures in this package.

    In case you're wondering, the error was probably because you do not have a variable called participantspecificedits. If you had a dynamic code in a string called variable x, then "EXECUTE IMMEDIATE x"; would be the way to run it.

  • Question packets and stored procedures

    Been working with Oracle for a month now and I have a question about stored procedures/packages. I created 3 stored procedures and a search function to do some work as follows:
    1 load the initial set of data into a table.
    2. turn to day of the preceding table columns by using a specific search function.
    3. put to update a single column with a value based on grouped subsets of the data in the table.

    I divide the latter in separate proceedings to facilitate debugging and validation of each step, but now I want to put them all together and run them sequentially. In my view, there are two ways to do this:
    1. create another stored procedure that will run the three in the right order.
    2 create a package and move all procedures and functions and call each in the right order.

    Don't know which approach is preferable, although I'm leaning towards the first option, any actual experience would be appreciated. Thanks in advance!

    Dave

    How about option 3:

    3 create a package and move all procedures and functions, and then add an additional procedure in the package that calls each in the correct order and call this procedure.

    PS. only this last procedure must be in the package spec. All others must be put into the body (and can remain hidden for the code outside the package).

    Published by: Toon Koppelaars February 26, 2010 21:07

  • How to use &amp; APP_ID. stored procedure to the inside.

    Hello, I created a stored procedure. I'm calling it my code in process. I want to use so many variables like page & APP_ID. I don't want to pass as an argument. Is it possible to use these variables without passing on the inside. ? (& page_id. and also an application of variable level)

    Any help is appreciated.

    Thank you.

    Dark Lord

    You can use the function 'v' to reference the APEX elements in pl/sql. Then APP_ID is done refers to

    v ('APP_ID')

    CITY

  • Simple question-how to call a stored procedure or function of apex?

    Simple question-how to call a stored procedure or function of apex?
    Thanks advance.
    Doug

    Hi Doug,.
    You can call a procedure or function of apex. It depends on what you want to do with the function or procedure. If you want to retrieve table data in a specific area, you can try something like this-

    The following statement creates the function get_bal on the oe.orders of sample table (PL/SQL is in italics):

    CREATE FUNCTION get_bal (acc_no in NUMBER)
    RETURN NUMBER
    IS acc_bal NUMBER (11.2);
    BEGIN
    SELECT order_total
    IN acc_bal
    Orders
    WHERE customer_id = acc_no;
    Return (acc_bal);
    END;
    /

    The function created in the previous example can be used in a SQL statement. For example:

    SELECT get_bal (165) FROM DUAL;

    GET_BAL (165)
    ------------
    2519

    hope this will help,

    Kind regards

    Pascal M
    http://Tajuddin.whitepagesbd.com

  • stored procedure question

    Hello guys,.

    could someone help me in this matter?

    Write a PL/SQL stored procedure that adds a dvd title at the end of the list of movies to a customer. This procedure should take as parameters in entry the customer_ID and film title_ID as your schema defines.

    I wrote the below procedure. I am a very beginner, I appreciate your help.

    create or replace
    PROCEDURE PROC_MOVIE_TITLE
    (CUST_ID IN VARCHAR2, DVD_ID IN VARCHAR2)
    AS

    BEGIN
    SELECT r.memberid, d.dvdid in CUST_ID, DVD_ID
    R, d dvd RENTAL
    where are.dvdid = d.dvdid;



    END PROC_MOVIE_TITLE;

    Add new column as "dvdtitle" in the table of the rental.
    And follow these steps

    create or replace procedure upd_dvdtitle (p_dvdid number)
    is
    Start
    Update rental r set dvdtitle = (select dvdtitle from dvd d where d.dvdid = p_dvdid)
    where are.dvdid = p_dvdid;
    end;

  • Stored procedure calendar basic questions

    If I have the following statements in a stored procedure, what happens if a user performs an update, insert, or select this option for the MAIN_TABLE during the procedure (assume that the procedure takes a few seconds)?

    MY_PROCEDURE:
    INSERT INTO BACKUP_TABLE (COL1, COL2, ETC.)
    (SELECT COL1, COL2, ETC. FROM MAIN_TABLE WHERE BACKED_UP = 'N');
    
    UPDATE MAIN_TABLE SET BACKED_UP = 'Y' WHERE BACKED_UP = 'N';
    If they do an update/insert, MY_PROCEDURE data will see that update/insert and send it to BACKUP_TABLE? Or it will depend on if this file has been processed yet? Or the update/insert will not persist until the procedure is completed? (I suspect the latter, but it's hard to get the timing right to test it myself)

    Would there be problems doing a select on MAIN_TABLE while this process occur?

    If there is a reference to how these timing issues are resolved that someone could tell me, that would be great; I can't find anything on mine.

    When all else fails, read the Fine

    http://download.Oracle.com/docs/CD/E11882_01/server.112/e16508/consist.htm#sthref1118

Maybe you are looking for

  • synchronize photos from iPad to PC &amp; ne can not find them

    I have synced my photos/videos from my iPad with iOS 9.3.1 2 Air on my laptop with a Windows 7 professional OS. I can not find them... someone can you please say they their whereabouts? Thank you!

  • MB Pro restarts randomly with the ethernet cable is plugged

    I recently bought a thunderbolt reduction - Ethernet [1] and it works very well. The problem is that when it is plugged in, my MacBook Pro (13', 2015, i5, 8 GB) gets very hot. This isn't a problem, but the next thing is really annoying. Whenever I'm

  • Installation SSD / old MacBook

    I have a 13-inch, aluminum, late 2008 MacBook, 2 Ghz Intel Core 2 Duo with 4 GB memory. I read that I could improve performance by installing an SSD in this regard. I cleaned up as I can without any result. It is currently running Yosemite 10.10.5 Is

  • Satellite L670-153: problem with a SONY 4 GB mass storage

    I have a portable satellite L670-153 with Windows 7 64 bit. When I conect a 4 GB SONY USB mass storage, Windows install the USB mass storage. But after a few minutes, the USB mass storage is disassemble and assemble and disassemble some indefinitely.

  • Playlist order no alphabetical longer (no order of song)

    Hi, until an update a few months ago, all of the selections have been classified by alphabetical order. Now the selections in the subfolders don't order alphabetically and rather order when they are created/added to the file. The songs in a playlist