Cursor using variable table name

I'm new to Oracle, and am wondering if I can create a cursor that can take a variable name as the table it's selecting from. I am working on an application that is loading data to 1 of 3 tables. The table name is stored as a variable and the loads are done using dymanic sql to constuct the insert statement. At the end, I want to select and process some specific info from the table that's been loaded by cycling thru a cursor on whichever of the 3 tables was populated. I could create 3 cursors, one for each table, test the table_name variable, and then reference the specific cursor that way, but I thought there must be another way to do this. I want to be able to do something like this:
CURSOR loaded_table is select distinct(process_key) from 'v_tablename' (where v_tablename is either TABLE2, TABLE2 or TABLE3)
Any suggestions would be greatly appreciated.
Tks...MCR

It's possible to use dynamic SQL for something like this. If we're only talking about three tables, though, my hunch is that you'll be much happier defining three different cursors. Dynamic SQL is significantly harder to write, debug, and maintain than static SQL, so you're better off resorting to it only when there are so many tables that static SQL is impractical.
Justin
Distributed Database Consulting, Inc.
www.ddbcinc.com/askDDBC

Similar Messages

  • Creating a form with variable table name(s)

    Hi,
    I am building a form that will allow user(s) switch to other user
    (s) dynamically. I have been able able to do this by creating a
    non-database table and use record group to read from the base
    table of each user.(All the tables in question have the same
    structure).
    Record group is a read only, and I want to be able to do data
    maninpulation with this form.
    The questions are
    (1) It possible to do data manipulation with record group and if
    it is, how?
    (2) Is it possible to build a form with a variable table name?
    if possible, how?
    Somebody pls help.
    Thanks,
    Olutunde.
    null

    Olutunde Babarinsa (guest) wrote:
    : Hi,
    : I am building a form that will allow user(s) switch to other
    user
    : (s) dynamically. I have been able able to do this by creating a
    : non-database table and use record group to read from the base
    : table of each user.(All the tables in question have the same
    : structure).
    : Record group is a read only, and I want to be able to do data
    : maninpulation with this form.
    : The questions are
    : (1) It possible to do data manipulation with record group and
    if
    : it is, how?
    : (2) Is it possible to build a form with a variable table name?
    : if possible, how?
    : Somebody pls help.
    : Thanks,
    : Olutunde.
    Hi,
    you can create and manipulate record groups at runtime(see Forms
    Reference 'Create_Group' and 'Add_Group_Row') don't use
    Create_Group_from_Query. For your purpose it's better to build a
    cursor loop on your Query and add your datas after
    manipulating with 'Add_Group_Row' to your Record Group.
    It's possible to SET_BLOCK_PROPERTY(QUERY_DATA_SOURCE_NAME) but
    it's not possible to change the item property 'Column Name'.
    Therefor I would suggest to build a non database block and
    populate these block by a programm unit, which works with a
    pl/sql cursor loop and the 'create record' to populate these
    block.
    null

  • Cannot use duplicate table name error in AMDP

    Hi Experts,
    I'm developing an application which has the following architecture
    SAP UI5->Gateway->ABAP Managed DB procedures(AMDP)->HANA SP(Stored Procedure)
    We are having a very peculiar problem where the gateway service works inconsistently for the same input data.
    i.e If I execute the same service n number of times, I get the results successfully for say n-3 times but the other 3 times I get a "RFC Error :Error while executing Database procedure"
    There is no change in the input data or DB table data during all the executions.
    Running the stored procedure stand alone in HANA studio works all the time.
    We tried executing the AMDP from SE24 and the same behavior occurred.
    When trouble shooting we found that the exception occurs inside the AMDP when the call db procedure statement is executed.
    Could you please advise what could be the possible reason for this any tuning parameter/unavailability of db connections?
    Or could you please suggest some other troubleshooting mechanism by which we can zero in on the cause?
    When I go in the debug mode I can see the exact error as
    cannot use duplicate table name:  [288] SAPXXX.ZCL_PLAN_MAINTENANCE=>GET_COMP_TYPE#stub#20140905055908: line 15 col 3 (at pos 492): cannot use duplicate table name exception:
    where ZCL_PLAN_MAINTENANCE=>GET_COMP_TYPE is the AMDP class and method. SAPXXXis the schema.

    I reported this to Adobe customer support on 11/29, and here is their response:
    Wednesday, December 5, 2007 12:51:27 PM PST
    Hello John,
    Thank you for contacting Adobe® Web Support for assistance with Adobe
    Photoshop Elements® 6.0.
    I understand that images are deleted if you accidentally try to move
    them to a folder that already includes a file of the same name.
    Thank you for bringing this to our attention. I was able to replicate
    this behavior as well. The best method to report errors of this nature
    is using the following form on our website:
    http://www.adobe.com/cfusion/mmform/index.cfm?name=wishform
    I will report this to the product team through my channels. You may want to submit this issue through the web form as it goes directly to the product development team.
    I hope this information helps to resolve your issue. If you require
    further assistance with this issue, please update your web case with
    complete details, including what steps you have applied and any error
    messages you are receiving.
    You may also call Technical Support at (800) 642-3623. We are available from 6:00 am to 5:00 pm Monday - Friday, Pacific Time.
    Kind regards,
    Alan C.
    Adobe Web Support

  • Can we create cursor using the table created dynamically

    Dear all,
    Can we create the cursor using the table which is created dynamically using "Execute immediate" within the same procedure,
    or is there any way to call the table created dynamically ..
    please Do Help me in this
    thanks alot
    Edited by: khaja on Jan 18, 2009 10:57 AM

    Well, I don't think this approach is bad in any possible circumstances. It depends on the requirements we don't know. Yes, usually applications should not be designed in this way. But 'usually' does not mean 'never'. One possible case is described in Oracle's example Referencing Database Objects that Do Not Exist at Compilation. It's possible to assume that tables inv_01_2003, inv_04_2003, etc from the referenced topic What Is Dynamic SQL? are generated automatically by some job using dynamic create table stmt. If the OP has similar requirements tnen this approach is not bad. It may not be the best one but it at least is not so bad as you said.
    I believe that OPs know their requirements much better than me. This is why I always try to answer the exact question. If the approach is really ugly then I don't answer such questions at all.
    Regards,
    Dima
    Message was edited by:
    DimaCit

  • Using variable coulmn name in sql function

    Hi there,
    I am not an expert with PL/SQL and I can not figure out how to use variable column names in my function.
    My function is:
    CREATE OR REPLACE FUNCTION RESET_TRIGGERS(aTrigger VARCHAR2) RETURN NUMBER IS
    TEMP_ID NUMBER;
    TEMP_USER_ID NUMBER;
    BEGIN
    SELECT 'LIMS.'||'$aTrigger'||'.NEXTVAL' INTO TEMP_ID FROM DUAL;
    SELECT 'LIMS.'||'$aTrigger'||'_USER.NEXTVAL' INTO TEMP_USER_ID FROM DUAL;
    IF TEMP_ID > TEMP_USER_ID THEN
    LOOP
    SELECT LIMS.SQ_U_FINALRESULT_USER.NEXTVAL INTO TEMP_USER_ID FROM DUAL;
    EXIT WHEN TEMP_USER_ID = TEMP_ID;
    END LOOP;
    ELSE
    WHILE TEMP_ID < TEMP_USER_ID LOOP
    SELECT LIMS.SQ_U_FINALRESULT.NEXTVAL INTO TEMP_ID FROM DUAL;
    END LOOP;
    END IF;
    COMMIT;
    RETURN (TEMP_ID);
    END;
    What I want is that I pass a seqencename with aTrigger and that two triggers will be equal if not.
    eg ifaTrigger = 'SQ_U_FINALRESULT'
    than I want the triggers LIMS.SQ_U_FINALRESULT and LIMS.SQ_U_FINALRESULT_USER to be set equal.
    The above function will not work, but what will?????
    I hope you can help me out!
    Cheers

    A very strange function indeed.
    But here is what I think he meant to do:
    SQL> create procedure reset_sequences
      2  ( p_sequence_name in  varchar2
      3  , p_nextval          out number
      4  )
      5  is
      6    l_nextval1 number;
      7    l_nextval2 number
      8    ;
      9    procedure reset_sequence_value
    10    ( p_sequence_name in varchar2
    11    , p_current_value in number
    12    , p_new_value     in number
    13    )
    14    is
    15      l_dummy number;
    16    begin
    17      execute immediate 'alter sequence ' || p_sequence_name || ' increment by ' || to_char(p_new_value-p_current_value);
    18      execute immediate 'select ' || p_sequence_name || '.nextval from dual' into l_dummy;
    19      execute immediate 'alter sequence ' || p_sequence_name || ' increment by 1';
    20    end reset_sequence_value
    21    ;
    22  begin
    23    execute immediate
    24      'select ' || p_sequence_name || '.nextval,' || p_sequence_name || '_user.nextval from dual'
    25    into l_nextval1, l_nextval2
    26    ;
    27    if l_nextval1 < l_nextval2
    28    then
    29      reset_sequence_value(p_sequence_name,l_nextval1,l_nextval2);
    30    end if
    31    ;
    32    if l_nextval1 > l_nextval2
    33    then
    34      reset_sequence_value(p_sequence_name || '_user',l_nextval2,l_nextval1);
    35    end if
    36    ;
    37    p_nextval := greatest(l_nextval1,l_nextval2)
    38    ;
    39  end reset_sequences;
    40  /
    Procedure is aangemaakt.
    SQL> show err
    Er zijn geen fouten.
    SQL> create sequence testseq start with 5 increment by 1
      2  /
    Reeks is aangemaakt.
    SQL> create sequence testseq_user start with 2 increment by 1
      2  /
    Reeks is aangemaakt.
    SQL> declare
      2    l_new_value number;
      3  begin
      4    reset_sequences('testseq',l_new_value);
      5    dbms_output.put_line(l_new_value);
      6  end;
      7  /
    5
    PL/SQL-procedure is geslaagd.
    SQL> select testseq.currval from dual
      2  /
                                   CURRVAL
                                         5
    1 rij is geselecteerd.
    SQL> select testseq_user.currval from dual
      2  /
                                   CURRVAL
                                         5
    1 rij is geselecteerd.Regards,
    Rob.

  • Import Data using Full Table Name

    Is there a way to run the import data wizard so when pulling data from a csv it generates an insert statement not only using the table name but using the Oracle Schema name -- ie fully qualified table name?

    Hi Nilanjan,
    I need help ASAP.
    About this dump-load task, how does it works?  Because I was checking out a package called Import SQL  and this one only imports data from a table, however this table has to be allocated within the database that is being used, and I right? Are these 2 related somehow?
    Does this task order BPC to find data from a SQL Table (allocated in a different server, different instance for SQL) and import it to SQL Fac2 table?
    Can you help me with a simple explanation what do I need to do to run this task?  The page help.sap.com  talks shows a section within DumpLoad Task Usage which is called  Importing Into SQL Server... 
    * Processing the Application using DumpLoad  -
    Importing into SQL
    You can use DumpLoad to process the application as a standalone procedure, or with data import, export, or clear.
    Prerequisites
    The DumpLoad task (OsoftTaskDumpLoad2008.dll) is registered with Microsoft SSIS. See Registering Custom Tasks.
    Procedure
    1.Open a package or create a new package in Microsoft SSIS on the Planning and Consolidation server.  i already did this for the DumpLoad task ... should I do this for the Import SQL task also ?
    2.Select the task and add it to the package.   WHAT DOES SELECT THE TASK  MEAN?
    3.Open the task, and choose  Data Management  None .
    4.Enter the application set, application, and user ID.
    Im getting confused, can you please provide more details about the whole procedure. Thanx in advance
    Velázquez

  • How to use a table name in the select statement using a variable?

    Hi Everybody,
                       I got a internal table which has a field or a variable that gets me some tables names. Now I need to retrieve the data from all these tables in that field dynamically at runtime. So could you suggest me a way out to use the select query which uses this variable as a table ?
    Regards,
    Mallik.

    Hi all,
    Actually i need some more clarification. How to use the same select statement, if i've to use the tabname in the where clause too?
    for ex : select * from (tab_name) where....?
    Can we do inner join on such select statements? If so how?
    Thanks & Regards,
    Mallik.

  • Help with using variable for name of table in PLSQL block...

    I am trying to use a variable for the name of a table but getting errors ...i am sure I am close to the correct form here but something is wrong. I sure would appreciate some help!
    DECLARE
    ln_month Number;
    ln_day Number;
    ln_year Number;
    ls_year VarChar2(4);
    ls_prev_table VarChar2(30);
    ls_cur_table VarChar2(30);
    ln_prev_year Number;
    ls_prev_year VarChar2(4);
    ln_prev_month Number;
    BEGIN
    Select To_Number(To_Char(sysdate, 'MM')) into ln_month from dual;
    Select To_Number(To_Char(sysdate, 'DD')) into ln_day from dual;
    Select To_Number(To_Char(sysdate, 'YYYY')) into ln_year from dual;
    If ln_month = 01 Then
    ls_cur_table := "T_CPRS_FDW_CUR_JAN_ACTUALS";
    ls_prev_table := "T_CPRS_FDW_PREV_DEC_ACTUALS";
    ln_prev_year := ln_year - 1;
    /***above is where I am trying to use variables for assignement to years and months tables***//// ln_prev_month := 12;
    End If;
    /*------MORE IF STATEMENTS FOR EACH MONTH ---OF --THE YEAR ...AND its the following 2 variable statements that the compiler doesnt like! */
    If ln_day < 20 Then
    Delete from :ls_prev_table;
    INSERT INTO :ls_prev_table /*(STUFF TO BE INSERTED GOES HERE)*/
    HELP PLEASE!
    null

    Hi,
    The parser does not under variables directly in dml statements.u need to form a statement and the parse and execute the same...
    so the soln is
    Declare
    lv_stmt varchar2(250);
    Begin
    lv_stmt := 'Delete from '&#0124; &#0124;ls_prev_table;
    execute immediate lv_stmt;
    -- Same is the case with the insert stmt--
    End;
    This should solve ur problem.
    u could also give a direct call like
    execute immediate 'Delete from '&#0124; &#0124;ls_prev_table ;
    Note: This statement "execute immediate" holds good for oracle versions 8.x and above which makes the stmt very simple. For lower version u need to use the dbms_sql package to parse and execute the statement which has a series of statements for the same.
    Kiran

  • 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.

  • Parameterized cursor for varient Table name?

    Hi all,
    I am using Oracle 9i and have a cursor defined as :-
    Code:
    CREATE PROCEDURE Proc_Abc
    AS
    CURSOR
    My_Cursor (UserName VARCHAR) IS
    SELECT Emp_Name, Salary FROM Employee_Table
    WHERE User_Name = UserName;     
    (Rest of the code)
    This code is working perfectly, but if I try to provide the table name through the cursor variable, it gives an error
    Below is the code that I am writing to pass table name through variable:-
    Code:
    CREATE PROCEDURE Proc_Abc
    AS
    CURSOR
    My_Cursor (TableName VARCHAR, UserName VARCHAR) IS
    SELECT Emp_Name, Salary FROM TableName
    WHERE User_Name = UserName;     
    (Rest of the code)
    All the tables that I need to pass through cursor variables have the same fields and are all pre known to me, thats why "SELECT Emp_Name, Salary " is remaining common throughout.
    Please suggest how can I make a cursor with variant tables?
    Thanks in advance.

    The following procedure compares between two tables and then it picking the column from all_tab_column table and finally execute the SELECT statement to compare the data between this two table. But, you can proceed your program taking help from this -
    satyaki>ed
    Wrote file afiedt.buf
      1  create or replace procedure compr_tab_dat(TAR_TAB IN VARCHAR2,
      2                                            TAR_UID IN VARCHAR2,
      3                                            SRC_TAB IN VARCHAR2,
      4                                            SRC_UID IN VARCHAR2)
      5  is
      6   cursor c1
      7   is
      8    select column_name
      9    from (
    10           select column_name,column_id
    11           from all_tab_columns
    12           where table_name = SRC_TAB
    13           and   owner = SRC_UID
    14           intersect
    15           select column_name,column_id
    16           from all_tab_columns
    17           where table_name = TAR_TAB
    18           and   owner = TAR_UID
    19         )
    20     order by column_id;
    21   cursor c_count
    22   is
    23     select count(column_name) as c_cnt
    24     from (
    25             select column_name
    26             from all_tab_columns
    27             where table_name = SRC_TAB
    28             and   owner = SRC_UID
    29             intersect
    30             select column_name
    31             from all_tab_columns
    32             where table_name = TAR_TAB
    33             and   owner = TAR_UID
    34           );
    35   rec1 c1%rowtype;
    36   rec2 c1%rowtype;
    37   rec3 c1%rowtype;
    38   rec6 c_count%rowtype;
    39   cnt  number(10);
    40   cnt1  number(10);
    41   str  varchar2(32000);
    42   --str  clob;
    43  BEGIN
    44     cnt := 0;
    45     cnt1 := 1;
    46     dbms_output.enable(10000000);
    47     for rec6 in c_count
    48     loop
    49      cnt := rec6.c_cnt;
    50     end loop;
    51     if cnt = 0 then
    52        dbms_output.put_line('No matched columns found.... ');
    53     else
    54        dbms_output.put_line('UnMatched Datas Are-- ');
    55           str:='declare '||
    56          '  cursor c3 '||
    57          '  is '||
    58          '    select ';
    59           open c1;
    60           loop
    61             fetch c1 into rec1;
    62             exit when c1%notfound;
    63             if cnt = cnt1 then
    64                str:= str||rec1.column_name;
    65             elsif cnt1<cnt then
    66               str:= str||rec1.column_name||',';
    67             end if;
    68             cnt1 := cnt1 + 1;
    69           end loop;
    70           close c1;
    71           str:=str||' from '||SRC_TAB||
    72                ' minus '||
    73                ' select ';
    74           cnt1:=1;
    75           open c1;
    76           loop
    77             fetch c1 into rec2;
    78             exit when c1%notfound;
    79             if cnt = cnt1 then
    80                str:= str||rec2.column_name;
    81             elsif cnt1<cnt then
    82                str:= str||rec2.column_name||',';
    83             end if;
    84             cnt1 := cnt1 + 1;
    85           end loop;
    86           close c1;
    87           str:=str||' from '||TAR_TAB||';'||
    88                ' r3 c3%rowtype; '||
    89                ' begin '||
    90                '   for r3 in c3 '||
    91                '   loop '||
    92                '     dbms_output.put_line(';
    93           cnt1:=1;
    94           open c1;
    95           loop
    96             fetch c1 into rec3;
    97             exit when c1%notfound;
    98             if cnt = cnt1 then
    99                str:= str||' r3.'||rec3.column_name;
    100             elsif cnt1<cnt then
    101                str:= str||' r3.'||rec3.column_name||
    102                     '||'',''||';
    103                cnt1 := cnt1 + 1;
    104             end if;
    105           end loop;
    106           close c1;
    107           str:=str||');'||
    108                ' end loop;'||
    109                ' exception '||
    110                '   when others then '||
    111                '     dbms_output.put_line(sqlerrm); '||
    112                ' end; ';
    113     end if;
    114     execute immediate(str);
    115     --dbms_output.put_line(str);
    116  exception
    117    when others then
    118      dbms_output.put_line(sqlerrm);
    119* END;
    120  /
    Procedure created.
    satyaki>
    satyaki>
    satyaki>
    satyaki>create table emp_t
      2     as
      3       select * from emp
      4       where rownum < 5;
    Table created.
    satyaki>
    satyaki>
    satyaki>desc emp;
    Name                                      Null?    Type
    EMPNO                                     NOT NULL NUMBER(4)
    ENAME                                              VARCHAR2(10)
    JOB                                                VARCHAR2(9)
    MGR                                                NUMBER(4)
    HIREDATE                                           DATE
    SAL                                                NUMBER(7,2)
    COMM                                               NUMBER(7,2)
    DEPTNO                                             NUMBER(2)
    satyaki>
    satyaki>
    satyaki>desc emp_t;
    Name                                      Null?    Type
    EMPNO                                              NUMBER(4)
    ENAME                                              VARCHAR2(10)
    JOB                                                VARCHAR2(9)
    MGR                                                NUMBER(4)
    HIREDATE                                           DATE
    SAL                                                NUMBER(7,2)
    COMM                                               NUMBER(7,2)
    DEPTNO                                             NUMBER(2)
    satyaki>set lin 1000
    satyaki>
    satyaki>select * from emp;
         EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM     DEPTNO
          7369 SMITH      CLERK           7902 17-DEC-80        800                    20
          7499 ALLEN      SALESMAN        7698 20-FEB-81       1600        300         30
          7521 WARD       SALESMAN        7698 22-FEB-81       1250        500         30
          7566 JONES      MANAGER         7839 02-APR-81       2975                    20
          7654 MARTIN     SALESMAN        7698 28-SEP-81       1250       1400         30
          7698 BLAKE      MANAGER         7839 01-MAY-81       2850                    30
          7782 CLARK      MANAGER         7839 09-JUN-81       2450                    10
          7788 SCOTT      ANALYST         7566 19-APR-87       3000                    20
          7839 KING       PRESIDENT            17-NOV-81       5000                    10
          7844 TURNER     SALESMAN        7698 08-SEP-81       1500          0         30
          7876 ADAMS      CLERK           7788 23-MAY-87       1100                    20
         EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM     DEPTNO
          7900 JAMES      CLERK           7698 03-DEC-81        950                    30
          7902 FORD       ANALYST         7566 03-DEC-81       3000                    20
          7934 MILLER     CLERK           7782 23-JAN-82       1300                    10
    14 rows selected.
    satyaki>
    satyaki>
    satyaki>select * from emp_t;
         EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM     DEPTNO
          7369 SMITH      CLERK           7902 17-DEC-80        800                    20
          7499 ALLEN      SALESMAN        7698 20-FEB-81       1600        300         30
          7521 WARD       SALESMAN        7698 22-FEB-81       1250        500         30
          7566 JONES      MANAGER         7839 02-APR-81       2975                    20
    satyaki>
    satyaki>
    satyaki>set serveroutput on
    satyaki>
    satyaki>
    satyaki>begin
      2   compr_tab_dat('EMP_T','SCOTT','EMP','SCOTT');
      3  end;
      4  /
    No matched columns found....
    ORA-06535: statement string in EXECUTE IMMEDIATE is NULL or 0 length
    PL/SQL procedure successfully completed.
    satyaki>
    satyaki>
    satyaki>sho user;
    USER is "TRG2"
    satyaki>
    satyaki>
    satyaki>
    satyaki>
    satyaki>begin
      2       compr_tab_dat('EMP_T','TRG2','EMP','TRG2');
      3     end;
      4      /
      5  .
    satyaki>
    satyaki>ed
    Wrote file afiedt.buf
      1  begin
      2       compr_tab_dat('EMP_T','TRG2','EMP','TRG2');
      3* end;
    satyaki>/
    UnMatched Datas Are--
    7654,MARTIN,SALESMAN,7698,28-SEP-81,1250,1400,30
    7698,BLAKE,MANAGER,7839,01-MAY-81,2850,,30
    7782,CLARK,MANAGER,7839,09-JUN-81,2450,,10
    7788,SCOTT,ANALYST,7566,19-APR-87,3000,,20
    7839,KING,PRESIDENT,,17-NOV-81,5000,,10
    7844,TURNER,SALESMAN,7698,08-SEP-81,1500,0,30
    7876,ADAMS,CLERK,7788,23-MAY-87,1100,,20
    7900,JAMES,CLERK,7698,03-DEC-81,950,,30
    7902,FORD,ANALYST,7566,03-DEC-81,3000,,20
    7934,MILLER,CLERK,7782,23-JAN-82,1300,,10
    PL/SQL procedure successfully completed.N.B.: May be Any other member can come with much shorter or better technique than this one. But, according to your requirement - i'm posting it. Hope this will help you, or atleast give you some idea.
    Regards.
    Satyaki De.

  • Variable table name in the select statment ?

    data: h_itab like hier_out occurs 0 with header line,
          table_name(30) type C.
    here i declare h_itab as the variable for the table name
    itab_name = input_name
    input_name is imported
    select * from table_name
           into table h_itab
           where OBJVERS = 'A'.
    it does not allow me to use table_name in the select statement i get the error 'table_name is not defined i the abap dictionary as a table, projection view or database view'.
    Any suggestions are highly appreciated.
    Thanks in advance,
    BWer

    Hi,
    You must enclose the name of the table in braces to let the compiler know that the value will be supplied at run time.
    For example -
    tables mara.
    data table_name(30) type c.
    table_name = 'MARA'.
    SELECT *
      FROM (table_name)
    endselect.
    Regards,
    Anand Mandalika.

  • SQL Task Expression: SQL Command using variable tables

    Hello,
    I've got a question I hope someone could help me out with. I'm working on a SSIS project that basically synchronises logins of a Source and Destination server that are part of an AlwaysOn Cluster.
    The idea is as followed: On the source server there are variable tables created named Logins_<SourceServerName>_To_<DestinationServerName> for example. This one is filled with all the login information from that server, then on the Destination
    server the same table is created and the data is transfered. The Source and Destination server names are project variables.
    Once all data is copied I use a set of code from the dba_CopyLogins script I got from SQLSoldier (http://www.sqlsoldier.com/wp/sqlserver/transferring-logins-to-a-database-mirror) to create the missing logins on the Destination server.
    What I've found is that I can't use expressions to handle the code, at least I haven't found a way yet, expression evaluation will give an error on (for example) the following code:
    Begin
    Set @SQL = 'Create Login ' + quotename(@LoginName)
    If @Type In ('U', 'G')
    Begin
    Set @SQL = @SQL + ' From Windows;'
    End
    Else
    Begin
    Set @PasswordHashString = '0x' +
    Cast('' As XML).value('xs:hexBinary(sql:variable("@PasswordHash"))', 'nvarchar(300)');
    Set @SQL = @SQL + ' With Password = ' + @PasswordHashString + ' HASHED, ';
    Set @SIDString = '0x' +
    Cast('' As XML).value('xs:hexBinary(sql:variable("@SID"))', 'nvarchar(100)');
    Set @SQL = @SQL + 'SID = ' + @SIDString + ';';
    End
    I've tried various things to get this to work as an expression, however I seem to continue to get the error:
    The expression might contain an invalid token, an incomplete token, or an invalid element. It might not be well-formed, or might be missing part of a required element such as a parenthesis.
    So the next thing I tried was just using the script as Direct input with Parameter Mapping on the tables but I've got the error:
    Must declare the table variable "@P1".
    All the way up to @P12. Since we're talking about 12 variables that are in the script in total. But they are table variables and apparently Parameter mapping is not possible on table variables or am I wrong here? Pieces of code for example are:
    Set @MaxID = (Select Max(LoginID) From ?)
    Set @CurrID = (Select Min(LoginID) From ?)
    I hope there's someone who could help me out or at least can give me a direction to look into.
    Yes we could use the script provided, but we don't want to work with LinkedServers, besides I'd have to make local jobs on the servers instead of controlling it all from 1 centralised server.
    Thank for looking at the topic and I hope you have a clue about what to do...
    Regards,
    Danny

    The code you posted is a t-sql code. If you want to store it inside ssis variable syntax is different. As per what you say what might be best would be to warp above code inside a procedure and then call it from SSIS execute sql task after passing values
    through SSIS variable
    also for 
    Set @MaxID = (Select Max(LoginID) From ?)
    etc
    create variable in SSIS, set EvaluateAsExpression true and set expression as
    "Select Max(LoginID) AS MaxID From " + @[User::TableName]
    Where TableName is SSIS variable through which you pass the table name. Then use above statement inside execute sql task and set resultset option as single row and map resultset to SSIS variable (say @[User::MaxID]) to get return value 
    similarly for other statements
    Please Mark This As Answer if it solved your issue
    Please Mark This As Helpful if it helps to solve your issue
    Visakh
    My MSDN Page
    My Personal Blog
    My Facebook Page

  • Variable table name in insert

    In forms i want to give variable as table name.
    e.g When_button_press
    Insert into :txt1 values (select * from abc1)
    return error on :txt1.
    how can i do this.

    There are examples in the forum that show how. Here are some of them:
    CAN I PASS A TABLE NAME AS A VARIABLE IN THE FROM CLAUSE?
    DML issue in Oracle forms
    Re: How to include variable on block's query
    How to use table name as variable

  • Can substitution strings be used for table name references?

    Hi,
    I was wondering if it's possible to use substituion strings for table names in SQL queries. This would allow for me to edit all references to a table at one location. For instance, if a table name or dblink changed, I could edit the substitution string or application item to ensure all the queries reference the new table.
    For example:
    select * from &table1.
    table1 would be the substitution string for the actual table name
    I know this is possible if I used a pl/sql function returning a query, but I would much rather use a substition string in a SQL query.
    Thanks in advance.
    Brian

    i think not
    because how create the fields of * in this case
    in the other because all is dynamic, i think that you obtain only an error

  • Variable table name

    Hi all,
    I tried to execute the statement below inside a push button on forms 6i
    select count(*) into :global.temp from *:variable_table_name* where rownum=1;
    but it fails to compile because it only accepts a static table name....I need such statement because i wanna check empty table that is to be selected by user
    from list item (variable_table_name ) that contain table names
    Any help or advice ? thanks

    Hi,
    You can not use statement like this in forms. You will have to use EXECUTE IMMEDIATE or DBMS_SQL
    One example through EXECUTE IMMEDIATE.
    Create one function in database as below...
    CREATE OR REPLACE FUNCTION EXEC_STR(vStr IN VARCHAR2) RETURN NUMBER IS
      vCnt NUMBER;
    BEGIN
      EXECUTE IMMEDIATE vStr INTO vCnt;
      RETURN vCnt;
    END;and then from your form you can use like below...
    :GLOBAL.temp:=EXEC_STR('SELECT COUNT(*) FROM '||:variable_table_name);-Ammad

Maybe you are looking for