Data declaration for result of dynamic query

Good day,
can you help me with declaration of data for query result that can be from any columns of any tables.
For example (this is from SAP documentation):
DATA: wa   TYPE spfli,
      ftab TYPE TABLE OF STRING.
APPEND 'CITYFROM' TO ftab.
APPEND 'CITYTO'   TO ftab.
SELECT DISTINCT (ftab)
       FROM spfli
       INTO CORRESPONDING FIELDS OF wa
       WHERE
         carrid   = 'LH'.
  WRITE: / wa-cityfrom, wa-cityto.
ENDSELECT.
This is clear to me, but I don't know how to do in case  FROM clause is generated dynamically too.
SELECT DISTINCT (ftab)
       FROM (tab1)
Thanks

Check out this sample program....
report zrich_0002.
type-pools: slis.
field-symbols: <dyn_table> type standard table,
               <dyn_wa>,
               <dyn_field>.
data: alv_fldcat type slis_t_fieldcat_alv,
      it_fldcat type lvc_t_fcat.
type-pools : abap.
data : it_details type abap_compdescr_tab,
       wa_details type abap_compdescr.
data : ref_descr type ref to cl_abap_structdescr.
data: new_table type ref to data,
      new_line  type ref to data,
      wa_it_fldcat type lvc_s_fcat.
selection-screen begin of block b1 with frame title text .
parameters: p_table(30) type c.
selection-screen end of block b1.
* Get the structure of the table.
ref_descr ?= cl_abap_typedescr=>describe_by_name( p_table ).
it_details[] = ref_descr->components[].
loop at it_details into wa_details.
  clear wa_it_fldcat.
  wa_it_fldcat-fieldname = wa_details-name .
  wa_it_fldcat-datatype = wa_details-type_kind.
  wa_it_fldcat-intlen = wa_details-length.
  wa_it_fldcat-decimals = wa_details-decimals.
  append wa_it_fldcat to it_fldcat .
endloop.
* Create dynamic internal table and assign to FS
call method cl_alv_table_create=>create_dynamic_table
             exporting
                it_fieldcatalog = it_fldcat
             importing
                ep_table        = new_table.
assign new_table->* to <dyn_table>.
* Create dynamic work area and assign to FS
create data new_line like line of <dyn_table>.
assign new_line->* to <dyn_wa>.
* Select Data from table.
select * into table <dyn_table>
           from (p_table).
* Write out data from table.
loop at <dyn_table> into <dyn_wa>.
  do.
    assign component  sy-index  of structure <dyn_wa> to <dyn_field>.
    if sy-subrc <> 0.
      exit.
    endif.
    if sy-index = 1.
      write:/ <dyn_field>.
    else.
      write: <dyn_field>.
    endif.
  enddo.
endloop.
Regards,
Rich Heilman

Similar Messages

  • Unable to retrieve results using dynamic query

    Hi Experts,
    I have created a custom table and have created a custom bol to integrate it with web ui. I have redefined the dynamic query result method of genil layer. I find the data into LT_RESULT but when I invoke the root list method the LR_OBJECT does not contain the values.
    Please see below the code that I have written.
    ====================================
    METHOD IF_GENIL_APPL_INTLAY~GET_DYNAMIC_QUERY_RESULT.
    DATA: LR_OBJECT TYPE REF TO IF_GENIL_CONT_ROOT_OBJECT,
    LT_RESULT TYPE TABLE OF ZCRMST_XXXX,
    LV_DYN_WHERE TYPE STRING,
    LV_LEN TYPE I,
    LS_RANGE TYPE SELOPTOBJ.
    DATA: LT_XXXX TYPE TABLE OF SELOPTOBJ,
    LT_YYYY TYPE TABLE OF SELOPTOBJ.
    FIELD-SYMBOLS: <LFS_RESULT> TYPE ZCRMST_XXXX,
    <LFS_SELECTION_RANGE> TYPE GENILT_SELECTION_PARAMETER.
    decomposition of selection parameters and build a dynamic where condition
    SELECT * FROM ZXXXX INTO TABLE LT_RESULT[].
    CHECK LINES( LT_RESULT[] ) > 0.
    LOOP AT LT_RESULT[] ASSIGNING <LFS_RESULT>.
    LR_OBJECT = IV_ROOT_LIST->ADD_OBJECT( IV_OBJECT_NAME = 'Root'
    IS_OBJECT_KEY = <LFS_RESULT>-XXXX ).
    CHECK LR_OBJECT IS BOUND.
    LR_OBJECT->SET_QUERY_ROOT( ABAP_TRUE ).
    ENDLOOP.
    ENDMETHOD.
    ==================================================
    Thanks in advance,

    Hi,
    Please check your get_objects method of the genil class. I made some changes to my implementation of get_objects method and it fixed the problem.
    Regards,
    Sandeep

  • Alternative for result from other query  and merge dimension option option

    Hi Everyone ,
    Am Developing one webi report over bex Query.
    Actual scenario is output of one webi report should be the input of other webi report.
    Eg:
    Table 1
    2010        Cus 1
    2010        Cus 2
    2011        Cus 3
    table 2
    cus1    m1   100
    cus2    m2   200
    Cus3    m1  400
    Report 1 designing 
    First report created using table 1 and prompt for year
    Report  2 designing
    Second report created using table2 and prompt for customer
    So when am Running first report it will ask for parameter year and if am selecting 2010 then the report will return C1 and C2
    this out put should e the input for report 2.
    So out put will be 100+200=300
    NOTE:1. Result from other query is not working in webi filter pane since am building on olap universe.
               2. Merge Dimension performance is very slow .
    Any Solution ?
    Regards,
    Kannan.B

    Hi,
    Thanks for ur reply
    As you said , If am giving hyperlink to other report .
    Eg: User selected Tamilnadu then report 1 opened  then  he has to click the some cell or hyperlink cell to view the actual report(2nd report).
    Suppose user Clicked that hyperlink cell and 2nd report opened and he is viewing the data for Tamil nadu and he decided to see the report for
    Andrapradesh so according to this logic he has to select first report and refresh the data for Andra and from there he has to come to 2nd report.
    totally 4 screen will be opened for seeing the two states report.
    So Some other alternative.......

  • Display results from dynamic query created and executed inside procedure

    Hi;
    I have created this code:
    CREATE OR REPLACE PROCEDURE RunDynamicQuery(Var1 IN VARCHAR2, Var2 IN VARCHAR2, VAR3 IN VARCHAR2) AS
    -- Do something
    -- That ends up with a variable holding a query.... (just an example)
    MainQuery :='select sysdate from dual';
    end RunDynamicQuery;
    How can I run this procedure and see the result on the dymanic query generated inside it?
    BEGIN
    compare_tables_content('VAR1','VAR2','VAR3');
    END;
    Expected Output for this given example:
    20-05-2009 11:04:44 ( the result of the dymanic query inside the procedure variable MainQuery :='select sysdate from dual';)
    I tested with 'execute immediate':
    CREATE OR REPLACE PROCEDURE RunDynamicQuery(Var1 IN VARCHAR2, Var2 IN VARCHAR2, filter IN VARCHAR2) AS
    -- Do something
    -- That ends up with a variable holding a query.... (just an example)
    MainQuery :='select sysdate from dual';
    execute immediate (MainQuery );
    end RunDynamicQuery;
    BEGIN
    compare_tables_content('VAR1','VAR2','VAR3');
    END;
    Output:"Statement processed'' (no sysdate displayed ! )
    Please consider that the collums in the query are always dynamic... PIPELINE Table would not work because I would need to define a container, example:
    CREATE OR REPLACE TYPE emp_tabtype AS TABLE OF emp_type;
    FUNCTION RunDynamicQuery (p_cursor IN sys_refcursor)
    RETURN emp_tabtype PIPELINED
    IS
    emp_in emp%ROWTYPE;
    BEGIN
    LOOP
    FETCH p_cursor
    INTO emp_in;
    EXIT WHEN p_cursor%NOTFOUND;
    PIPE ROW (...)

    That would be a nice solution, thanks :)
    ''For now'' I implemented like this:
    My dynamic query now returns a single string ( select col1 || col2 || col3 from bla)
    This way I don't have dynamic collumns issue, and from business side, this ''string'' format works for them.
    This way I can use the pipelines to get the result out...
    OPEN myCursor FOR MainQuery;
    FETCH myCursor
    INTO myRow;
    WHILE (NOT myCursor%notFound) LOOP
    PIPE ROW(myRow);
    FETCH myCursor
    INTO myRow;
    END LOOP;
    CLOSE myCursor;

  • All Dates displayed for "Characteristic Values" in Query Designer

    Hello Experts,
    We are on BI 7.0, level 13.  I am having an issue within the Query Designer with regards to dates.  I have a write-optimized DSO that contains 3 date fields (for example, ZDATE1, ZDATE2, and ZDATE3).  Each date InfoObject is of type DATS so the system automatically creates it with reference to 0DATE. 
    When I create a query in the Query Designer, on the left hand side, I expand the "Characteristic Values" node under each date field.  The Query Designer shows the same list of values for each of the 3 dates even though they are not valid values posted in the DSO for those fields.
    For example, ZDATE1 only has 1 value posted in the DSO (01/01/2005).
    ZDATE2 only has 1 value posted in the DSO (01/01/2006).
    ZDATE3 only has 1 value posted in the DSO (01/01/2007).
    Bute when I expand the "Characteristic Values" node in the Query Designer, I see ALL THREE values under each date field.  I would expect to only see the 1 value posted for the InfoObject in the DSO.  Also note that each InfoObject is defined to show "Only posted values in InfoProvider".
    It appears that Query Designer will show all values for the reference InfoObject 0DATE instead of the ones posted to the actual InfoObject in the DSO.  If I delete the data in the DSO, the Characteristic Values list still remains because they exist in 0DATE.  Anyone encounter this before?  How can I get the Characteristic Values list to only show posted values for that InfObject?
    Thanks for your help!
    J

    Thanks for the response.  I went into the DSO and right clicked on each of the Date fields.  I looked at the Provider-specific properties and there is the option for "Query Exec.FilterVal" which only restricts what values appear when restricting during execution, not during query creation.
    Is there someplace else I should look or someplace else I can make change to only display posted dates when creating a query?  Thanks!

  • StatBlockCalc without creating data channels for results

    I am trying to create the min, max, and mean for a group of channels (3 of them) but I do not want to create/store them in new data channels - I would rather use the StatMin, StatMax, and StatArithMean variables.  For example, using the script
    StatClipCopy = 0StatClipValue = 0StatFormat = ""StatResChn = 1Call StatBlockCalc("Channel","2-254","'[1]/Amp-Hours' - '[1]/Temperature A1'") '... StatDirec,RowNoStr,ChnNoStr
    causes 3 new channels (min/max/avg) with 3 items in each channelto be created in the data portal.  Rather than creating channels, I change the StatResChn = 0 which I thought should only store the values in the variables StatMin, StatMax, and StatArithMean  
    StatClipCopy = 0
    StatClipValue = 0StatFormat = ""StatResChn = 0Call StatBlockCalc("Channel","2-254","'[1]/Amp-Hours' - '[1]/Temperature A1'") '... StatDirec,RowNoStr,ChnNoStr  
    However this only appears to work is using a single channel (i.e. Amp-Hours or Temperature A1)
    Is there a way to get multiple Statistics values (statmin, statmax, etc) over multiple channels without creating separate channels in the data portal?
    Solved!
    Go to Solution.

    Jim,
    When you calculate the statistics for a block of channels, the result cannot be stored in one of the channels. Instead, it goes into one of the result variables for the statistics calculation function:
    The maximum would be stored in the StatTxt2(5) variable, a complete list of the result variables is below:
    StatTxt2(1)
    Index number
    StatTxt2(2)
    Measurement value sum
    StatTxt2(3)
    Measured value square sum
    StatTxt2(4)
    Minimum
    StatTxt2(5)
    Maximum
    StatTxt2(6)
    Arithmetic mean
    StatTxt2(7)
    Root mean square
    StatTxt2(8)
    Geometric mean
    StatTxt2(9)
    Harmonic mean
    StatTxt2(10)
    0.25 quantile (lower quartile)
    StatTxt2(11)
    0.50 quantile (median)
    StatTxt2(12)
    0.75 quantile (upper quartile)
    StatTxt2(13)
    Range
    StatTxt2(14)
    Standard deviation
    StatTxt2(15)
    Variance
    StatTxt2(16)
    Variation coefficient
    StatTxt2(17)
    Quartile distance
    StatTxt2(18)
    Relative variation coefficient
    StatTxt2(19)
    Average absolute deviation from mean
    StatTxt2(20)
    Average absolute deviation from median
    StatTxt2(21)
    Skewness
    StatTxt2(22)
    Kurtosis
    StatTxt2(23)
    Standard error
    Hope that helps,
    Otmar D. Foehner
    Business Development Manager
    DIAdem and Test Data Management
    National Instruments
    Austin, TX - USA
    "For an optimist the glass is half full, for a pessimist it's half empty, and for an engineer is twice bigger than necessary."

  • Conditions for results row in query

    Hi,
    I've query with conditions. Let us say without conditions query result has 10 records. But After applying conditions result has 4 records.
    When you check the result row the subtotals and totals are coming wrong. Result rows are considering the 10 records.
    How can I get the result row has to consider only those 4 records after conditions.

    Hi,
    I have the same problem. And I didn't find any solution.
    kind regards
    Tobias

  • Using Dynamic Query in For Loop

    I have a doubt whether i can use the result from dynamic query in the for loop.
    for example,
    declare
    v_sql varchar2(1000);
    v_Id INTEGER;
    begin
    v_sql := 'select id from table1 where id in ('||v_Id||')';
    FOR i in vsql LOOP
    dbms_output.put_line(i.id);
    end loop;
    end;
    The above query is possible ?

    And here's a basic example of opening up a cursor for your dynamic query...
    SQL> ed
    Wrote file afiedt.buf
      1  declare
      2    v_sql varchar2(2000);
      3    v_cur sys_refcursor;
      4    i     emp.sal%type;
      5  begin
      6    v_sql := 'select sal from emp';
      7    open v_cur for v_sql;
      8    loop
      9      fetch v_cur into i;
    10      exit when v_cur%NOTFOUND;
    11      dbms_output.put_line('Salary: '||i);
    12    end loop;
    13* end;
    SQL> /
    Salary: 800
    Salary: 1600
    Salary: 1250
    Salary: 2975
    Salary: 1250
    Salary: 2850
    Salary: 2450
    Salary: 3000
    Salary: 5000
    Salary: 1500
    Salary: 1100
    Salary: 950
    Salary: 3000
    Salary: 1300
    PL/SQL procedure successfully completed.
    SQL>

  • Parameter in form for dynamic query in report

    Hello,
    I want to send parameters to report for replacing a dynamic query. I don't know how to make it...
    1.I want my select statement like
    select * from emp
    &p_where_clause
    2.I want to transfer parameters to report to replace the &p_where_clause.
    3.I know I should create a parameter list first, and then use add_parameter built-in to add the parameter, but what I don't know is the correct format of the add_parameter statement.
    4.I know the common format is add_parameter(paramlist,'TEXT_PARAMETER',value) , is it the same format if I want to transfer a select statement to report such as "where deptno in ('25','28','30')" for replacing &p_where_clause in report?
    Thanks in advance
    Joseph

    I appreciate your reply, but I am still a little confused...
    Actually, I want to pass the where clause to report by different condition. Such as when the user choose the deptment 1, then I can transfer the "where dept_no = '1' and empno > 500", or user choose the deptment 2, I can transfer "where dept_no = '2' and company = '0001'". What I want is to pass different where clause to report according to different condition.
    So is it right the statement should be like
    DECLARE
    vWhere1 varchar2 := 'where dept_no = "1" and empno > 500';
    vWhere2 varchar2 := 'where dept_no = "2" and company = "0001"';
    BEGIN
    IF choose = 1 THEN
    ADD_PARAMETER(&P_WHERE_CLAUSE, TEXT_PARAMETER, vWhere1);
    ELSIF choose = 2 THEN
    ADD_PARAMETER(&P_WHERE_CLAUSE, TEXT_PARAMETER, vWhere2);
    END IF;
    END;
    THANKS AGAIN!!
    Joseph

  • Efficient or Better Ways for Data Declaration

    Hi Guys,
    Basically I've always programmed much the same way for a pretty long time now. Lately I've been asking myself the question - "how can i start improving on my tried and trusted ways".(oftern a linear programming approach). As part of this I'm examining ways to declare data better within in programs and to adopt a neat way to do so.
    This may seem pretty trivial but the first question I want to ask is how to declare data better
    e.g. I need to declare constants - each constant corresponds to a rule number; 1, 2,3,4,5,6 - 23 etc.
    Instead of declaring the following line 23 times - is there a better way to declare in such a way.
    Constants: c_rule_1 type c value '1' ,
                    c_rule_2  type c value '2',
                    etc etc etc
    Your thoughts are greatly appreciated.
    Thanks.
    DK
    Edited by: Damien Kesle on Nov 26, 2008 10:00 AM

    Hi,
    With my knowledge, data declarations for all constants with DATA comamnd itself is good practise in point of performance. Alternative is dynamic creation of variables. But it consumes more time and is not good to practise. Instead of crating individual parameters for each constant, you could create a structure and add all constants as fields as follows:
    constants: begin of con,
                       1 type c value '1',
                       2 type c value '2',
                    end of con.
    Regards,
    Prasanth

  • Ref Cursor Using Dynamic Query

    Hi,
    I need to use the ref cursor to fetch result from dynamic query.
    e.g.
    Open ref_test_tbl for Select * from tbl1 where tbl1.field1= :1
    and tbl1.field2 =:2 using i_v1,i_v2;
    The thing is i_v1, i_v2 are dynamic.
    i.e. using clause can include i_v3, i_v4 also.
    How to include dynamic variables in the using clause.
    thanks

    > How to include dynamic variables in the using clause.
    Cannot using a ref cursor.. (and anyone that post code that writes dynamic PL/SQL in order to achieve this, I will call an idiot).
    What you should be using in PL/SQL is a DBMS_SQL cursor. This allows you to create a fully dynamic SQL statement with bind variables. E.g.
    select * from tbl1 where col1 = :1
    select * from tbl1 where col2 = :1 order by 2
    select * from tbl1 where col3 between :0 and :1
    Using this dynamically created SQL statement, you can parse it using DBMS_SQL, determine the number of bind variables, and bind each of these dynamically.
    You can then execute the SQL and again, dynamically, determine just what the projection of the cursor is. How many columns are returned, their names, data types, precision and so on.
    This is what APEX (see http://apex.oracle.com) use extensively in order to run SQLs and render these as reports - all dynamically.
    DBMS_SQL is detailed in the [url http://download.oracle.com/docs/cd/B19306_01/appdev.102/b14258/d_sql.htm#BABEDAHF]Oracle® Database PL/SQL Packages and Types Reference manual.

  • Query on results from another query

    Post Author: Duncan1980
    CA Forum: Crystal Reports
    Is it possible to query on the results from another query.  I have a query that produces a list of document numbers.  I want to use the output from that query as the filter criteria in a another query in the same Universe. 
    The output from the first query can be as much as 2000, so it would be very time consuming to cut and paste these into a filter.
    Both reports are built, but can not figure out how to link the first report output to the filtering criteria on the second report.  I
    I am using business objects XI release 2 web intelligence 11.5.3.417 enterprise.

    Hi Vivek,
    It was not directly solved but I applied alternate logic to over come the issue.
    Here's what I did to overcome:
    I used a sub query in place of the whole result from another query.
    For Ex:
    Dim1 inlist result from another query1
    I made it as
    Dim1 inlist (Dim0)
    where Conditions.
    Here Dim0 is the object which we use for Result from another query and Conditions will be the necessary filter conditions to arrive proper Dim0.  Make sure proper context is formed for the sub query.
    Even though it resolved my problem, It introduces an new issue. It causes increase in query run time when huge set of data is returned from sub query.
    Please let me know if i haven't explained clearly.
    Hi Aris_BO,
    Sorry for not responding earlier.  The logic would probably make more queries null & not null. Thats why I was not advised to use it.
    Thanks
    LN

  • Issue with Results from Another Query (Error on Null value)

    Hi All,
    We have a WebI report using "Result from Another Query" option of BO XI R3.1. The report was running fine till recently the dimension object using result from another query had a null value. Report suddenly throwed error as the query filters are invalid.
    Is there a way to make this filter optional if no data/null value is there ? Because we need those null values in report as well.
    Thank you for your time.
    Thanks & Regards
    LN

    Hi Vivek,
    It was not directly solved but I applied alternate logic to over come the issue.
    Here's what I did to overcome:
    I used a sub query in place of the whole result from another query.
    For Ex:
    Dim1 inlist result from another query1
    I made it as
    Dim1 inlist (Dim0)
    where Conditions.
    Here Dim0 is the object which we use for Result from another query and Conditions will be the necessary filter conditions to arrive proper Dim0.  Make sure proper context is formed for the sub query.
    Even though it resolved my problem, It introduces an new issue. It causes increase in query run time when huge set of data is returned from sub query.
    Please let me know if i haven't explained clearly.
    Hi Aris_BO,
    Sorry for not responding earlier.  The logic would probably make more queries null & not null. Thats why I was not advised to use it.
    Thanks
    LN

  • To accept all date formats for variables

    Hi,
    I am getting a vadidation message on refresh of a query on entering European date format in the selection criteria "Please enter value in permitted format for variable XXXX"
    I need date to accept all date formats for variables to execute query.
    Currently US date format has been accomodated, On entering European date format i get the above validation message.
    Please suggest how do i supress this validation message or any other thoughts on the same.
    If anyone experienced the same before or applied any notes plz send the SAP Note number for the same. I am currently on BW 3.5.
    Message was edited by:
            tyson m

    tyson,
    why dont you control the date display through the user creation / user details option - then your date entries should sort themselves out.
    Arun

  • Help reqd - Data declaration

    Hi,
    I have a problem here. I am analyzing an existing program. This is an active prg in Production.
    In that I see usage of an internal table called 'itab1'. On double clicking on it (to find it's data declaration) it takes no-where. It asks if I would like to create it.
    In an include program (which in embedded in this main program), I find some declaration as follows:
      data: itab&1 like &2 occurs 0 with header line.
    Can you help me understand this? I need to change the data declaration for this 'itab1' for an upgrade.
    Regards,
    Sanju.

    Macros are defined using the following statement:
    DEFINE conversion.
      if &1 is not initial.
        &2 = c_X.
      else.
        &2 = space.
      endif.
    END-OF-DEFINITION.
    You apply it like described bellow:
      conversion var_1 var_return.
    My guess is that this internal table is being declared using the macro parameter. so, if &1 is valued with "10", that internal table will be named itab10.
    Not quite sure about that, but maybe that's what is happening. Try to find the place where the macro is being called and see if &1 value is '1'.

Maybe you are looking for

  • How can I save my e-mails?

    My accumulated e-mail list is so long that I think it having an effect on the Thunderbird performance, and there are many of the e-mails that I wish to save. Can you advise me how I can save e-mails that I will be able to refer to at a later date? Th

  • Why can't I open RAW camera files with Photoshop 6 anymore??

    Attempted to open file, as I've done hundreds of times before. I get pop up window saying software can't open file. Nikon cameras used in my office are all supported.

  • Saving data clusters

    Hi, I've just got a question concerning the best way to save my data. I'm using LV 7.0 and Vision 7.1 on a 2P Microscope. I'm acquiring images, heartbeat of the animal and a stimulation signal which all depend on the same internal clock. Now, for eac

  • USB Modem not being recognised in OSX

    Sorry if this is being posted in the wrong forum. I have an oldish Usb modem (Intel(R) AnyPoint(R) DSL Modem 3240 http://www.intel.com/support/network/dslmodems/sb/CS-009978.htm) that I have been using for a while with my PC laptop. I have recently u

  • Get form field value using an offset

    I have a Form created using Perl that has a table with a row for each invoice in a large table. I display all the rows in a table and allow the user to edit some info for each record: for ($item_cntr = 0; $cntr < $record_cnt; ++$item_cntr) { print qq