Dynamic Update Statement in Native SQL

Hi Experts,
I want to dynamically pass the field in Update statement in Native SQL. For eg.
data: str1 type string.
str1 = 'MARKETS'.
EXEC SQL.
      UPDATE PRDT.TBVEHDS4 SET (str1) = :'U'
      WHERE  VEH_NO       =  :'K1WK-54520'
      AND    SEGMENT_NO   =  :'01'
ENDEXEC.
But this is not taking (str1) as MARKETS field to update as U , its taking STR1 itself, Giving native SQL exception as Invalid Token as we are using DB2 as external DB system.
I checked with field-symbols also, but nothing helped.
Please help, thanks in Advance.
Regards,
Abhishek

Hi,
Check this demo program in SE38  ADBC_DEMO, take as example to construct your own dynamic native sql
Regards,
Felipe

Similar Messages

  • Dynamic table name in native SQL

    Hi,
    How can i use dynamic table name in native SQL?
    My req is to select data from a external database table , but the table name will be only poulated during runtime.
    How can i acheive this?
    Regards,
    Arun.

    It should work OK - see demo below.
    Jonathan
    report zsdn_jc_adbc_test.
    start-of-selection.
      perform demo_lookup.
    form demo_lookup.
      data:
        l_error_msg          type string,
        ls_t001              type t001, "Company
        ls_t003              type t003. "Doc types
      perform dynamic_lookup
        using
          'T001'
        changing
          ls_t001
          l_error_msg.
      write: / l_error_msg.
      perform dynamic_lookup
        using
          'T003'
        changing
          ls_t003
          l_error_msg.
      write: / l_error_msg.
    endform.
    form dynamic_lookup
      using
        i_tabname            type tabname
      changing
        os_data              type any
        o_error_msg          type string.
    * Use ADBC to select data
      data:
        l_mandt_ref          type ref to data,
        l_result_ref         type ref to data,
        l_mandt              type symandt,
        l_tabname            type tabname,
        l_sql_statement      type string,
        lo_cx_root           type ref to cx_root,
        lo_cx_sql            type ref to cx_sql_exception,
        lo_connection        type ref to cl_sql_connection,
        lo_statement         type ref to cl_sql_statement,
        lo_result_set        type ref to cl_sql_result_set.
      clear: os_data, o_error_msg.
      get reference of l_mandt into l_mandt_ref.
      get reference of os_data into l_result_ref.
      l_mandt   = '222'.   "i.e. select from client 222
      l_tabname = i_tabname.
      try.
          lo_connection = cl_sql_connection=>get_connection( ).
          lo_statement  = lo_connection->create_statement( ).
    * Set criteria for select:
          lo_statement->set_param( l_mandt_ref ).
          concatenate
            'select * from' l_tabname
            'where mandt = ?'
            into l_sql_statement separated by space.
    * Execute
          call method lo_statement->execute_query
            exporting
              statement   = l_sql_statement
              hold_cursor = space
            receiving
              result_set  = lo_result_set.
    * Get the data from the resultset.
          lo_result_set->set_param_struct( l_result_ref ).
          while lo_result_set->next( ) > 0.
            write: / os_data.
          endwhile.
    * Tidy up:
          lo_result_set->close( ).
          lo_connection->close( ).
        catch cx_sql_exception into lo_cx_sql.
          o_error_msg = lo_cx_sql->get_text( ).
        catch cx_root into lo_cx_root.
          o_error_msg = lo_cx_root->get_text( ).
      endtry.
    endform.

  • Dynamic UPDATE statement with parameters for column names.

    Hello,
    On this* website I read "The SQL string can contain placeholders for bind arguments, but bind values cannot be used to pass in the names of schema objects (table or column names). You may pass in numeric, date, and string expressions, but not a BOOLEAN or NULL literal value"
    On the other hand, in this Re: execute immediate with dynamic column name update and many other
    posts people use EXECUTE IMMEDIATE to create a dynamic UPDATE statement.
    dynSQL:='UPDATE CO_STAT2 CO SET CO.'||P_ENT_B_G_NAME||' = '||P_ENT_E_G_WE||'
    WHERE ST IN
    (SELECT ST FROM STG_CO_STAT2_TEST CO WHERE
    '||P_ST||' = CO.ST AND
    CO.'||P_ENT_E_G_NAME||' > '||P_ENT_E_G_WE||' AND
    CO.'||P_ENT_B_G_NAME||' < '||P_ENT_E_G_WE||');';
    EXECUTE IMMEDIATE dynSQL ;
    Since this statement is part of a Stored Procedure, I wont see the exact error but just get a ORA-06512.
    The compiling works fine and I use Oracle 11g.
    http://psoug.org/definition/EXECUTE_IMMEDIATE.htm

    OK I extracted from all of your posts so far that I have to use "bind-variables with :"
    From all the other tuorials and forums posts, I assume using the pipe is correct so I added those as well into the script:
    set serveroutput on format wraped;
    DECLARE
    dynSQL VARCHAR2(5000);
    P_ENT_E_G_NAME VARCHAR2 (100) :='test1'; P_ENT_E_G_WE VARCHAR2 (100) :='01.02.2012'; P_ENT_B_G_NAME VARCHAR2 (100) :='01.01.2012';
    P_ST                VARCHAR2 (100) :='32132';
    BEGIN
    dynSQL:= 'UPDATE CO_STAT2 CO SET CO.'||:P_ENT_B_G_NAME||' = '||:P_ENT_E_G_WE||'
                  WHERE ST IN (SELECT ST FROM STG_CO_STAT2_TEST CO WHERE
                  '||:P_ST||'                           = CO.ST                  AND
                  CO.'||:P_ENT_E_G_NAME||'     > '||:P_ENT_E_G_WE||' AND
                  CO.'||:P_ENT_B_G_NAME||'    
    < '||:P_ENT_E_G_WE||')';
    --this is somehow missing after the last < '||:P_ENT_E_G_WE||')';
    dbms_output.enable;
    dbms_output.put(dynSQL);
    --EXECUTE IMMEDIATE dynSQL;    
    END;Problem:I think I figured it out, the dates that I parse into the query need additional '

  • Creating a Dynamic Update Statement based on Select

    hi,
    i'm trying to create a dynamic update statement based on select statement
    my requirment is to query a joint tables and get the results then based on the results i need to copy all the data and create an update statement for each row
    for ex
    the update statement should look like this
    update iadvyy set SO_SWEEP_CNT = '1' where inst_no = '003' and memb_cust_no = 'aaaaaaaaaaaaaaaa';
    and the select statement like the following
    select substr(key_1,11,9) account_no,sord_mast SO_SWEEP_CNT from
    select acct_no,count(*) sord_mast from
    (select from_acct_no acct_no,update_mast
    from sord where FROM_SYS in ('DEP','INV') and TERM_DATE > 40460
    union all
    select to_acct_no acct_no,update_mast
    from sord where TO_SYS in ('DEP','INV') and TERM_DATE > 40460)
    group by Acct_no)
    right outer join
    invm
    on
    key_1 = '003'||acct_no
    where sord_mast > 0;
    so taking the above two columns from the above select statement and substitue the values as separate update statement.
    is that doable , please share your knowledge with me if poosible
    thanks in advanced

    is that doable , please share your knowledge with me if poosibleyes
    The standard advice when (ab)using EXECUTE IMMEDIATE is to compose the SQL statement in a single VARCHAR2 variable
    Then print the SQL before passing it to EXECUTE IMMEDIATE.
    COPY the statement & PASTE into sqlplus to validate its correctness.

  • Possible to do "grant" sql statement in Native SQL?

    We have a need to do a grant of access from one of our systems out for various applications.  In order for this to work we need to run a grant access command on the table and are trying to put a wrapper around this so we can use an abap.  Below is the code I am unit testing.  Two questions.  First, can a grant be done via native SQL in abap?  Second, if it can be done, what is the error with the logic where I am trying to put in the table name via a parameter.
    REPORT  ZLJTEST2.
    tables dd02l.
    DATA scarr_carrid TYPE dd02l-tabname.
    SELECT-OPTIONS s_carrid for dd02l-tabname no intervals.
    DATA s_carrid_wa LIKE LINE OF s_carrid.
    DATA name TYPE c LENGTH 20.
    TRY.
        EXEC SQL.
          CREATE FUNCTION selfunc( input CHAR(20) )
            RETURNING char(20);
            DEFINE output char(20);
            set schema sapr3;
            grant select on table input to group infouser;
            RETURN output;
            END FUNCTION;
        ENDEXEC.
        LOOP AT s_carrid INTO s_carrid_wa
                         WHERE sign = 'I' AND option = 'EQ'.
          TRY.
             EXEC SQL.
                EXECUTE PROCEDURE selfunc( IN  :s_carrid_wa-low,
                                           OUT :name )
              ENDEXEC.
              WRITE: / s_carrid_wa-low, name.
            CATCH cx_sy_native_sql_error.
              MESSAGE `Error in procedure execution` TYPE 'I'.
          ENDTRY.
        ENDLOOP.
        EXEC SQL.
          DROP FUNCTION selfunc;
        ENDEXEC.
      CATCH cx_sy_native_sql_error.
        MESSAGE `Error in procedure handling` TYPE 'I'.
    ENDTRY.

    Hi,
    Yes it is posible.
    I made one program like you want. But it need very long code.
    Here I explain the idea:
    1. Create Screen with input TEXT EDIT CONTROL.
        This is for input SQL Statement.
    2. Get SQL Statement from Text Edit Control using method <b>get_text_as_r3table</b>.
    3. Now we need to separate SQL Statement into different table.
        We Separate SELECT, FROM, WHERE, GROUP, HAVING, ORDER, etc.
    4. We need dynamic internal table to store the data.
    5. Select the data according SQL statement.
          SELECT (IT_SELECT)
            into corresponding fields of table  <dyn_table>
          FROM (IT_FROM)
          WHERE (IT_WHERE)
          GROUP BY (IT_GROUP)
          HAVING (IT_HAVING)
          ORDER BY (IT_ORDER).
    6. Display our data using ALV GRID
    Hopefully it will help you.
    Regards,

  • Dynamic 'from' Clause in Native SQL

    Hi All,
    While running a select query in ORACLE, we can put the table name in '<i>from</i>' clause at the runtime-Seletion Screen Parameter .
    But I am not able to do so in SAP using Native Sql. Does SAP has any  restrictions regarding this?
    It might be possible that ,by using Embedded SQL (C/C++ precompiler ),we may be able to achive this.
    Can any body please throw light on this.
    Its Urgent.
    Thanks in Advance.
    Regards,
    Gaurav

    Hi
    <u>Assuming you can also use Open SQL.</u>
    Macroman's suggestion is OK.  in Open SQL (this is SQL of ABAP platform) but a query statement can be constituted using some dynamic clauses. Although macro coding has some restrictions it will be less time consuming using it than a dynamic query.
    <u>An example SELECT statement with dynamic clauses:</u>
    SELECT (fields) FROM (table_name_variable)
    INTO <target_fields>
    WHERE (conditions_tab) .
    For more information:
    <a href="http://help.sap.com/saphelp_47x200/helpdata/en/fc/eb3983358411d1829f0000e829fbfe/frameset.htm">Open SQL -Reading Data</a>
    *--Serdar

  • Dynamic Update statements

    Hello,
    I have one SP to do DMLs. This SP works fine without any problem, but for Update I have problem to construct. The update statement should be constructed depending on the values.
    For example, in a table we may have so many columns, but only few non primary key columns are updated. The SP is given below.
    Following is the update statement. the set_nk_update_cols should be dynamic.
       '    if rec.operation$ = ''UN'' then ' ||
                     '      update <owner>.' || p_target ||
                     '         set ' || replace(set_nk_update_cols,'= ','= rec.') ||
                     '       where (' || pk_target || ') = (select rec.' || replace(pk_target,',',',rec.') ||
                     '                                        from dual ' ||

    I just did something like this not long ago
    The trick is to build your 2 columns lists using LISTAGG seperately.
    1) build your PK col list. Look into dba_ind_columns for this.
    2) build your full col list. Look into dba_tab_columns
    3) splice them all together in the right place:  ie  PK list is:  where col1 and col2 and col3 .... etc.  Full list is: select col1, col2, col3 ... etc.
    [edit]
    Something else you may want to do to make things more "readable"
    WITH w_template AS
       ( SELECT TO_CLOB(
             RTRIM(q'[UPDATE <table_name>           ]')||CHR(10)||
             RTRIM(q'[   SET                        ]')||CHR(10)||
             RTRIM(q'[<set_cols>                    ]')||CHR(10)||
             RTRIM(q'[ WHERE                        ]')||CHR(10)||
             RTRIM(q'[<where_cols>                  ]')||CHR(10)||
             RTRIM(q'[;                             ]')||CHR(10)
             ) tform
         FROM dual
       w_main AS (
          SELECT DISTINCT
                   dtc.table_name,
                   LISTAGG ( ?? )  set_cols,
                   LISTAGG ( ?? )  where_cols
             FROM dba_tab_columns dtc,
                  dba_ind_columns dic
            WHERE ???
    Select replace ( replace ( replace(
                                  w_template, '<table_name>', table_name ),
                         '<set_cols>', set_cols ),
                '<where_cols>', where_cols )
      from w_template t,
           w_main;
    Something like that, anyway ..
    Sorry, I haven't got much free time to play with this at the moment, but hopefully this gives you (or somebody else?) a direction you could take.
    [/edit]

  • Dynamic Update Statement

    How do I invoke a dyanmic update statement where the variable in the WHERE clause is a varchar? for instance code below will not work b/c the value in the where clause is missing single quotation marks '<varible>'
    v_dml_str := 'UPDATE D_UNIT_KP'
    || ' SET ' || p_unit_lvl_cd1 || '=' || p_hier1
    || ' WHERE uic = ' || p_uic;
    --execute immediate v_dml_str;
    DBMS_OUTPUT.PUT_LINE(v_dml_str);
    UPDATE D_UNIT_KP SET BN=WS5DAA WHERE uic = WS5DCD

    However, that should work. See example below :
    SQL> set serveroutput on
    SQL> declare
      2       p_username varchar2(30):='SYSTEM';
      3       p_status   varchar2(30):='OPEN';
      4       v_created  date;
      5       v_stmt     varchar2(1000);
      6  begin
      7       v_stmt:='select created from dba_users where username=:b1 and account_status=:b2';
      8       execute immediate v_stmt into v_created using p_username,p_status;
      9       dbms_output.put_line(v_created);
    10  end;
    11  /
    10-APR-03
    PL/SQL procedure successfully completed.You maybe miss some variables in the USING clause, as it shows here below (I removed the p_status from the USING clause) :
    SQL> declare
      2       p_username varchar2(30):='SYSTEM';
      3       p_status   varchar2(30):='OPEN';
      4       v_created  date;
      5       v_stmt     varchar2(1000);
      6  begin
      7       v_stmt:='select created from dba_users where username=:b1 and account_status=:b2';
      8       execute immediate v_stmt into v_created using p_username;
      9       dbms_output.put_line(v_created);
    10  end;
    11  /
    declare
    ERROR at line 1:
    ORA-01008: not all variables bound
    ORA-06512: at line 8Count the number of bind variables used, you should have the same number of variables. Even if you are using the same bind variable more than once, the variables should be repeated, as the following example :
    SQL> declare
      2       p_username varchar2(30):='SYSTEM';
      3       p_status   varchar2(30):='OPEN';
      4       v_created  date;
      5       v_stmt     varchar2(1000);
      6  begin
      7       v_stmt:='select created from dba_users where username=:b1 and username=:b1';
      8       execute immediate v_stmt into v_created using p_username;
      9       dbms_output.put_line(v_created);
    10  end;
    11  /
    declare
    ERROR at line 1:
    ORA-01008: not all variables bound
    ORA-06512: at line 8
    SQL> declare
      2       p_username varchar2(30):='SYSTEM';
      3       p_status   varchar2(30):='OPEN';
      4       v_created  date;
      5       v_stmt     varchar2(1000);
      6  begin
      7       v_stmt:='select created from dba_users where username=:b1 and username=:b1';
      8       execute immediate v_stmt into v_created using p_username,p_username;
      9       dbms_output.put_line(v_created);
    10  end;
    11  /
    10-APR-03
    PL/SQL procedure successfully completed.
    SQL>Nicolas.

  • Dynamic IF statement in PL/SQL on-insert trigger in Forms 6.0

    I would like to build a dynamic IF statement for the on-insert trigger in a form... Users are restricted to which "projects" they are allowed to enter into the database. These restrictions are based on a security table. The same restrictions are used to build the dynamic where clause that limits which previously inserted records the users can see when they are in the form. Since the restrictions are identical, I want to use the same logic that I use to build a where clause to build a dynamic If statement instead... If the statement is true, the user can insert the record. If its false, they can't.
    I'm trying to this as follows:
    declare
    big_if varchar2(10000) :=' ';
    begin
    if :global.admin='YES'
    then
    insert_record;
    else
    declare
    cursor cur1 is
    select paren1, field, comparison_operator, value, paren2, and_or
    from rcdb.user_project_assign2
    where user_id = user;
    c1_rec cur1%ROWTYPE;
    begin
    for c1_rec in cur1 loop
    big_if:= big_if &#0124; &#0124; c1_rec.paren1 &#0124; &#0124; ':tbl_main_data.' &#0124; &#0124;c1_rec.field &#0124; &#0124;c1_rec.comparison_operator &#0124; &#0124; '''' &#0124; &#0124; c1_rec.value &#0124; &#0124; '''' &#0124; &#0124; c1_rec.paren2 &#0124; &#0124; ' '&#0124; &#0124; c1_rec.and_or &#0124; &#0124; ' ';
    end loop;
    end;
    if big_if
    then
    insert_record;
    else message('You are not allowed to insert this record');
    end if;
    end if;
    end;
    The problem is in the line
    IF big_if
    big_if is the variable that holds the text to my if statement (hense dynamic If statement) but I can't get the code to allow me to use that variable.
    Can anyone do this?
    null

    That code was great, but forms isn't allowing me to use the dbms_sql so I have to make a procedure out of forms and call it from the form. Both my form code and the procedure are compiling, but I"m still getting an error when the result is coming back to the form...
    the code in the form is:
    declare
    big_if varchar2(10000) :=' ';
    result varchar2(5) :=' ';
    check_state varchar2(2):=:tbl_main_data.state_abr;
    check_region varchar2(3):=:tbl_main_data.region_abr;
    check_program varchar2(25):=:tbl_main_data.program;
    begin
    if :global.admin='YES'
    then
    insert_record;
    else
    declare
    cursor cur1 is
    select paren1, field, comparison_operator, value, paren2, and_or
    from rcdb.user_project_assign2
    where user_id = user;
    c1_rec cur1%ROWTYPE;
    begin
    for c1_rec in cur1 loop
    big_if:= big_if &#0124; &#0124; c1_rec.paren1;
    if c1_rec.field = 'state_abr' then
    big_if:=big_if &#0124; &#0124; 'check_state' &#0124; &#0124;c1_rec.comparison_operator &#0124; &#0124; '''' &#0124; &#0124; c1_rec.value &#0124; &#0124; '''' &#0124; &#0124; c1_rec.paren2 &#0124; &#0124; ' '&#0124; &#0124; c1_rec.and_or &#0124; &#0124; ' ';
    elsif c1_rec.field = 'region_abr' then
    big_if:=big_if &#0124; &#0124; 'check_region' &#0124; &#0124;c1_rec.comparison_operator &#0124; &#0124; '''' &#0124; &#0124; c1_rec.value &#0124; &#0124; '''' &#0124; &#0124; c1_rec.paren2 &#0124; &#0124; ' '&#0124; &#0124; c1_rec.and_or &#0124; &#0124; ' ';
    elsif c1_rec.field = 'program' then
    big_if:=big_if &#0124; &#0124; 'check_program' &#0124; &#0124;c1_rec.comparison_operator &#0124; &#0124; '''' &#0124; &#0124; c1_rec.value &#0124; &#0124; '''' &#0124; &#0124; c1_rec.paren2 &#0124; &#0124; ' '&#0124; &#0124; c1_rec.and_or &#0124; &#0124; ' ';
    end if;
    end loop;
    end;
    message(big_if); pause;
    message(check_state); pause;
    message(check_region); pause;
    message(check_program); pause;
    rcdb.check_if(big_if,result,check_state,check_region,check_program);
    message('resulte = &#0124; &#0124;result&#0124; &#0124;');
    if result = 'TRUE' then insert_record;
    else
    message ('you cant enter');
    end if;
    end if;
    end;
    AND THE CODE IN THE PROCEDURE IS....
    create or replace procedure check_if (
    big_if in varchar2,
    result out varchar2,
    check_state in varchar2,
    check_region in varchar2,
    check_program in varchar2
    IS
    v_indx binary_integer := 0;
    v_sql_syntax varchar2(32767);
    root_cursor number;
    ignore integer;
    your_if_statement VARCHAR2(32767);
    v_check VARCHAR2(5);
    BEGIN
    your_if_statement := ' if '&#0124; &#0124;big_if&#0124; &#0124;' then :v_check := ''TRUE''; end if;';
    v_sql_syntax := 'begin '&#0124; &#0124;your_if_statement&#0124; &#0124;' end;';
    root_cursor := dbms_sql.open_cursor;
    v_sql_syntax := replace(replace(REPLACE(v_sql_syntax,chr(10),' '),chr(13),' '),chr(9),' ');
    dbms_sql.parse(root_cursor,v_sql_syntax,dbms_sql.v7);
    dbms_sql.bind_variable( root_cursor, ':v_check',v_check,5);
    ignore := dbms_sql.execute(root_cursor);
    dbms_sql.variable_value(root_cursor, ':v_check', v_check);
    if v_check = 'TRUE' then result:='TRUE';
    else
    result:='FALSE';
    end if;
    dbms_sql.close_cursor(root_cursor);
    END check_if;
    DO YOU KNOW WHATS WRONG?

  • Dynamic table name in native sql query

    Can i pass a dynamic table name in this query ---
                EXEC SQL PERFORMING APPEND_MTO.
                  SELECT          
                               LTOG_QUANTITY_TYPE,
                               FCONO,
                              WBSELEMENT,
                             PROJECT
                  INTO   :IMTO
                  FROM  RWORKS.MTO_ISO_V2
                  WHERE LTOD_DATE > TO_DATE(:MAXD,'YYYYMMDDHH24MISS')
                ENDEXEC.
    How can i pass this table name RWORKS.MTO_ISO_V2  dynamically in the query
    Edited by: Madhukar Shetty on Nov 26, 2009 2:40 PM

    Can i pass a dynamic table name in this query ---
                EXEC SQL PERFORMING APPEND_MTO.
                  SELECT          
                               LTOG_QUANTITY_TYPE,
                               FCONO,
                              WBSELEMENT,
                             PROJECT
                  INTO   :IMTO
                  FROM  RWORKS.MTO_ISO_V2
                  WHERE LTOD_DATE > TO_DATE(:MAXD,'YYYYMMDDHH24MISS')
                ENDEXEC.
    How can i pass this table name RWORKS.MTO_ISO_V2  dynamically in the query
    Edited by: Madhukar Shetty on Nov 26, 2009 2:40 PM

  • Help automate an UPDATE Statement with PL/SQL

    Hello, I am on 10g R2, I am slowly learning PL/SQL, got a few books, watching videos on youtube and other things about like Best Practices and etc. I wanted to automate this SQL:
    Basically each month I have to update a whole bunch of older Tables against the current months Table.
    Older Table = ta
    Newer Table = tt
    I was thinking maybe I can like put into a list (like an array), and list all the older tables in there for ta, I have 26 so far and each month I add +1.
    The tt table is just one, that's why I was thinking I could automate that inside a loop ?
    UPDATE OLDER_TABLE_DATE ta
    SET (ta.GTP, ta.UPDATE_DT) =
         (SELECT tt.GTP, SYSDATE
          FROM NEWEST_TABLE_UPDATED tt
          WHERE ta.customer_id = tt.customer_id
          AND ta.STAMP_DATE = tt.STAMP_DATE)
    WHERE EXISTS (SELECT 1
            FROM NEWEST_TABLE_UPDATED tt
            WHERE ta.customer_id = tt.customer_id
            AND ta.STAMP_DATE = tt.STAMP_DATE
            AND (NVL(ta.GTP, 'X') != NVL(tt.GTP, 'X')));
    COMMIT;What do you guys think ? example or help would be appreciated. I can run this as an anonymous block for now, that's fine, thank you!

    Assumptions:-
    -Your all old/new tables are in same schema.
    - Their names have word "OLDER".
    - You will run the procedure from the same schema where all old and new tables exits.
    NOTE: - Please handle exceptions in the given code accordingly, for example, when update fails then handle the exceptions.
    -As mention earlier, this approach does not sound very good. A good solution will be change in design such a way that one table(partition on months) stores all the monthly data.
    CREATE OR REPLACE PROCEDURE UPDATE_TABLES_PROC
    AS
    cursor c1 is
    select table_name from user_tables
    where table_name like '%OLDER%';
    type t_current_table is table of c1%rowtype;
    v_current_table t_current_table;
    SQL_STMT VARCHAR2(3000):=0;
    BEGIN
    OPEN C1;
    LOOP
        FETCH C1 BULK COLLECT INTO  v_current_table  LIMIT 10;
        IF v_current_table.count>0 then
        FOR i in v_current_table .first..v_current_table .last
        LOOP
        SQL_STMT:='UPDATE ' ||v_current_table(i).table_name|| ' ta
                         SET (ta.GTP, ta.UPDATE_DT) =
                                       (SELECT tt.GTP, SYSDATE
                                        FROM NEWEST_TABLE_UPDATED tt
                                        WHERE ta.customer_id = tt.customer_id
                                       AND ta.STAMP_DATE = tt.STAMP_DATE)
                          WHERE EXISTS (SELECT 1
                                       FROM NEWEST_TABLE_UPDATED tt
                                       WHERE ta.customer_id = tt.customer_id
                                       AND ta.STAMP_DATE = tt.STAMP_DATE
                                       AND (NVL(ta.GTP, ''X'') != NVL(tt.GTP, ''X'')))';
        EXECUTE IMMEDIATE SQL_STMT;
        EXIT WHEN c1%NOTFOUND;
        END LOOP;
        END IF;
    COMMIT;
    EXIT WHEN c1%NOTFOUND;
    END LOOP;
    CLOSE C1;
    END UPDATE_TABLES_PROC;

  • Native sql DBCO, CONNECT statement short dump

    Hi ALL ,
    I am facing the  problem here.  please help me out in this.
    i am trying to connce to the external database MSS, i made all the entries in the DBCO t-code and writing the follwoing native sql statement.
    EXEC SQL.
    CONNECT TO 'AAJ' AS 'V'
    ENDEXEC.
    EXEC SQL.
    SET CONNECTION 'V'
    ENDEXEC.
    The above statement is not giving any error  when i activate it but when i execute it is giving me shortdump error .
    I already maintan   entries in DBCON table or DBCO t-code
    And also tel me once it is connect how can i access the data from MSS (Microsoft sql server)i mean what kind of statement.
    helpfull answer will be rewarded.
    Anees
    9886358645
    Message was edited by:
            anees jawad

    Native SQL
    Open SQL allows you to access database tables declared in the ABAP Dictionary regardless of the database platform that you R/3 System is using. Native SQL allows you to use database-specific SQL statements in an ABAP program. This means that you can use database tables that are not administered by the ABAP Dictionary, and therefore integrate data that is not part of the R/3 System.
    As a rule, an ABAP program containing database-specific SQL statements will not run under different database systems. If your program will be used on more than one database platform, only use Open SQL statements.
    Native SQL Statements in ABAP Programs
    To use a Native SQL statement, you must precede it with the EXEC SQL statement, and follow it with the ENDEXEC statement as follows:
    EXEC SQL [PERFORMING  )
    The parameters are separated by commas. You must also specify whether the parameter is for input (IN), output (OUT) or input and output (INOUT). For further information, refer to SAP Note 44977.
    EXEC SQL
       EXECUTE PROCEDURE proc1 ( IN , OUT :y )
    ENDEXEC.
    Cursor Processing
    Cursor processing in Native SQL is similar to that in Open SQL:
    OPEN
      ENDIF.
    ENDDO.
    EXEC SQL.
      CLOSE c1
    ENDEXEC.
    This example opens a cursor, reads data line by line, and closes the cursor again. As in Open SQL, SY-SUBRC indicates whether a line could be read.
    Data Types and Conversions
    Using Native SQL, you can
    Transfer values from ABAP fields to the database
    Read data from the database and process it in ABAP programs.
    Native SQL works without the administrative data about database tables stored in the ABAP Dictionary. Consequently, it cannot perform all of the consistency checks used in Open SQL. This places a larger degree of responsibility on application developers to work with ABAP fields of the correct type. You should always ensure that the ABAP data type and the type of the database column are identical.
    If the database table is not defined in the ABAP Dictionary, you cannot refer directly to its data type. In this case, you should create a uniform type description in the ABAP Dictionary, which can then be used by all application programs.
    If the table is defined in the ABAP Dictionary, you should remember that the sequence of fields in the ABAP Dictionary definition may not be the same as the actual sequence of fields in the database. Using the asterisk (*) in the SELECT clause to read all columns into a corresponding work area would lead to meaningless results. In the worst case, it would cause an error.
    The Native SQL module of the database interface passes a description of the type, size, and memory location of the ABAP fields used to the database system. The relevant database system operations are usually used to access and convert the data. You can find details of these operations in the manuals for the programming interface of the relevant database system. In some cases, Native SQL also performs other compatibility checks.
    The documentation from the various database manufacturers provides detailed lists of combinations of ABAP data types and database column types, both for storing ABAP field values in database tables (INSERT, UPDATE) and for reading database contents into ABAP fields (SELECT). You can also apply these descriptions for the input and output parameters of database procedures. Any combinations not listed there are undefined, and should not be used.
    The following sections provide details of the data types and conversions for individual databases. Although they are database-specific, there are also some common features.
    Recommended type combinations are underlined. Only for these combinations is behavior guaranteed from release to release. For any other combinations, you should assume that the description only applies to the specified release.
    The results of conversions are listed in a results column:
    "OK": The conversion can be performed without loss of data.
    Operations that fail are indicated by their SQL error code. Errors of this kind always lead to program termination and an ABAP short dump.
    In some cases, data is transferred without an SQL error occurring. However, the data is truncated, rounded, or otherwise unusable:
    Right truncation.
    "Left" or "right" applies to the normal way of writing a value. So, for example, if a number is truncated, its decimal places are affected.
    : Left truncation
    : Number is rounded up or down during conversion
    : A number that was "too small" is rounded to 0 (underflow)
    : The conversion result is undefined.
    There are several possible results. The concrete result is either not known at all, or can only be described using a set of rules that is too complicated for practical use.
    : The conversion returns the SQL value NULL.
    : The conversion is performed without fields and unchecked.
    The original data is converted, but without its format being checked. The result may therefore be a value invalid for the result type, which cannot be processed further. An example of this is a date field containing the value "99999999" or "abcdefgh" after conversion.
    Combinations of ABAP data type and database column type can be divided into finer subcategories. Here, for example, using the transfer direction ABAP ® database (INSERT, UPDATE):
    If the width of the ABAP field is greater than that of the database column, the ABAP field may contain values for which there is not enough space in the database column. This can produce other cases: The concrete data value in ABAP finds space in the database column, or not.
    If the ABAP field is at most as long as the database column, there is always space for the ABAP value in the database column.
    Some types, such as numeric columns, expect values in a particular format. This is particularly important in connection with character types, for example, when you want to write an ABAP character field (type C) into an integer column.
    Native SQL for Oracle
    Native SQL for Informix
    Native SQL for DB2 Common Server
    http://help.sap.com/saphelp_nw04/helpdata/en/fc/eb3b8b358411d1829f0000e829fbfe/content.htm

  • Convert a SQL Server Update Statement for Oracle

    Hi,
    i need to convert an update statement written for SQL Server to make it work on Oracle,
    the update is the following:
    UPDATE     TABLE1
    SET     CDate = TBL2.CDate
    FROM      TABLE1 TBL1
              JOIN TABLE2 TBL2 ON TBL2.Code = TBL1.TBL2Code
              JOIN TABLE3 TBL3 ON TBL3.Id = TBL1.Id
    WHERE     TBL3.TypeCode = '07'
    how can i make it compatible with oracle?
    thanks!!

    Billy  Verreynne  wrote:
    Karthick_Arp wrote:
    Try this
    <snipped>Dislike such an approach that requires multiple hits on the same tables. This is never scalable. A key performance issue is to do only one pass through a data set when possible and not do multiple passes.Yes i understand. But to do a update on a join the table must be a Key Preserved Table. And for a normal Update it is necessory to have an EXISTS clause so that we dont update the non-matching rows to NULL.

  • Executing a Variable in a Stored Procedure containing an Update Statement

    Hi,
    I have created a Stored Procedure, it has one input parameter. Inside the Stored Procedure there are 5 other variables are declared, based on the input parameter 4 of these variables are populated.
    Fifth variable is a a concatenation of text and some of these variables and creates a dynamic Update Statement. Once it is created I want to execute this variable but it is not executing.
    Could someone please help me in knowing how to execute a variable within a stored procedure that contains an update statement.
    Thanks for your help.
    Thanks
    Vineesh
    vineesh1701

    If you have set up the variable so that it contains a valid sql update, it should work.  For example, see below, which does do an update
    use tempdb
    go
    Create Table #Test(i int);
    Insert #Test(i) Values(1);
    go
    Create Procedure TestProc As
    Begin
    Declare @SQL nvarchar(250);
    Set @SQL = 'Update #Test Set i = 2';
    --Print @SQL;
    Exec sp_executesql @SQL;
    End
    go
    Exec TestProc
    -- Test Result
    Select * From #Test;
    go
    Drop Procedure TestProc;
    go
    Drop Table #Test;
    One thing I would recommend while testing is that you print out or select the variable that contains the dynamic sql.  (Like the above code where I have a print statement.  That lets you see exactly what you are executing and if there is any problem
    with the update command. 
    As Naomi said, to get anything more than general help, you will probably have to show us your code and the parameter values you are passing when you execute the stored proc.
    Tom

  • Insert into external DB using native SQL

    Hi,
    Is there any easier way to insert data in to External DB using native SQL insert.
    there are 250 columns to be inesrted. table structures in SAP and ext db are same.
    Thanks in Advance
    Somesh

    Hi someswara,
    Creating a dynamic statements using Native SQL is a bit tough task. I don't think anyone might have tried it. Instead you can write down the all columns as it is one time activity only, and by the time you will find solution you will be ready with your queries :).
    Reward points if useful.
    Regards,
    Atish

Maybe you are looking for