How to perform a select op on a table whose name is contained by a variable

I am using Oracle SQL*Plus client.
declare
tablename_var varchar(10); LOOK HERE
date_var date;
begin
select sysdate
into date_var
from &tablename_var; LOOK HERE
dbms_output.put_line('The system date is '||date_var);
end;
The above code prompts the user to enter a value for the variable tablename_var, once the user enters 'dual' on prompt... The system date is given as output.
declare
tablename_var varchar(10):='dual'; LOOK HERE
date_var date;
begin
select sysdate
into date_var
from tablename_var; LOOK HERE
dbms_output.put_line('The system date is '||date_var);
end;
This code fails and gives an error. I understand. Engine interprets it as... trying to perform a select operation on a variable, and says a table or a view is expected.
Can someone please tell me what to do, to perform a select operation on a table whose name is contained by a variable, like above?

Hi,
Welcome to the forum!
You need Dynamic SQL if the table- or column names are variables.
Getting the current date isn't a good example; you could get that without a query simply by saying
date_var := SYSDATE;Also, SELECT ... INTO will raise an error if the query does not return exactly 1 row.
In the example below, let's get the latest entry_date from a given table:
For example:
declare
     tablename_var     varchar(10);
     date_var      date;
     sql_txt           VARCHAR2 (1000);
begin
     tablename_var := 'table_x;
     sql_txt := 'select  MAX (entrydate)'
          || '  from  ' || tablename_var;
     dbms_output.put_line (sql_txt || ' = sql_txt');    -- Not essential, but recommended
     EXECUTE IMMEDIATE  sql_txt  INTO date_var;
     dbms_output.put_line ('The latest entry_date is ' || date_var);
end;Dynamic SQL involves creating a string that contains the SQL statement. Displaying the string is purely optional, of course, but it's a very good idea when writing and de-bugging code. Remember to comment-out or delete the display before moving the code into Production.
For an exercise, make the column name a variable, instead of the hard-coded entry_date.
Edited by: Frank Kulash on Nov 5, 2009 9:20 AM

Similar Messages

  • How to insert the select query result into table?

    How to insert the select query result into table?
    SELECT  top 20 creation_time  
            ,last_execution_time 
            ,total_physical_reads
            ,total_logical_reads  
            ,total_logical_writes
            , execution_count 
            , total_worker_time
            , total_elapsed_time 
            , total_elapsed_time / execution_count avg_elapsed_time
            ,SUBSTRING(st.text, (qs.statement_start_offset/2) + 1,
             ((CASE statement_end_offset 
              WHEN -1 THEN DATALENGTH(st.text)
              ELSE qs.statement_end_offset END 
                - qs.statement_start_offset)/2) + 1) AS statement_text
    FROM sys.dm_exec_query_stats AS qs
    CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) st
    ORDER BY total_elapsed_time / execution_count DESC;
    Thanks,
    Tirumala

    1. SELECT INTO
    Below method will create table when data is inserted from one table to another table. Its useful when you need exactly same datatype as source table.
    Use AdventureWorks2008R2;
    Go
    ---Insert data using SELECT INTO
    SELECT AddressLine1, City
    INTO BothellAddresses
    FROM Person.Address
    where City = 'Bothell';
    GO
    ---VERIFY DATA
    Select AddressLine1, City
    FROM BothellAddresses
    ---DROP TABLE
    DROP TABLE BothellAddresses
    GO
    2. INSERT INTO SELECT
    Below method will need table to be created prior to inserting data. Its really useful when table is already created and you want insert data from
    another table.
    Use AdventureWorks2008R2;
    Go
    ---Create Table
    CREATE TABLE BothellAddresses (AddressLine1 NVARCHAR(60), City NVARCHAR(30))
    ---Insert into above table using SELECT
    INSERT INTO BothellAddresses(AddressLine1, City)
    SELECT AddressLine1, City
    FROM Person.Address
    where City = 'Bothell';
    ---VERIFY DATA
    Select AddressLine1, City
    FROM BothellAddresses
    ---DROP TABLE
    DROP TABLE BothellAddresses
    GO
    Regards,
    Vishal Patel
    Blog: http://vspatel.co.uk
    Site: http://lehrity.com

  • How to do a select from a table whose name is saved in a field?

    Hello,
    This is an abap report.
    I need to do a select from a table whose name is saved in a field of Z* table.
    Like this..
    SELECT * FROM znodos.
       SELECT * FROM znodos-tabla.   "znodos-tabla contain the Z* table name
           move zodos-tabla-txtmd to.....
       endselect.
    endselect.
    Is this possible??
    Thanks

    Hi Ilie Aleman 
    Give variable name in ().. Variable name refers to variable which contains table name
    SELECT * FROM (znodos-tabla).
    Regards,
    Mohaiyuddin
    Edited by: Mohaiyuddin Soniwala on Jan 22, 2008 5:18 PM

  • How to perform a QBE in the tree table

    Hi,
    How can we perform the following actions on the tree table
    1) We wanted to display all the enabled children for all the node stamp. There is a column for the child node which can say whether tehy are enabled or not .
    we can have an action listener. But how can perform this query in the action listener. How to access all the childern in all the node stamps
    2) similarly we we want go back again to display all the children for all the node stamp, jow can we do that.
    As per my uderstanding we don't have toolbars filter for the tree table.
    any pointer or code snipet will help a lot
    Thanks
    Santosh

    Hi,
    you cannot disable nodes in the sense that they cannot be expanded or selected. What you can do though is to ignore node selections in a custom selection handler or to render the node different (e.g. using an output text component instead of a command link or image etc.). What is your functional requirement for what disabled nodes should not do
    Frank

  • How to data from selected row of a TABLE

    Hi ,
    I have a table displaying some PO order details. In the last column , I have a field with element Link to Action . My requirement is , that when I click on the link to action column of specific row , I want to get the data present( specifically the the data present on the first column which is PO number) i that specific row.
    How can i get this data?
    Regards
    PG

    Hi PG,
    If you want to get Selected row data,  you can use get_selected_elements
    Try this example code.. this gives you selected records into one table..
      DATA lo_nd_et_postab_1 TYPE REF TO if_wd_context_node.
      DATA lo_el_et_postab_1 TYPE REF TO if_wd_context_element.
      DATA ls_et_postab_1 TYPE wd_this->Element_et_postab_1.
      DATA lt_et_postab_1 TYPE wd_this->Elements_et_postab_1.
    DATA: wa_temp TYPE REF TO if_wd_context_element,
            lt_temp TYPE wdr_context_element_set.
    navigate from <CONTEXT> to <ET_POSTAB_1> via lead selection
      lo_nd_et_postab_1 = wd_context->path_get_node( path = `ZSHP_EXTENDED_DUE_LI.CHANGING_3.ET_POSTAB_1` ).
      CALL METHOD lo_nd_et_postab_1->get_selected_elements
        RECEIVING
          set = lt_temp.
      LOOP AT lt_temp INTO wa_temp.
        CALL METHOD wa_temp->get_static_attributes
          IMPORTING
            static_attributes = ls_et_postab_1.
        append ls_et_postab_1 to lt_et_postab_1.
        CLEAR ls_et_postab_1.
      ENDLOOP.
    lo_nd_et_postab_1->bind_table( new_items = lt_et_postab_1 ).
    NOW LT_ET_POSTAB_1 Contains selected records in table.
    Hope it helps...
    Cheers,
    Kris.

  • How to freeze the selection column in the table control of the module pool.

    hi ,
    in my module pool there is a row selection field  <b>w/selcolumn</b> of the table control called as mark.
    how to freeze the selection column where there is no record in the table control row.
    or in other words where wa is initial.
    thanks
    ekta

    Hi all,
    in the PBO of the screen the following code is written.
    say the selection column is MARK and is declared in the data as well.
    thanks
    ekta
    *************************C O D E **************************************************
    MODULE disp_tabctrl1 OUTPUT.
      IF flag_c = 1.
        READ TABLE it_create_data INTO wa_material_data
             INDEX tab_ctrl1-current_line.
      ELSE.
        READ TABLE it_material_data INTO wa_material_data
             INDEX tab_ctrl1-current_line.
        IF sy-subrc = 0.
          IF ok_code_0101 = '&SEL1'.
            mark = 'X'.
          ELSEIF ok_code_0101 = '&DSEL'.
            mark = ' '.
          ENDIF.
        ELSE.
          LOOP AT SCREEN.
            IF screen-name = 'MARK'.
              screen-input = 0.
              MODIFY SCREEN.
            ENDIF.
          ENDLOOP.
        ENDIF.
      ENDIF.
      index_t = tab_ctrl1-top_line.
      index_d = tab_ctrl1-top_line + n.
    ENDMODULE.                 " DISP_TABCTRL1  OUTPUT

  • How to save the  selected rows from Advance table into database

    Hi
    I have requirement like..
    In custom page , Manager Search the Candidates and selects the candidate ROWS from advance table.
    The reqt is how to save the selected multiple rows into the database.

    hi Reetesh,
    In Custom page
    Supoose the Recruiter Search is for Position Finance Mangager , it retrieves 100 rows , out of which Recruiter select 10 rows .
    So in Such scenario how to save this 10 rows against Recruiter
    , i mean , Is i need to create custom table, to save Recruiter , these selected 10 rows.
    I hope u understand my question

  • How to move the selected rows from a  table control in dialog programming

    hiiiiiiii Every1
    I have to update some fields for a slected row in table control on click of a button and save it in database.
    Regards
    Sachin Dhingra

    see below example, I have added INSERT option after DELETE option, you can use same table or you can use differnt table by populating into that table and insert into the db table. If you want to use same internal table then use below code
    LOOP AT itab INTO demo_conn WHERE mark = 'X'.
    insert into table from itab.
    ENDLOOP.
    REPORT demo_dynpro_tabcont_loop_at.
    CONTROLS flights TYPE TABLEVIEW USING SCREEN 100.
    DATA cols LIKE LINE OF flights-cols.
    DATA: ok_code TYPE sy-ucomm,
    save_ok TYPE sy-ucomm.
    DATA: itab TYPE TABLE OF demo_conn.
    TABLES demo_conn.
    SELECT * FROM spfli INTO TABLE itab.
    LOOP AT flights-cols INTO cols WHERE index GT 2.
    cols-screen-input = '0'.
    MODIFY flights-cols FROM cols INDEX sy-tabix.
    ENDLOOP.
    CALL SCREEN 100.
    MODULE status_0100 OUTPUT.
    SET PF-STATUS 'SCREEN_100'.
    ENDMODULE.
    MODULE cancel INPUT.
    LEAVE PROGRAM.
    ENDMODULE.
    MODULE read_table_control INPUT.
    MODIFY itab FROM demo_conn INDEX flights-current_line.
    ENDMODULE.
    MODULE user_command_0100 INPUT.
    save_ok = ok_code.
    CLEAR ok_code.
    CASE save_ok.
    WHEN 'TOGGLE'.
    LOOP AT flights-cols INTO cols WHERE index GT 2.
    IF cols-screen-input = '0'.
    cols-screen-input = '1'.
    ELSEIF cols-screen-input = '1'.
    cols-screen-input = '0'.
    ENDIF.
    MODIFY flights-cols FROM cols INDEX sy-tabix.
    ENDLOOP.
    WHEN 'SORT_UP'.
    READ TABLE flights-cols INTO cols WITH KEY selected = 'X'.
    IF sy-subrc = 0.
    SORT itab STABLE BY (cols-screen-name+10) ASCENDING.
    cols-selected = ' '.
    MODIFY flights-cols FROM cols INDEX sy-tabix.
    ENDIF.
    WHEN 'SORT_DOWN'.
    READ TABLE flights-cols INTO cols WITH KEY selected = 'X'.
    IF sy-subrc = 0.
    SORT itab STABLE BY (cols-screen-name+10) DESCENDING.
    cols-selected = ' '.
    MODIFY flights-cols FROM cols INDEX sy-tabix.
    ENDIF.
    WHEN 'DELETE'.
    READ TABLE flights-cols INTO cols WITH KEY screen-input = '1'.
    IF sy-subrc = 0.
    LOOP AT itab INTO demo_conn WHERE mark = 'X'.
    DELETE itab.
    ENDLOOP.
    ENDIF.
    WHEN 'INSERT'.
    READ TABLE flights-cols INTO cols WITH KEY screen-input = '1'.
    IF sy-subrc = 0.
    LOOP AT itab INTO demo_conn WHERE mark = 'X'.
    itab1 = itab.
    modify itab1.
    ENDLOOP.
    ENDIF.
    if not itab1 is initial.
    INSERT dbtab FROM TABLE itab1.
    endif.
    ENDCASE.
    ENDMODULE.

  • How to highlight the selected value in a table

    Hi All,
    I have list of roles in a table like admin,dba...etc.
    i will select the current selected role using
      Stgin role =wdCotext.currentElement().getRole();
    After getting the role i bind this value in context.
    now my requiremnet is i will navigate to other table view containing all roles like admin,dba,networkadmin .
    I have to highlight the selected role in previous table to new table containing the  all the roles.
    means selected role should be highlight in a next table view list of roles.
    please help.

    Hi mohammad,
    To achieve this functionality, use another integer variable to store the index of the currently selected element in the first view. Pass this variable's value to the next view (either as plug parameter or throgh mapping) and set the Lead selection for the table as this index in the second view.
    In the first View ....
    int n = wdContext.current<your_node>Element().index()
    pass this value from first view to second view.
    In the second view
    wdContext.node<your_node>().setLeadSelection(n)
    Hope this helps,
    Best Regards,
    Nibu.
    Message was edited by: Nibu Wilson

  • How to pass multiple selected values of a table to RFC

    Hi all,
    My scenerio is that user selects multiple rows and click a button. i want to pass a unique id of the row to rfc.
    means there are userid is unique in the table.
    i want to paas the userids of all the selected rows to rfc.
    Can any one help me to implement that??
    regards
    Anoop

    Hi
    In the context view the properties of the model node for which you have to pass the table data.
    In the properties you will find a property called Structure from where you can get the structure name..in my example scenario the structre name is
    Zdp_Str_Shopping_Cart
    After adding all the datas to the model node, finally add that instance to the root node....
    You want to pass only the selected data in the table right, for that refer the looping concept suggested by of Ayyapparaj KV
    Zdp_Str_Shopping_Cart shoppingCart;
    for(int i=0;i<wdContext.nodeChnIt_Shopping_Cart().size();i++)
                   shoppingCart = new Zdp_Str_Shopping_Cart();
                   shoppingCart.setEquipment_No(wdContext.nodeChnIt_Shopping_Cart().getChnIt_Shopping_CartElementAt(i).getEquipment_No());
                   shoppingCart.setIbase(wdContext.nodeChnIt_Shopping_Cart().getChnIt_Shopping_CartElementAt(i).getIbase());
                   shoppingCart.setKbetr(wdContext.nodeChnIt_Shopping_Cart().getChnIt_Shopping_CartElementAt(i).getKbetr());
                   shoppingCart.setMaktx(wdContext.nodeChnIt_Shopping_Cart().getChnIt_Shopping_CartElementAt(i).getMaktx());
                   shoppingCart.setMatnr(wdContext.nodeChnIt_Shopping_Cart().getChnIt_Shopping_CartElementAt(i).getMatnr());
                   shoppingCart.setQuantity(wdContext.nodeChnIt_Shopping_Cart().getChnIt_Shopping_CartElementAt(i).getQuantity());
                   shoppingCart.setSerial_No(wdContext.nodeChnIt_Shopping_Cart().getChnIt_Shopping_CartElementAt(i).getSerial_No());
                   shoppingCart.setUnit(wdContext.nodeChnIt_Shopping_Cart().getChnIt_Shopping_CartElementAt(i).getUnit());
                   shoppingCart.setUnit_Of_Wt(wdContext.nodeChnIt_Shopping_Cart().getChnIt_Shopping_CartElementAt(i).getUnit_Of_Wt());
                   shoppingCart.setWaers(wdContext.nodeChnIt_Shopping_Cart().getChnIt_Shopping_CartElementAt(i).getWaers());
                   shoppingCart.setWeight(wdContext.nodeChnIt_Shopping_Cart().getChnIt_Shopping_CartElementAt(i).getWeight());
                   createProductOrder.addIt_Shopping_Cart(shoppingCart);           
              wdContext.nodeZdp_Bapi_Create_Product_Order_Input().bind(createProductOrder);
    Let me know whether you got an idea and your problem get solved......
    Regards
    Chandran S

  • How to populate the selected row details of table in the next view?

    hi,
    Im having a table, on selecting a particular row of a table by clicking on a radio button. i need that row details to be passed on(populated) to the next view when i navigate to that view by clicking on a button?
    Thanks & Regards,
    Suresh

    Hi Suresh,
    Your scenario is simple. Just follow the ex as shown below
    (Assuming you want default selection view provided by table itself.)
    1>Create 2 views (Ex:A and B)
    2>Create a Context with a node and attributes(For Ex:Person as node and Fname and Last Name as attributes
    2>In A view create a table with F name and L Name(map to context as well) and a action button to navigate to B view when you selected a particualr row o.k
    3>In B view, create a TextView with mapping to LastName(or all the data if you want from input selection) from the context
    If you want you can add back action button from B view to A view for easy navigation.
    4>Execute the application and select any row in the table appeared, press next action button,you can see that the selected row details will be shown in second(B) view.
    If you do the above example, I think you can easily find the solution for navigation issue.. try it out.
    In case if you are not able to ...let me know..I have that example.
    Hope that helps
    Regards
    Praveen

  • How to reatin record selection in a master table

    Hi,
    I have table A with single selection item and detail table B below, both VO based. Once single selection is clicked the detail table shows corresponding result record set. I need to retain the selection of the record in table A once query on table B. is there was to do that?
    Thank you
    Anatoliy

    Hi,
    Refer below links:
    http://oracle.anilpassi.com/master-detail-screen-in-oa-framework-part-i.html
    https://kr.forums.oracle.com/forums/thread.jspa?threadID=2364464
    --Sushant                                                                                                                                                                                                                                                                                                                                                                               

  • How to perform multiple selection from dropdown list

    Hi,
           I have developed one application in which i have taken one dropdownbyindex and entered some values in it, now i need to have multiple selection of entries, can any one please suggest how to do these
    Regards,
    Prasanna

    A drop-down list is by definition a 1-from-many selector.
    Armin

  • How to perform 'block selection' in table control?

    Hi,
    I'm looking for an easy way to implement block selection in a table control (i.e. select one line, hit 'block select' button, select a second line, hit 'block select' button again, and all lines between first and last are selected).
    I'm currently with a customer who is on 4.0b, so I can't use the wizard, it didn't exist back then.
    I hope someone knows
    regards,
    rs

    Rob,
    I generated a quick screen with wizard doing the multiple select.  This is the code chucks it generated
    in _TOP  first part is my definition of the table to be displayed and the one column on it.
    TABLES:   zsd_act_salespr.
    DATA:
      BEGIN OF wk_rec.
            INCLUDE STRUCTURE zsd_act_salespr.
    DATA:
        sel    TYPE c,
      END OF wk_rec,
      it_wk   LIKE STANDARD TABLE OF wk_rec.
       controls: TBLCLT_MAIN type tableview using screen 0100.   " Table Control on screen
    in I01
    *&spwizard: input modul for tc 'TBLCLT_MAIN'. do not change this line!
    *&spwizard: mark table
    module TBLCLT_MAIN_mark input.
      data: g_TBLCLT_MAIN_wa2 like line of IT_WK.
      if TBLCLT_MAIN-line_sel_mode = 1.
         loop at IT_WK into g_TBLCLT_MAIN_wa2
           where SEL = 'X'.
           g_TBLCLT_MAIN_wa2-SEL = ''.
           modify IT_WK
             from g_TBLCLT_MAIN_wa2
             transporting SEL.
         endloop.
      endif.
      modify IT_WK
        from WK_REC
        index TBLCLT_MAIN-current_line
        transporting SEL.
    endmodule.
    in the screen workflow
    PROCESS BEFORE OUTPUT.
    *&spwizard: pbo flow logic for tablecontrol 'TBLCLT_MAIN'
      module TBLCLT_MAIN_change_tc_attr.
    *&spwizard: module TBLCLT_MAIN_change_col_attr.
      loop at   IT_WK
           into WK_REC
           with control TBLCLT_MAIN
           cursor TBLCLT_MAIN-current_line.
    *&spwizard:   module TBLCLT_MAIN_change_field_attr
      endloop.
    PROCESS AFTER INPUT.
    *&spwizard: pai flow logic for tablecontrol 'TBLCLT_MAIN'
      loop at IT_WK.
        chain.
          field WK_REC-SPNUM.
        endchain.
        field WK_REC-SEL
          module TBLCLT_MAIN_mark on request.
      endloop.
    *&spwizard: module TBLCLT_MAIN_change_tc_attr.
    *&spwizard: module TBLCLT_MAIN_change_col_attr.
    in the O01
    *&spwizard: output module for tc 'TBLCLT_MAIN'. do not change this line!
    *&spwizard: update lines for equivalent scrollbar
    module TBLCLT_MAIN_change_tc_attr output.
      describe table IT_WK lines TBLCLT_MAIN-lines.
    endmodule.
    in the F01 nothing
    Edited by: Paul Chapman on Apr 24, 2008 4:56 PM

  • How to pass the selected data of a table to SAP(R/3)

    Hi all,
             I have a table with 2 colums and 10 records.Now i want to send the selected record to the R/3.
    Once the user checks the checkbox i retrivred the colum values of that row into a string array.
    and i added all the string arrays to an AbstractList.
    Now in function module it is giving as setT_Operations(AbstractList)
    but it is giving error..can u plz tell me how the function module understands the individual String arrays
    Regards
    Padma N

    Hey try to accept the values of a column as import parameters to a BAPI and you can send the data.But this will work out for a single data.
    In the BAPI insert the data making use of insert command.
    ITAB-COLUMN1 = IMPORT PARAMETER 1.
    ITAB-COLUMN2 = IMPORT PARAMETER 2.
    INSERT INTO VALUES ITAB.
    If you want to insert multiple records then you have to change the cardinality of the node.And add the node elements to the node.If any doubts then do reply.

Maybe you are looking for