Defaulting variable in select query

Hi Experts,
  I am having some different requirement here like, while selecting data from a table, for some column need to deafult some constant value for all lines if the actual value in the DB table has no value. There are some aggregate results like MAX( [DISTINCT] col ), MIN( [DISTINCT] col )...COUNT( * ) (or count(*)) , But there is nothing as such to fill constant value in query time it self while selecting data.
  Do help me if any one come across such a like.
Ex:
   Normal code we write:
   SELECT carrid connid cityfrom cityto
       FROM spfli
       INTO CORRESPONDING FIELDS OF wa.
     WRITE: / wa-carrid, wa-connid, wa-cityfrom, wa-cityto.
   ENDSELECT.
   Required code need to write:
SELECT NVL(e.carrid,1000) connid cityfrom cityto
       FROM spfli
       INTO CORRESPONDING FIELDS OF wa.
  WRITE: / wa-carrid, wa-connid, wa-cityfrom, wa-cityto.
ENDSELECT.
Thanks in advance.
Mohan.

Hi Mohan
As per my knowledge, we do not have the provision of the function that you are looking in ABAP as we have only 5 aggregate functions. Below are the same for your reference.
1. MAX
2. MIN
3. AVG
4. SUM
5. COUNT
I can understand your concern on the performance, but i guess there is not other way to go. Since you need to modify only one field, you can try with a logic similar to the below one:
SELECT carrid connid cityfrom cityto
       FROM spfli
       INTO TABLE itab.
wa-carrid = '1000'.
MODIF itab FROM wa TRANSPORTING carrid WHERE carrid IS INITIAL.
I guess this should be even better than modifying in the SELECT itself...
Hope this helps...
Kind Regards
Eswar

Similar Messages

  • How to store data in a variable in select query

    Hi,
    Below is the select query in which i want to store the data in variable but i am getting runtime error as the fields are more as compared to variable,the declaration is necessary for it due to the reteriving for data.
    Please provide me guidelines how to do it.
    DATA: BEGIN OF ITSC01 OCCURS 0,
          PRUEFLOS LIKE QALS-PRUEFLOS,
          MBLNR LIKE QAMB-MBLNR,
          LGORT LIKE MSEG-LGORT,
          END OF ITSC01.
    IF STIT-MGEIG GE 0.
    LOOP AT STIT." WHERE PRUEFLOS EQ STIT-PRUEFLOS.
        SELECT A~PRUEFLOS B~MBLNR C~LGORT FROM QALS AS A
               INNER JOIN QAMB AS B ON B~PRUEFLOS = A~PRUEFLOS
               INNER JOIN MSEG AS C ON C~MBLNR = B~MBLNR AND C~MJAHR = B~MJAHR
               INTO SLOC WHERE A~PRUEFLOS EQ STIT-PRUEFLOS.
    *           INTO TABLE ITSC01 WHERE A~PRUEFLOS EQ STIT-PRUEFLOS AND A~MATNR EQ STIT-MATNR AND C~LGORT EQ 'SC03'.
        SORT ITSC01 BY PRUEFLOS.
         READ TABLE ITSC01 WITH KEY PRUEFLOS = ITSC01-PRUEFLOS.
          IF ITSC01-LGORT EQ 'SC03'.
          STIT-MGEIG = 0.
          ENDIF.
    MODIFY STIT.
    ENDLOOP.
    ENDIF.

    hmm..
    try this:
    DATA: l_PRUEFLOS LIKE QALS-PRUEFLOS,
              l_MBLNR LIKE QAMB-MBLNR,
             l_LGORT LIKE MSEG-LGORT,
    IF STIT-MGEIG GE 0.
    LOOP AT STIT." WHERE PRUEFLOS EQ STIT-PRUEFLOS.
        SELECT APRUEFLOS BMBLNR C~LGORT FROM QALS AS A
               INNER JOIN QAMB AS B ON BPRUEFLOS = APRUEFLOS
               INNER JOIN MSEG AS C ON CMBLNR = BMBLNR AND CMJAHR = BMJAHR
               INTO ( l_PRUEFLOS , l_MBLNR, l_LGORT )
          WHERE A~PRUEFLOS EQ STIT-PRUEFLOS.
              INTO TABLE ITSC01 WHERE APRUEFLOS EQ STIT-PRUEFLOS AND AMATNR EQ STIT-MATNR AND C~LGORT EQ 'SC03'.
        SORT ITSC01 BY PRUEFLOS.
         READ TABLE ITSC01 WITH KEY PRUEFLOS = ITSC01-PRUEFLOS.
          IF ITSC01-LGORT EQ 'SC03'.
          STIT-MGEIG = 0.
          ENDIF.
    MODIFY STIT.
    ENDLOOP.
    ENDIF.

  • Variable has SELECT query how to execute it

    hi all
         question may be odd one but guide me on that.
    requirement is as ,  i have a BAPI which have a STRING type INPUT parameter let P1,  P1 contain a SELECT query as its value.
      now i want to execute the select query which P1 contain in my BAPI , how i can do it.
    e.g.
         P1 = 'Select single * from MARA .'  
    now  either i have extract the P1 OR have to do something to get this query executed .
    please guide me.

    Hi
    We can use the FM
    Earlier WS_QUERY was there but it is obsolete. we need to check for the new one.
    Or try  to use the FM
    GUI_EXEC to run it.
    Thanks & Regards,
    Chandralekha.

  • Variables in SELECT query

    consider the query
    SELECT v_column_name FROM v_table_name
    I have declared and defined v_column_name, v_table_name variable but still Oracle PL/SQL
    complains
    it says v_table_name not declared.
    If i put this query in a string and open
    with cursor it works fine.
    Please send me mail at [email protected]

    Does any one knows answer to this problem
    thanks
    Pramod

  • DYNAMIC VARIABLE IN SELECT QUERY

    hi all,
    i m selecting a belnr from one table and want to fire another query on different table wherein this belnr falls somewhere in middle of the whole field.
    eg: wa-belnr = 4000
    select * from cdhdr where
    objectclass = '%wa-belnr%.
    i want to select records which contains this belnr.
    but %wa-belnr% doesnt work.
    pls suggest if theres any solution.

    Hi,
    select * from cdhdr where
    objectclass = '%wa-belnr%.
    In the above query use LIKE instead of '='.
    Also instead of %wa-belnr% Use like this
    <b>DATA: str type string.
    concatenate '%' wa_belnr '%' into str.
    select * from cdhdr where
    objectclass LIKE str.</b>
    Regards,
    Sesh

  • Select Query Problem

    Hi Experts,
    I am having a select query in which I am using a variable in the where condition but it is giving error. Please suggest how to use variable in the select query.
    The query I am using is a s below.
    select * from zexc_rec into table it_ZEXC_REC
          where
           LIFNR in S_LIFNR and
          DOCNO in S_DOCNO and
          DOCTYP in doc_typ and
          DATE1 in S_DATE1 and
          MATNR in S_MATNR.
    Here doc_typ is a variable.
    Thanks.

    Rahul,
    use RANGES type variable instead of variable . It acts as a select-options variable. Thn use this variable in SELECT query with IN.
    Eg :
    RANGES r_t510 FOR t510-lgart.
        r_t510-low = '1600'. 
        APPEND r_t510.
        r_t510-low = '3190'. 
        APPEND r_t510.
    Note  : can be use SIGN, OPTIONS properties too in RANGES type.
    More deatils go through on HELP of RANGES
    Rgds,
    Ranjith

  • How to set default value in select option for ABAP query

    Hi experts,
    What is the way to set up default values for select-options in ABAP query.
    e.g.
    I have one field 'Year' in my ABAP query selection screen.
    I want value of current year to be appeared here whenever user execute report
    Thanks in Advance
    -Harkamal

    Hi Harkamal,
    execute your Query via SQ01. On Selection-Screen
    goto save Variant. Mark your field
    as selection variable an press Button election variable.
    Take variable from TVARV and use it.
    Than save the Variant.
    Look at TVARV if the 'Year' is updated to the actualYear!
    regards, Dieter

  • User exit variable I_STEP=1 does not show default value WebI selection scr

    Hello,
    We have a BW query with a user exit variable. We have written user exit code under I_STEP = 1 so that the default value for the field is displayed on the selection screen to the user. Works very well on the BW side.
    We based a universe on top of the query and then created a WebI report.
    When the WebI report is executed the default value is NOT seen on the selection screen,
    Note:
    1. If the user exit variable is optional and we execute the report, the default value is NOT seen on the selection screen. However if the report is executed without entering any value for the field the logic under I_STEP = 1 is picked up and the report gets the correct output
    1. If the user exit variable is mandatoryand we execute the report, the default value is NOT seen on the selection screen. The user now has to enter the value for the field.
    The scheduled reports will work in case of optional user exit variable.
    However this amounts to loss of functionality.
    Is this a known bug?
    Is there a way to display default values based on user exit variables on the WebI selection screen?
    Kindly advice. Btw we are on XI 3.1 SP3
    Rgds
    Edited by: Anup Deshmukh on Jun 29, 2010 11:13 PM

    OSS Note 1285993 - "Support of Customer Exit Variables from BEx query into WebIntelligence via OLAP universe"  deals with this issue and has deemed it out of scope for XI 3.1 SP3.
    Rgds

  • Personalization and Default values in selection screen variables

    Hi everyone,
    I have certain questions regarding Front end part:-
    1)     Can we have multiple personalizations for a single user in Web templates; I know it is quite possible in BEx using Variants. Is there any such approach or any other solutions regarding it?
    2)     In our project we had a range selection screen customer exit variable on 0CALMONTH, which is showing a default value of 07.2007 to 09.2007, although we have removed the default values from the query variable and also checked the customer exits where it is used, but still it’s showing the value. We can’t delete and recreate the variable as such, as it is used in number of function module. Also these values are also transported to Quality system, where also it’s showing the same value.
    Did anyone face such a situation before?
    Eagerly waiting for a solution.....
    Thanks in advance

    Hi Chiran,
    Please try with following approach for multiple   personalizations for a single user in Web templates.
    While Defining Variable Choose the Option Copy personalization data from variable .
    The Features  of above approach is as follows.
    1)It will personalize variable values for Input Variables for each user.
    2)These values are saved for each variable as well as for user.
    3) At the time of defining variable it will allow us to enter one or more default values.All these values are appear in the variable screen when u execute the query or Web Application.
    So you can see the variable values in web also.Just try with this approach.
    May be it will resove u r issue some extent.
    Regarding u r Second Query Just try with Some tables, all the default values entries were saved in those tables. Delete default values from those entries.
    Thanks & Regards,
    Chandu.

  • How to edit the properties for existing variables in BEX query, so that I can get multiple input selections

    Dear fellow developers,
    I'm trying to edit an existing variable using BEX query, so that it can allow multiple input selections.
    As you can see in the screenshot attached, the option is selectable during creation.
    However, during editing of an existing field, this field (Details -> Basic Settings -> Variable Represents) is not selectable.
    Does anyone knows why, and how to remedy this?

    Yes you can do it at the table level.
    Go to SE11 enter table name as RSZGLOBV.
    Enter the technical name of variable in VNAM field..You need to change the value in VPARSEL column.
    Please make sure to get the where used list of this variable so that you can know the impact,if something goes wrong.
    Also change it in DEV and then transport across the landscape.
    PS:Same thing has been described in this blog as well
    Changing BI variable parameters
    Regards,
    AL
    Message was edited by: Anshu Lilhori

  • Select query with variable no of conditions

    Hi,
    I want to write a select query such that the conditions mentioned under WHERE clause can be variable .like :
    Select * from TABLE where F1 = var1 and F2 = var2 and F3 = var3.
    Now on the basis of some conditons the equations in where clause should be used.Say condition is variable value >6.
    Now in this case ,if
    var1 =7 var2 =3 var3 = 8 then the query shud behave like
    select * from TABLE where F1 = var1 and F3 = var3
    if :
    var1 =3 var2 = 4 var3 =3 the :
    select * from TABLE
    But only one query is to be wrote that satisfies all of these
    thanx
    Edited by: aachal on Feb 4, 2011 11:36 AM

    Hi Aachal,
    Use Dynamic select statement.
    Ex:
    data : lv_wherecond type string.
    Select * from TABLE where ( lv_wherecond ).
    Before Select stmt, code as follows.
    if var1 > 6.
           if lv_wherecond is initial.
              F1 = var1.
           else.
              concatenate lv_wherecond ' and F1 = var1'  into lv_wherecond.
           endif.
    elseif var 2 > 6.
            if lv_wherecond is initial.
               F2 = var2
            else.
               concatenate lv_wherecond ' and F2 = var2'  into lv_wherecond.
           endif.
    elseif var3 > 6.
          same as above ..
    endif.
    Hope it helps..
    Regards,
    Sravan Guduru.

  • How to execute a select query stored in variable

    Hello  helpers ,
    I have some "select queries" stored in the database . Now I can derive this query in some variable . How do I execute this query from the variable .
    example :
    Data Query type char50 .
    QueryVar = 'Select MATNR from MBEW where BWKEY = '0001' . '
    How do I execute this Query stoored in variable QueryVar in ABAP program ?
    Thanks a lot for helping .
    Regards
    Shashank

    Shashank,
    It is also possible to use (column_syntax) and (dbtab_syntax) together with (cond_syntax) when using SELECT statements in ABAP. For more info on (column_syntax) and (dbtab_syntax) just have a quick look at ABAP Keyword documention on SELECT statement (hit F1 on SELECT then scroll down to Select->Select result->Select Columns....)
    So in your case, you need to separate out (split) the value in 'wa_itab-query' into other variables or append into separate internal tables using common keys etc. - then looping at those tables with the common key (READ TABLE WITH KEY....) use the following syntax at the time of triggering the SELECT query:
    SELECT (column_syntax)
           FROM (dbtab_syntax)
           WHERE (cond_syntax).
    Also worth a look at this example below:
    PARAMETERS: p_cityfr TYPE spfli-cityfrom,
                p_cityto TYPE spfli-cityto.
    DATA: BEGIN OF wa,
             fldate TYPE sflight-fldate,
             carrname TYPE scarr-carrname,
             connid   TYPE spfli-connid,
           END OF wa.
    DATA itab LIKE SORTED TABLE OF wa
                   WITH UNIQUE KEY fldate carrname connid.
    DATA: column_syntax TYPE string,
          dbtab_syntax TYPE string.
    column_syntax = `c~carrname p~connid f~fldate`.
    dbtab_syntax = `( ( scarr AS c `
      & ` INNER JOIN spfli AS p ON p~carrid  = c~carrid`
      & ` AND p~cityfrom = p_cityfr`
      & ` AND p~cityto   = p_cityto )`
      & ` INNER JOIN sflight AS f ON f~carrid = p~carrid `
      & ` AND f~connid = p~connid )`.
    SELECT (column_syntax)
           FROM (dbtab_syntax)
           INTO CORRESPONDING FIELDS OF TABLE itab.
    LOOP AT itab INTO wa.
      WRITE: / wa-fldate, wa-carrname, wa-connid.
    ENDLOOP.
    Hope this helps.
    Cheers,
    Sougata.

  • How to Run a Select Query  stored in a Variable

    Hello,
    I have a following requirement:
    Result of one select query on Var1 , result of other select query in Var2 ,
    if Va2 = 'value11' OR Var2 = 'Value2' then Var1 = 'select query'. Now how can I run this SQL query at the end of the Pl/SQL?
    so I'm writing following query for the same:
    DECLARE
    qry nvarchar2(500);
    result nvarchar2(500);
    BEGIN
    select 'select TEXTVAL as "CHARG" FROM TABLE1 WHERE LOC =''[ParameterValue]'' and KEYNAME =''<<REPLACE>>''' INTO qry from dual;
    SELECT CASE WHEN count(RW."CountofBATCH") > 1 then 'Mixing'
    WHEN count(RW."CountofMAT") = 0 then 'None'
    ELSE 'Other'
    END
    INTO result
    FROM TABLENAME2 TT, XMLTable('/Rowsets/Rowset/Row' PASSING TT.XMLCOL
    COLUMNS
    "CountofBATCH" PATH '/Row[CLABS > 0]/CHARG',
    "CountofMAT" PATH '/Row[MATNR = "[Parameter Value]"]/MAT'
    ) AS RW
    where
    TT.PL = '[Parameter Value]' and
    TT.TANK = '[Parametr Value]' ;
    IF result = 'Mixing' OR result = 'None' THEN
    qry := replace( qry , '<<REPLACE>> ' , result);
    else
    qry := 'Nothing';
    END IF;
    This way the variable qry will have select statement. Now How can I run this qry variable to get the output of that select statement in the same query?

    you can use execute immediate if the output of the query is in the single query.
    that is very simple.
    have the query in the signle string and then pass like this
    declare
    qry varchar2(255);
    result varcharf2(2500);
    vempid number :=1;
    begin
    qry:='select empname from emp where empid=:empid';
    execute immediate qry into result using vempid;
    -----now the data result is in result
    end;

  • Want to See Selected Input Variables in the query output

    Hi al
    My client gives a requirment like they want to see selected variables in the query output. Like suppose if i am  entering Profit center, controlling area, version, caldayas a input on the screen. Now they want to see only Profit center and version on the query result.
    Please let me know if any one has this solution.
    Regards,
    Amit Shetye

    Hi
    Can you please give us user requirement and according to ure requirement so we given replies . If ure requirement please explain detail
    But it is not neccessary that we can put something in free char and all. Inspite of that they wanted to select the input variable as per their requirement. They dont want to restrict them in Free char.

  • Select query using variable

    Hi all,
    If I have a variable with multiple value (returned from bulk collect), how can I pass it into a select query as a condition?
    Eg.
    set serveroutput on
    declare
    type v_LockSessInfo is table of integer;
    empdata v_LockSessInfo;
    begin
    select unique sid bulk collect into empdata from v$lock;
    [select sid,serial#,username from v$session where sid in (empdata)]
    end;
    I hope to be able to pass in the variable collected, into the next select statement. How can I achieve it?
    Thanks in advance.
    Eugene

    Sorry for any confusion caused, I have almost 0 knowledge on pl/sql, only can survie on normal sql query.
    Further elaboration, the intention of my query is to query out any locking that exists in my DB. It meant to serve as a report to management, so I have to break it down into couple of sections for easier reference (minimising the complication that may arise from others who view the report).
    Couple of steps/procedures I am thinking of:
    1) query out the unique sid that exists in v$lock (blocking and blocked sessions) <<< this set of returned record (SIDs) should be stored in a variable.
    Eg. achived by the select statement "select unique sid from v$lock into [variable]"
    2) query v$sessions for the lock details from step 1's output. Username, sid, serial#, machine etc.
    Eg. To be achieved by "select sid,serial#...... from v$session where sid in [variable]; Same condition applies to 2 and 3.
    3) query v$locked_object to identify which are the blocking/blocked sessions, and who's blocking who from step 1's output.
    4) query the locked object and locked record (based on rowid locked) from step 1's output.
    This is all that I can think of at the moment. Any opinions that can perform the above activity will be appreciated alot. Hope I'm able to clear up any confusion triggered.
    PS: I can't re-query the SID as the SID may change or be lost upon the re-query therefore losing any evident. Thus I was thinking of putting it into a variable for consistent querying for step 2,3 and 4. All 4 steps will be performed within a single script.
    Regards
    Eugene
    Edited by: eteo78 on May 28, 2009 8:40 AM

Maybe you are looking for