Using Table name in Read statement dynamically

Hi Experts,
I have the following requirement.
Based on a country code say "A" , "B", "C" and based on this i have to read different internal tables lt_tabA, lt_tabB and lt_tabC.
The table key can be given dynamically, but how can i use the internal table name dynamically without using if statements.
Please help.
Thanks!
Best Regards,
Gayathri

Hi Gayathri,
something like
DATA:
    lt_t100 TYPE TABLE OF t100,
    lt_t000 TYPE TABLE OF t000.
  FIELD-SYMBOLS:
    <table> TYPE table,
    <rec>   TYPE ANY,
    <field> TYPE ANY.
  SELECT:
    * INTO CORRESPONDING FIELDS OF TABLE lt_t100 FROM t100 UP TO 10 ROWS,
    * INTO CORRESPONDING FIELDS OF TABLE lt_t000 FROM t000 UP TO 10 ROWS.
  CASE abap_true.
    WHEN space.
      ASSIGN  lt_t100 TO <table>.
    WHEN OTHERS.
      ASSIGN  lt_t000 TO <table>.
  ENDCASE.
  READ TABLE <table> with key ('MANDT') = sy-mandt ASSIGNING <rec>.
Regards,
Clemens

Similar Messages

  • Use table name in UPDATE statement as variable

    Hi All,
    I am using ORACLE 9i AIX base. I want to write following procedure:
    PROCEDURE update_header (
    p_care_msg_nbr IN NUMBER,
    p_care_msg_id IN NUMBER,
    p_care_msg_gkey IN NUMBER,
    p_old_status IN VARCHAR2,
    p_new_status IN VARCHAR2
    IS
    l_table varchar2(50);
    BEGIN
    l_table := 'CARE_EDI_MESSAGES';
    UPDATE '&l_table'
    SET status = p_new_status,
    changed = sysdate,
    changer = user
    WHERE care_msg_no = p_care_msg_nbr
    AND care_msg_id = p_care_msg_id
    AND gkey = p_care_msg_gkey
    AND status = p_old_status;
    END;
    I want to give the table name by passing value in a variable or parameter. Is it the write way or can I do this function in any other way.
    Pleae tell me in urgency.
    Thanks & Regards
    Hassan Raza

    This implies that you have several tables with identical or near identical structures, otherwise you will get errors if any of the mentioned columns do not exist in the table passed but ...
    You cannot use a substitution variable that way. Oracle will ask for a value when you try to create the procedure, and compile what ever value you supply into the procedure viz.
    SQL> CREATE PROCEDURE p AS
      2  l_id NUMBER;
      3  BEGIN
      4     SELECT id INTO l_id FROM &t;
      5  END;
      6  /
    Enter value for t: T
    old   4:    SELECT id INTO l_id FROM &t;
    new   4:    SELECT id INTO l_id FROM T;
    Procedure created.
    SQL> SELECT text FROM user_source WHERE name = 'P'
      2  ORDER BY line;
    TEXT
    PROCEDURE p AS
    l_id NUMBER;
    BEGIN
       SELECT id INTO l_id FROM T;
    END;Even if that is the effect you want, note that there are no quotes around the substitution variable in the CREATE statement.
    If you want to be able to pass the name of the table to the procedure, then you need to make the table name a parameter, then build the sql statement and execute it using EXECUTE IMMEDIATE. Something more like:
    PROCEDURE update_header (p_care_msg_nbr  IN NUMBER,
                             p_care_msg_id   IN NUMBER,
                             p_care_msg_gkey IN NUMBER,
                             p_old_status    IN VARCHAR2,
                             p_new_status    IN VARCHAR2,
                             p_table         IN VARCHAR2) IS
    l_sqlstr VARCHAR2(4000);
    BEGIN
       l_sqlstr := 'UPDATE '||p_table||' SET status = :b1 '||
                   'changed = sysdate, changer = user '||
                   'WHERE care_msg_no = :b1 and care_msg_id = :b3 and '||
                   'gkey = :b4 and status = :b5';
       EXECUTE IMMEDIATE l_sqlstr
          USING p_new_status, p_care_msg_nbr, p_care_msg_id, p_care_msg_gkey, p_old_status;
    END;HTH
    John

  • How to use bind variable value for table name in select statement.

    Hi everyone,
    I am having tough time to use value of bind variable for table name in select statement. I tried &p37_table_name. ,
    :p37_table_name or v('p37_table_name) but none worked.
    Following is the sql for interactive report:
    select * from v('p37_table_name') where key_loc = :P37_KEY_LOC and
    to_char(inspection_dte,'mm/dd/yyyy') = :P37_INSP_DT AND :p37_column_name is not null ;
    I am setting value of p37_table_name in previous page which is atm_state_day_insp.
    Following is error msg:
    "Query cannot be parsed, please check the syntax of your query. (ORA-00933: SQL command not properly ended) "
    Any help would be higly appreciated.
    Raj

    Interestingly enough I always had the same impression that you had to use a function to do this but found out from someone else that all you need to do is change the radio button from Use Query-Specific Column Names and Validate Query to Use Generic Column Names (parse query at runtime only). Apex will substitute your bind variable for you at run-time (something you can't normally do in pl/sql without using dynamic sql)

  • Problem in using table name dynamically in PL/SQL

    I tried to create a procedure to use table name dynamically and got the following error. Anything wrong with my procedure?
    NFADV>declare
      2   cnt number;
      3   cursor cur is select table_name from user_tables where table_name in ('EMP','DEPT');
      4  begin
      5   for c1 in cur
      6   loop
      7    execute immediate select count(*) into cnt from c1.table_name;
      8      dbms_output.put_line('Count is : '||cnt);
      9   end loop;
    10  end;
    11  /
    declare
    ERROR at line 1:
    ORA-06550: line 7, column 21:
    PLS-00103: Encountered the symbol "SELECT" when expecting one of the following:
    ( - + case mod new not null <an identifier>
    <a double-quoted delimited-identifier> <a bind variable> avg
    count current exists max min prior sql stddev sum variance
    execute forall merge time timestamp interval date
    <a string literal with character set specification>
    <a number> <a single-quoted SQL string> pipe
    <an alternatively-quoted string literal with character set specification>
    <an alternatively-quo
    ORA-06550: line 8, column 5:
    PLS-00103: Encountered the symbol "DBMS_OUTPUT"
    ORA-06550: line 8, column 45:
    PLS-00103: Encountered the symbol ";" when expecting one of the following:
    . ( , * % & - + / at mod remainder rem <an identifier>
    <a double-quoted delimited-identifier> <an exponent (**)> as
    from into || multiset bulkThanks and Regards
    Kaustubh

    Hi,
    The wrong part is in execute immediate, it should be as follows:
    execute immediate ('select count(*) from '|| c1.table_name)
    into cnt
    (not tested)
    Regards
    AK

  • How to use table name dynamically in report  procedure

    Hello every body
    I want to use table name dynamically means at runtime i want to pass table name.
    I can do this by lexical parameter in main query.
    But my problem is that i want to crate new format trigger or new function that use 1 cursor in which that lexical parameter : table name is used.
    so how to do that?
    i can not use that lexical parameter it is giving error.
    please help me how to do that.

    Call a database function which you pass the lexical parameter. Then in the database function you use dynamic sql or the execute immediate option to build the SQL string.
    Return the information from this function to build your business logic on in your format trigger.
    Marcos

  • Dynamic DB table name in insert statement

    In the following command i was able to build <dyn_table> dynamically but how do we do that for <dyn_wa>. table name should be populated dynamically here.
    for ex: pa0001, pa0002, .... and I have these names in an IT table
    INSERT  <dyn_wa> FROM TABLE <dyn_table>
                              ACCEPTING DUPLICATE KEYS.
    Pls help...Thanks

    Take it as charater type.
    This code works for me.
    TABLES : ztest123.
    DATA : tab_name(10) TYPE c.
    tab_name = 'ZTEST123'.
    DATA : wa LIKE ztest123.
    wa-name = 'Mahesh'.
    INSERT (tab_name) FROM wa.
    Thanks
    Mahesh

  • How to find the list of un used table names in a schema?

    Hi,
    I have a doubt in Oracle. The doubt is that If we are using any tables in Function Or Proc.... Then...We can list all those used table names from USER_DEPENDENCIES system table. Right...
    But, If the table is used with Execute Immediate Statement, then, those table names are not coming out with USER_DEPENDENCIES system table. Because they are identified at run time and not compile time.
    It is fine. And I agree.. But, If I want to list out those tables also...then...How to do? Any idea?
    I think ‘USER_SOURCE’ system table may not be the right one. If there is any other system table avails for this purpose...then..it would be very grateful to extract right...
    So I am wanting that exact system table.
    Please let me know about this, if you have any idea or check with your friends if they have any idea.
    Regards,
    Subramanian G

    Hi Guys,
    Thanks for all your answers.
    Yes....You are all right. We can list out the used tables upto certain extent. Anyhow, I have done some R&D to derive the SQL's which is given below:
    SELECT TABLE_NAME FROM USER_TABLES
    MINUS
    SELECT DISTINCT UPPER(REFERENCED_NAME)
    FROM user_dependencies
    where
    referenced_type='TABLE' and UPPER(NAME) in
    select distinct UPPER(object_name) from user_objects where UPPER(object_type) in
    'MATERIALIZED VIEW',
    'PACKAGE',
    'PACKAGE BODY',
    'PROCEDURE',
    'TRIGGER',
    'VIEW',
    'FUNCTION'
    UNION
    SELECT UT.TABLE_NAME FROM
    SELECT TABLE_NAME FROM USER_TABLES
    MINUS
    SELECT DISTINCT UPPER(REFERENCED_NAME)
    FROM user_dependencies
    where
    referenced_type='TABLE' and UPPER(NAME) in
    select distinct UPPER(object_name) from user_objects where UPPER(object_type) in
    'MATERIALIZED VIEW',
    'PACKAGE',
    'PACKAGE BODY',
    'PROCEDURE',
    'TRIGGER',
    'VIEW',
    'FUNCTION'
    AND REFERENCED_OWNER=(SELECT sys_context('USERENV', 'CURRENT_SCHEMA') FROM dual)
    ) UT,
    ( SELECT * FROM USER_SOURCE
    WHERE NAME IN
    ( SELECT DISTINCT NAME FROM USER_SOURCE
    WHERE TYPE NOT IN ('TYPE')
    AND
    UPPER(TEXT) LIKE '%EXECUTE IMMEDIATE%'
    ) US
    WHERE
    UPPER(US.TEXT) LIKE '%'||UPPER(UT.TABLE_NAME)||'%'
    AND
    (UPPER(US.TEXT) NOT LIKE '%--%')
    The above SQL Query can list out unused tables by checking the Dynamic SQL Statement also upto some level only.
    Once we extracted the list of unused tables, having a manual check would be also greater to verify as it is should not impact the business applications.
    Regards,
    Subramanian G

  • Unable to  use Table name in a variable

    Hi,
    I am trying to build a select statement which uses table name in a variable. Please help me in building such statements.
    Regards
    Kishore

    Hi,
    I am trying to build a select statement which uses table name in a variable. Please help me in building such statements.
    Regards
    Kishore

  • Dynamically assigning table name in select statement

    how can i assign the table name dynamically in Select statement?
    i tried following code
    create or replace procedure proc1
    as
    x varchar2(100);
    y varchar2(10);
    begin
    x='UNIT_MASTER';
    execute immediate 'select unit_code into y from x where
    rownum=1';
    dbms_output.put_line(y);
    end;
    the procedure is created but when i execute the procedure the
    error is shown in the execute immediate statement

    Do the following :
    Create or replace procedure pro1 as
    x varchar2(100);
    y varchar2(10);
    begin
    x := 'UNIT_MASTER';
    EXECUTE IMMEDIATE 'select unit_code from '||x||' where rownum
    = 1' INTO y;
    dbms_output.put_line(y);

  • ODI: Using Table Name in Dynamic filters

    We have a requirement, where the filters have to be dynamically generated and applied on the source system data stores.
    The requirement can be best explained by the below example.
    I have EMPLOYEE and DEPARTMENT table as the source datastores and EMP_DEPT (flat table) as the target datastore.
    The filter condition will be updated now and then by the admin in a table. They would like to run the integration interface with the condition mentioned in the table.
    Metadata table and sample data: (DY_FILTERS)
    TABLE_NAME | INTERFACE_NAME | CONDITION
    EMPLOYEE | EMP_DEPT | EMPLOYEE.EMPLOYEE_NAME LIKE 'A%'
    DEPARTMENT | EMP_DEPT | DEPARTMENT.DEPARTMENT_ID = 10
    So now the interface has to run with the conditions 'EMPLOYEE.EMPLOYEE_NAME LIKE 'A%' and DEPARTMENT.DEPARTMENT_ID = 10.
    To achieve, the best possible solution I can think of is, I have defined a variable for the dynamic filter and under the refresh section and I am planning to use the following query:
    SELECT CONDITION FROM DY_FILTERS WHERE INTERFACE_NAME = <%=odiRef.getPop("POP_NAME")%> AND
    TABLE_NAME = ***************.
    I was able to pick the interface that is currently involved by using getPop() method where as I dont have clue for getting the table name.
    Please share with me, if you have answer. Also if you have any other way to achieve this, please share the same.
    Note: The actual scenario is more complex than the example given above. But the crux of the requirement is very well covered in the example.
    Edited by: 986046 on Feb 14, 2013 2:06 PM

    Hi,
    If you've only one source datastore in your interface, you can retrieve it's name with <%=odiRef.getSrcTablesList("[RES_NAME]", "")%>.
    If you have more than one source it will list all you sources.
    However I can't see when you plan to refresh your variable. getSrcTablesList won't work before/after the interface execution.
    Regards,
    JeromeFr

  • How to use table name as variable in insert statement

    Hi,
    I want to insert data from the cursor into the table and I want to submit table name as a variable. I tried the code below but it's not working. Any ideas how to solve this problem? Thx
    DECLARE
    v_new_table_name VARCHAR2(30) := 'test';
    --open cursor   
    CURSOR ACM IS
    SELECT *
    FROM DWH.CLI_DIMENSION@SLSPDW CLI
    WHERE where_clause
    FROM some_table;
    BEGIN
    FOR REC_ACM IN ACM LOOP
    EXECUTE IMMEDIATE 'INSERT INTO ' || v_new_table_name || ' VALUES REC_ACM'; --table name as a variable in which the data from cursor will be inserted
    V_ROWCOUNT := V_ROWCOUNT + 1;
    IF MOD(V_ROWCOUNT, 100) = 0 THEN --this feature commits after 100 rows.
    COMMIT;
    END IF;
    END LOOP;
    COMMIT;
    DBMS_OUTPUT.PUT_LINE('Number of inserted records:' || V_ROWCOUNT);
    END;

    Try this and let me know if it works. I have not tested it so I can not say for sure.
    DECLARE
      v_new_table_name VARCHAR2(30) := 'test';
      CURSOR ACM IS
        SELECT * FROM DWH.CLI_DIMENSION@SLSPDW CLI
                WHERE where_clause
                FROM some_table;
      BEGIN
      loop
       fetch ACM INTO REC_ACM
       exit when ACM%NOTFOUND;
       EXECUTE IMMEDIATE 'INSERT INTO ' || v_new_table_name || ' VALUES REC_ACM';
       V_ROWCOUNT := V_ROWCOUNT + 1;
       IF MOD(V_ROWCOUNT, 100) = 0 THEN
        COMMIT;
       END IF;
      end loop;
      COMMIT;
      DBMS_OUTPUT.PUT_LINE('Number of inserted records:' || V_ROWCOUNT);
      END;

  • How user variable table names in select statement

    Dear all,
    I have three table gp1,gp2,g3. i want user variable table in sql query
    for example at oracle forms have a list table showing table names gp1,gp2,gp3
    at form i want user this query
    select gpno from :table where gpno=120;
    how i can specify table name Dynamicly in select query
    Thanks

    Forms_DDL is a one-way street: You can only pass DDL commands TO the database; you cannot get data back using Forms_DDL.
    Exec_SQL is the Forms package that enables dynamic sql within a form. But to retrieve data, you have to make a Exec_SQL call for every column in every row. So it is not a good thing to use, either.
    The ref cursor method should work. You could also retrieve the data into a record group using populate_group_with_query -- it also enables dynamic data retrieval.
    But if you already know you have three distinct tables and you know their names, I would keep it simple and just write three sql select statements.

  • Invalid table name when pass in the table name as variable in dynamic sql

    Hi,
    I need to create a stored procedure which will return a list of data to my java application like the following.
    first, select the table name from the first table.
    For example : SELECT T_NAME FROM MDR_SMSTABLES
    second, select the data from the table which returned by first select statement
    For example : SELECT * FROM T_NAME.
    I use dynamic sql with cursor to select the data. But it returns "java.sql.SQLException: ORA-00903: invalid table name"
    I fetch the table name to varchar2. I think this might the cause it returns me the this error. But i don't know what type should i put for the table name other then varchar2.
    val2 VARCHAR2(200);
    OPEN cv FOR
    SELECT T_NAME FROM MDR_SMSTABLES WHERE T_DATE=d_dt_sent;
    FETCH cv INTO val2;
    WHILE cv%FOUND
    LOOP
    OPEN refcur FOR
    'SELECT * FROM :t WHERE MID = :m' USING val2, msg_id;
    EXIT WHEN refcur IS NOT NULL;
    FETCH cv INTO val2;
    END LOOP;
    As my stored procedure is quite long, so i just paste some of the code here. Hope the information is enough. Can anyone please help?
    Thanks

    DECLARE
    val2 VARCHAR2 (200);
    cv sys_refcursor;
    refcur sys_refcursor;
    BEGIN
    OPEN cv FOR
    SELECT table_name
    FROM user_tables
    WHERE table_name IN ('EMP', 'DEPT');
    FETCH cv INTO val2;
    WHILE cv%FOUND
    LOOP
    OPEN refcur FOR 'SELECT * FROM '||val2;
    EXIT WHEN refcur IS NOT NULL;
    FETCH cv INTO val2;
    END LOOP;
    END;

  • How to find table name(s) in a dynamic query

    In one of our (9iAS PORTAL)applications, any user can able to run their query statment and get output. All queries will be executed as dynamic SQL thru' a common shema/user who has 'SELECT' privilege on all tables but all users don't have this privilege on all tables. Now, before executing any query I need to find out what are the table name(s) used in this query to check the privilege for the respective user. How do I extract all table names being used in a dynamic query?.
    Thanks
    -Krishnamurthy

    I guess that his users log in to application first, then application uses one account to log in to database, so application are not direct database user since all users share the same account to database. If so, when a user lgins in, you may write it down from application to a database audit table, and you need to write down the dynamic query to the database audit table too, then you may query that audit table to find out. up to here, it looks that your application has to do more to assign what role to a user running the application, since database does not know what user is assessing it.

  • How to insert variable for table name in Select statement ?

    I am creating a stored procedure which will take two table names as IN parameters. Within the procedures I would like to use the parameters in the following manner;
         SELECT count(*)
         INTO v_target_cnt
         FROM TargetTable;
    TargetTable is one of the parameters passed in. When I do this however it does not recognize the parameter. I have tried assigning the parameter to a local variable and using the variable, with not luck.
    Any help....thanks

    Null,
    What you are describing is called a LEXICAL parameter, which is allowed (preceded by an ampersand) in sql but not in pl/sql because it would not be possible to compile it. That is why you need to use Andrew's suggestion to make the sql dynamic. In older versions you would need to use DBMS_SQL which is horrible and now thankfully redundant.

Maybe you are looking for

  • I Installed ITunes, and now my computer can no longer find my CD-Drives?

    WTH is going on?

  • VPN clients can connect via SSTP but not IKEv2 due to error 808

    I have a Windows Server 2012 R2 with RRAS configured to allow SSTP / IKEv2 VPN connections. I'm using an external certificate for server authentication and the client authentication is done via domain username/password (Protected EAP). The clients ca

  • Wiki won't work because of proxy error. Help?

    Howdy All, After a long struggle I managed to get the Wikis working. Firstly, I didn't know the users and groups had to be in Open Directory - that could be made more clear and, by the way, why is that so? Secondly, it seems my OSXS upgrade didn't go

  • Opening file in PDF

    Does anyone know how to open a PDF file separate from the browser. I need the file to open in adobe, brand new window, not in the browser. I have this code which opens it in the browser, I just need it to open separate from the browser in adobe. _roo

  • File system cache performance

    hi. i was wondering if anyone could offer any insight into how to assess the performance of the file system cache. I am interested in things like hit rate (which % of pages read are coming from the cache instead of from disk), the amount of data read