Add single code in a query result

how i will single code in query result
suppose
query is
select '25' from dual;
i want to result shows '25'

try
SELECT '''25'''
  FROM DUAL;
SELECT CHR(39) || '25' || CHR(39)
  FROM DUAL;

Similar Messages

  • How to add new codes into SAP Query/Infoset

    Hi Experts,
    First time working with SAP Query.
    I need to enhance an existing Query to add a new field into a report.  I have added new tables into this Infoset and joined my new  tables with existing tables and able to extract the new fields out on the report.  But here are my problems:
    Example of original output:
    X88888 500000
    X99999 400000
    Example of new output after adding my 2 new tables to get 1 new field to output:
    X88888 50000  01/08/2009
    X88888 50000  01/25/2009      <--- for X88888 customer, I want only this record w/ latest date to be output
    X88888 50000  01/22/2009   
    X99999 40000  03/09/2009
    X99999 40000  04/18/2009
    X99999 40000  04/19/2009      < -- for X99999, I want only this record w/ latest date to be output
    Where/How i can add the code to control this logic so that only 1 record per customer with the highest date can be output.  I looked at the query program and it looks very confusing and hard to understand.
    Please advise,
    Thanks,
    Sam

    Hi Sam,
    Instead on adding new table/tables , better way to use Direct Read from your single table.
    Please read the below steps to suffice your requirement.
    Assumption: Let your original table be A and second table is B.
                            Tables A and B are having field X1 and X respectively, using these fields, they can be joined.
                             You want field date1 of table B.
    Infoset Changes:
    1. Go to SQ02 (Enter Infoset name, click on Change button)
    2. Create an extra field E_date of type date (Sy-datum), by clicking on 'Extras' (F5) button.
    3. Select that field in one of the field group of your Infoset.
    4. Click on Code Icon (Shift+F8) displayed on application bar, you will be getting lists events in 'Code section'.
    5. In Data Section , declare an internal table like below code (here pseudo code)
    data: begin of itab_date occurs 0,
                       date1  like B-date1,
                       end of itab_date.
    6. In Recod processing Event write the following code.
    CLEAR itab_date.
    CLEAR E_date.
    SELECT date1 FROM B into table itab_date                           " Retrieving date field from table B
    WHERE X = A-X1.
    Sort itab_date by itab_date-date1  DESCENDING.                 "Sort internal table B in decendind order on date1
    READ TABLE itab_date index 1.                                            "Read first record of interanal table
    E_date  = itab_date-date1                                                      "Assigning top most date of internal table into field E_date.
    7. Generate the Infoset
    Query changes
    1.Go To SQ01 (in a new session),
    2.Give your Query name, click on change button, and select that field group in which you have added extra field.
    3.Select extra field.
    4.Go to basic list to select that field in your O/p list.
    5.Click on Test button, and see if you are getting desired result.
    Please let me know if you need further details.
    Regards,
    Dinesh

  • Splitting  single row to multiple query result

    Hi All,
    I have a column is a table with data seperated by new like . I need to write a select query which can split this to multiple rows .. can any one help me out .
    please see the xample
    select data_comm from customer where rownum=1
    data_comm
    serial=0
    parallel=1
    comm=1
    a single row contain a field called data_comm which has got data stored seperated by new_line
    I need to write a query which can split data on data_comm and give as multiple rows.
    Thanks in advance

    I think something like this would do the trick.
    Part 1:
    select substr(data_comm, 1, instr(data_comm,chr(10),1)) from customer ;
    Part 2:
    select substr(data_comm,instr(data_comm,chr(10),1)+1,instr(data_comm,chr(10),2)) from customer ;
    Part 3:
    select substr(data_comm,instr(data_comm,chr(10),2)+1,instr(data_comm,chr(10),3)) from customer ;
    Maybe the chr(10) is not the used newline char, but you can figure that one out yourself

  • QA: Designer's operation to Add one more Field to display in Query Result Web Part

    QUESTION ABOUT Query Result Web Part presentation +1 Field
    I'd be looking at a property of Web Part to look up Discussion Board through Query Result Web Part. Currently it displays 'Title' column of Discussion Board, and my caring requirement is presentation customization to hold double
    columns of 'Title'+'Updated Date'. How could I add one more field 'Updated Date' to display in addition to that preexisting 'Title' field?
    Any procedural steps to realize how to add Filed to display in Query Result Web Part?

    Hi Yoshihiro,
    As I understand, you want to add the field to display in Query Result Web Part in SharePoint 2013.
    Which web part does you use? Content query web part or search results web part?
    If you use search results web part, you could edit the discussion board result template and add the updated field in the template.
    You could go to Design Manager: Edit Display Templates (site setting-> look and feel->design manager->edit display template), download the Discussion Item.htm file, and edit the file. 
    After editing, upload the file.
    The articles below are about how to modify an existing Display Template in SharePoint 2013.
    http://www.learningsharepoint.com/2012/09/17/sharepoint-2013-the-new-display-templates-for-styling-your-content/
    http://blogs.technet.com/b/sharepoint_quick_reads/archive/2013/08/01/sharepoint-2013-customize-display-template-for-content-by-search-web-part-cswp-part-1.aspx
     If you use content query web part, you could edit the content query web part, in the Property Mappings section select the “Change the mapping of managed”, and add the “modifiedOWSDATE” (it means the last modified date) in the line, after
    that you could see the update date under the title.
    Best regards,
    Sara Fan
    TechNet Community Support
    Please remember to mark the replies as answers if they help, and unmark the answers if they provide no help. If you have feedback for TechNet Support, contact
    [email protected]

  • Adding code in ABAP Query(ABAP HR)

    I have created an ABAP Query for getting the Absence details of employee within a specified period.I am populating the fields
    Employee number, Organization Unit,Absence start date,Absence hours, Absence enddate.
    If an employee is in leave for 10 days, this information will be stored in PA2001 table,and this table is not Organization specific.
    During the specified period(20 days), if an employee is under two Organization units(5 days in 1st Org Unit,15 days in 2nd Org Unit). The query is extracting two records for the same employee, because of two Organization units. And while populating the data, the query is getting the Absence information from PA2001 table, and this is causing overlapping of data. So it is showing the Absence details for that employee as 40 days(20 days in 1st Org Unit and 20 days in 2nd Org Unit,Instead of total 20 days). So i would like to add validation over here after extraction of data from the PA2001 table,before displaying the data.
    I have tried adding the code in the infoset, at END OF SELECTION Event,but i am not able to change the list, as i am not able to access the tables used by the Query.
    Can anybody suggest me where to add the code in the Query, so that i will be able to solve this issue, instead of changing the code in the Report program generated by the Query.

    you can look at BAPI_PTIMEOVERVIEW_GET
    this gives the output back for the selected period so you don't get alle the records back from you're pa2001 table. (only this overview doesn't take into considerationd the distiction the difference between leave and illness)
    further on if you consider using the pa2001 table you can loop through the begda and endda and add 1 day for each record and programm the logic how you want to intrepet each day you'reself
    I use the data from perws which comes from the function HR_PERSONAL_WORK_SCHEDULE
    (which is also used in the BAPI mentioned above)
    and from PA0001 you can retrieve how long somebody was in 1 org unit !!
      LOOP AT it_2001 WHERE begda BETWEEN i_begda AND i_endda
                        OR endda BETWEEN i_begda AND i_endda.
          h_datum = it_2001-begda.
          IF it_2001-awart BETWEEN '0100' AND '0199'.
            WHILE h_datum <= it_2001-endda.
              READ TABLE it_perws WITH KEY datum = h_datum.
              IF sy-subrc = 0.
    * you're logic
              ENDIF.
              CALL FUNCTION 'FKK_DTE_ADD'
                EXPORTING
                  i_base_date               = h_datum
                 i_periode                 = 'D'
                 i_period_factor           = 1
    *       I_USE_FACCAL              = ' '
    *       I_WORKDAY_INDICATOR       = '-'
                IMPORTING
                  e_datum                   = h_datum
    *     EXCEPTIONS
    *       ERROR_OCCURED             = 1
    *       OTHERS                    = 2
              IF sy-subrc <> 0.
    * MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
    *         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
              ENDIF.
            ENDWHILE.
    kind regards
    arthur de smidt
    Edited by: A. de Smidt on Jul 16, 2008 8:47 AM

  • Add a Code to Change the ABAP Query

    Hi All,
    I have a requirement in which i need to add a check box to a selection screen in ABAP query i inserted this check box in the infoset .The report is using joins from table EKKO,EKPO and EKES and display all the Vendor Confirmation details but when the user clicks on the checkbox which is added on the selection screen the report should only display the latest date Vendor confirmation for each line item of the PO.So i need your help how and where should i add the code for this in the Query???
    Result when no Checkbox is ticked
    PO    LineItem     Date
    100       1           15.01.2010   
    100        1           15.02,2010
    100        1           15.02.2010
    Result when Checkbox is ticked
    PO    LineItem     Date
    100        1           15.02,2010
    100        1           15.02.2010
    Please help!!!
    Regards,
    KB

    HI,
    After selecting data from tables EKKO,EKPO and EKES in internal table, sort that internal table in descending order of date and in ascending order of PO number and line item and then delete adjacent duplicates from that internal table by comparing PO number and line item. This will give you the latest entry for each PO number and line item.
    PFB the sample code..
    *--after select query on EKKO, EKPO and EKES
    SORT itab  po_number ascending line_item ascending date descending.
    DELETE ADJACENT DUPLICATES FROM itab COMPARING po_number line_item.
    Let's say input file contains following data
    PO LineItem Date
    100 1 15.01.2010
    100 1 15.02.2010
    101 1 15.02.2010
    101 1 15.02.2010
    Above code will give you output as given below :
    PO LineItem Date
    100 1 15.02.2010
    101 1 15.02.2010
    Regards,
    Pranjali

  • I need to print one query result as a single row

    Hi,
    I need to print one query result as a single row ,which gives more than one value (of subinventories)and also i would like to print the quantity of that particular subinventory at particular place.Please suggest how can i do this in the report builder?

    Actually I need to print inventory report with subinventory break up.For that all subinventories of category code AB are taken as single row.Based on that subinventory value Quantity must be printed at that particular place.
    For ex
    Quantity
    Item no Description Subinventory Code AB_Abc AB_Def AB_ghi
    1 ***** 12 9
    2 ****** 8 5
    like that.I am waiting for the reply.Plz its some how urgent.
    Thank you,

  • How to add different query results in Report designer

    Hi,
    I am creating a report based on 3 queries.  At the end i need to show total of 3 query results.
    How can i add results of queries in report designer??
    thanks in advance

    Hi Siva,
    As u said that u want to show the results of the queries through report designer but i think it is used for formatted reports .
    If u want to see tha results of diffrent query then u can use workbook .
    In workbook u can also analyse reports easily by applying slice and dice techniques .
    from different queries result set will be displayed on workbook with formatting  and good look and feel.
    regards,
    Supriya

  • Is there a way to combine their query results into a single function

    say I have a bunch of functions that return a query:
    <cffunction name="getDeals" access="remote"
    returntype="query">
    <cffunction name="getDeals2" access="remote"
    returntype="query">
    is there a way to combine their query results into a single
    function that returns a struct of the various results?

    Umm, it's probably more suited to an array - effectively
    making it into a list of queries. You'd then loop the array and
    output each.
    I use a similar approach when creating system messages to the
    user. Most of my functions that are called do their bit, then at
    the end call another function I call "messages". This function
    grabs whatever my function sends and appends it to a messages
    array.
    This way enables me to output a long list of messages -
    because I could be calling several functions at the same time. If I
    just output a message from each function, each one would overwrite
    the other. So I'd get one message at a time.
    Immediately after it's output, I then destroy the message
    array so that old messages aren't kept inside of it.
    Mikey.

  • Add rows to query result?

    Is it possible to add rows to a SQL query's result, similar to the COMPUTE command in SQL*Plus?
    That is, for example, when the value for column A changes, add a row after the previous row (the last one before column A changed) to show the sum of the values of column B.
    e.g.
    <PRE>A B
    1 3
    1 4
    1 5
    12
    2 9
    2 -1
    2 -7
    1</PRE>

    SQL> create table sales (person,city,sales)
      2  as
      3  select 'Alice', 'Phoenix', 19 from dual union all
      4  select 'Alice', 'Tulsa', 11 from dual union all
      5  select 'Bob', 'Phoenix', 17 from dual union all
      6  select 'Bob', 'Tulsa', 9 from dual union all
      7  select 'Tony', 'Miami', 5 from dual union all
      8  select 'Tony', 'San Francisco', 4 from dual union all
      9  select 'Tony', 'San Francisco', 3 from dual
    10  /
    Tabel is aangemaakt.
    SQL> select case grouping_id(person,city,rowid)
      2         when 1 then 'Sub-Total for'
      3         when 3 then 'Sub-Total for'
      4         when 7 then 'Grand Total'
      5         end notes
      6       , person
      7       , city
      8       , sum(sales) sales
      9    from sales
    10   group by rollup(person,city,rowid)
    11  having grouping_id(city,rowid) != 1
    12      or count(*) > 1
    13   order by person
    14       , city
    15       , grouping(rowid)
    16  /
    NOTES         PERSO CITY               SALES
                  Alice Phoenix               19
                  Alice Tulsa                 11
    Sub-Total for Alice                       30
                  Bob   Phoenix               17
                  Bob   Tulsa                  9
    Sub-Total for Bob                         26
                  Tony  Miami                  5
                  Tony  San Francisco          4
                  Tony  San Francisco          3
    Sub-Total for Tony  San Francisco          7
    Sub-Total for Tony                        12
    Grand Total                               68
    12 rijen zijn geselecteerd.Regards,
    Rob.

  • Is there a way that I can add tracking code (like google analytics) to a Single Edition app?

    Is there a way that I can add tracking code (like google analytics) to a DPS Single Edition app? If not, does the apple app store offer any analytics for a DPS Single Edition app after it is published there?

    Take a look at a previous thread on this topic: http://forums.adobe.com/message/5099353

  • How to set query result to a single window

    I am using SQL Developer 2.1063, and not happy with the default setting of result window. Everytimes when I run a query, the result appear in a new result window. Over a couple of hour I have tense or hundreds of result tabs in the low window. It is not quite fun to remove them one by one.
    I am sure it can be set so that all result appear in one result window, as I am used to in the old SQ Developer version. But I failed to find it in Prefernces and in Help.

    -K- wrote:
    This is fixed in the available 2.1.1.
    Have fun,
    K. K
    I've just tested on 2.1.1 and i came to the same conclusion as user10369687
    - if you check the box for "Preferences - Database - Worksheet - Automatically Freeze Result Tabs" or click the pin button or run as script (F5) or highlight the sql and run as script (F5) you always get new query result tab.
    The option name suggest that if you check the box the same result tab will be used for different queries which is not true, is the oposite way.
    If the option name is changed than the confusion will be cleared.
    Dani

  • How i can add document nu in below query

    i want to add one field in below query
    field is bkpf-xblnr
    i want document number after d_text.
    here d_text = 'Payment for'
        CLEAR: tab_out-value,tab_out-name.
        READ TABLE tab_out WITH KEY 'D_TEXT'.
        tab_out-value = d_text.
        MODIFY tab_out INDEX sy-tabix.
    please help
    thanks in advanced.

    hi help yar i m totaly new for this
    i m pasting whole code here just see yar and where u changing please hight light that things
    thanks yar
    PROGRAM zfisp0001.
    *& Subroutine pool   ZFISP0001                                         *
    *& Author: Rakesh Rao on 01.04.05                                      *
    *& Modified by: Rakesh Rao on 15.07.2005                               *
    *& Description: This subroutine pool is called from Check and Payment  *
    *&              Advice SAPScripts                                      *
    TABLES: itcsy.
    TYPES: BEGIN OF t_itcsy.
            INCLUDE STRUCTURE itcsy.
    TYPES: END OF t_itcsy.
    DATA: tab_in  TYPE STANDARD TABLE OF t_itcsy WITH NON-UNIQUE
           DEFAULT KEY INITIAL SIZE 0.
    DATA: tab_out TYPE STANDARD TABLE OF t_itcsy WITH NON-UNIQUE
           DEFAULT KEY INITIAL SIZE 0.
    DATA: d_count(3) VALUE 0,
          d_netamt_tot TYPE p DECIMALS 2,
          d_tdsamt_tot TYPE p DECIMALS 2,
          d_grossamt_tot TYPE p DECIMALS 2,
          d_tdsamt TYPE p DECIMALS 2.
    DATA flag.
    DATA: bschl LIKE bseg-bschl.
    DATA: shkzg LIKE bseg-shkzg.
    clear: d_count,d_netamt_tot.
    *&      Form  ZPAYMENT
          text
    -->  p1        text
    <--  p2        text
    FORM zpayment TABLES tab_in STRUCTURE itcsy
                         tab_out STRUCTURE itcsy.
      tables : bsak.
      TYPES: imis_type_c20(20) TYPE c,
             imis_type_c10(10) TYPE c.
      DATA: l_intern TYPE imis_type_c20.
      DATA: e_intern TYPE imis_type_c10.
      DATA: i_extern TYPE imis_type_c10.
      DATA: d_belnr(10),
           D_VBLNR(10),
            d_bukrs TYPE bseg-bukrs,
            d_gjahr TYPE bseg-gjahr,
            d_gjahr1 TYPE bseg-gjahr, " 15.07.2005 Rakesh
            d_buzei TYPE bseg-buzei,
            d_augbl TYPE bseg-augbl,
            d_umskz TYPE bseg-umskz,
            d_bschl TYPE bseg-bschl,
            d_bktxt TYPE bkpf-bktxt,
            d_budat(10),
            d_rebzg TYPE bseg-rebzg,
            D_SHKZG TYPE REGUP-SHKZG,
            d_swnes TYPE regud-swnes,
            d_netamt TYPE p DECIMALS 2,
           d_tdsamt TYPE p DECIMALS 2,
            d_discamt TYPE regud-wskto,
            d_gross  TYPE regud-wrbtr.
      DATA: BEGIN OF t_bkpf,
             bukrs TYPE bkpf-bukrs,
             belnr TYPE bkpf-belnr,
             gjahr TYPE bkpf-gjahr,
             budat TYPE bkpf-budat,
             bktxt TYPE bkpf-bktxt,
             xblnr TYPE bkpf-xblnr,
            END OF t_bkpf.
      DATA: BEGIN OF t_bseg,
             bukrs TYPE bseg-bukrs,
             belnr TYPE bseg-belnr,
             gjahr TYPE bseg-gjahr,
             buzei TYPE bseg-buzei,
             umskz TYPE bseg-umskz,
             bschl TYPE bseg-bschl,
             qbshb TYPE bseg-qbshb,
             augbl TYPE bseg-augbl,
             rebzg TYPE bseg-rebzg,
             rebzj TYPE bseg-rebzj,
             sgtxt TYPE bseg-sgtxt,
             xref1 TYPE bseg-xref1,
             ebeln TYPE bseg-ebeln,
             shkzg type bseg-shkzg,
            END OF t_bseg.
      DATA: num VALUE ' ',
            d_result VALUE ' '.
      DATA: d_symbol VALUE '/',
            d_text(11) VALUE 'Payment for'.
      DATA: d_belnr1 LIKE bseg-belnr.
      DATA: wa_payrq TYPE payrq. " 15.07.2005 Rakesh
    18.05.2006
      data : wa_belnr like bseg-belnr,wa_augbl like bseg-augbl.
    18.05.2006
    06.09.2006
      data : d_other type p decimals 2.
    Read incoming data
    *break-point.
    *zpayment.
      CLEAR: d_belnr,d_bukrs,d_gjahr,d_result.
      clear d_tdsamt.
    Company code
      CLEAR tab_in-value.
      READ TABLE tab_in WITH KEY 'REGUP-BUKRS'.
      d_bukrs = tab_in-value.
    Document number
      CLEAR tab_in-value.
      READ TABLE tab_in WITH KEY 'REGUP-BELNR'.
      wa_belnr = d_belnr(10) = tab_in-value(10).
    Posting Key
      CLEAR tab_in-value.
      READ TABLE tab_in WITH KEY 'REGUP-BSCHL'.
      d_bschl = tab_in-value.
    Special G/L Indicator
      CLEAR tab_in-value.
      READ TABLE tab_in WITH KEY 'REGUP-UMSKZ'.
      d_umskz = tab_in-value.
    Fiscal year
      CLEAR tab_in-value.
      READ TABLE tab_in WITH KEY 'REGUP-GJAHR'.
      d_gjahr = tab_in-value.
    <-- 15.07.2005 Rakesh
    Fiscal year1 - For Payment request case
      CLEAR tab_in-value.
      READ TABLE tab_in WITH KEY 'REGUD-GJAHR'.
      d_gjahr1 = tab_in-value.
    15.07.2005 Rakesh -->
    Document item
      CLEAR tab_in-value.
      READ TABLE tab_in WITH KEY 'REGUP-BUZEI'.
      d_buzei = tab_in-value.
    Discount amount, if any
      CLEAR tab_in-value.
      READ TABLE tab_in WITH KEY 'REGUD-WSKTO'.
      REPLACE ALL OCCURRENCES OF ',' IN tab_in-value WITH ' '.
      d_discamt = tab_in-value.
    Gross Amount
    CLEAR tab_in-value.
    READ TABLE tab_in WITH KEY 'REGUD-WRBTR'.
    REPLACE ALL OCCURRENCES OF ',' IN tab_in-value WITH ' '.
    d_gross = tab_in-value.
    Gross Amount
      CLEAR tab_in-value.
      READ TABLE tab_in WITH KEY 'REGUD-WNETT'.
      REPLACE ALL OCCURRENCES OF ',' IN tab_in-value WITH ' '.
      d_netamt = tab_in-value.
    Clearing document 18.05.2006
      CLEAR tab_in-value.
      READ TABLE tab_in WITH KEY 'REGUP-VBLNR'.
      wa_augbl = tab_in-value.
    DEBIT/CREDIT INDICATOR
      CLEAR tab_in-value.
      READ TABLE tab_in WITH KEY 'REGUP-SHKZG'.
      d_SHKZG = tab_in-value(1).
    IF  d_netamt < 0 AND D_SHKZG = 'S'.
         D_NETAMT = D_NETAMT * '-1'.
    ENDIF.
    Check document no.  is clearing document than reset wnett = 0.
    18.05.2006
      select single * from bsak where augbl = wa_augbl
                       and bukrs = d_bukrs and gjahr = d_gjahr1.
      if sy-subrc = 0 and wa_belnr = wa_augbl.
         d_netamt = 0.
      endif.
    18.05.2006
    Check if Down payment request
      IF d_bschl = '39'.
    This line item is not considered
        d_result = 'X'.
      ELSE.
    Check if Down payment
        IF d_bschl = '29'
            and wa_augbl = wa_belnr.  "18.05.2006
          IF d_umskz EQ 'A'
          OR d_umskz EQ 'B'
          OR d_umskz EQ 'I'
          OR d_umskz EQ 'M'
          OR d_umskz EQ 'D'.
    Put Document Number of the Payment Document value to 'd_belnr'
            CLEAR tab_in-value.
            READ TABLE tab_in WITH KEY 'REGUP-VBLNR'.
            d_belnr = tab_in-value.
          ENDIF.
        ENDIF.
    <--15.07.2005 Rakesh
    Check if Payment request exists in 'payrq'
        CLEAR wa_payrq.
    Convert 'd_belnr' to 10 digits,if required
        CLEAR: l_intern,e_intern.
        CALL 'CONVERSION_EXIT_ALPHA_INPUT' ID 'INPUT'  FIELD d_belnr
                                           ID 'OUTPUT' FIELD l_intern.
        e_intern = l_intern+10.
        d_belnr1 = d_belnr.
        CLEAR d_belnr.
        d_belnr  = e_intern.
        SELECT SINGLE keyno augbl FROM payrq
         INTO CORRESPONDING FIELDS OF wa_payrq
          WHERE keyno = d_belnr.
        IF sy-subrc = 0. " Yes
    Take clearing document which is the required accounting document
            d_belnr = wa_payrq-augbl.
    Convert 'd_belnr' to 10 digits,if required
          CLEAR: l_intern,e_intern.
          CALL 'CONVERSION_EXIT_ALPHA_INPUT' ID 'INPUT'  FIELD d_belnr
                                             ID 'OUTPUT' FIELD l_intern.
          e_intern = l_intern+10.
          CLEAR d_belnr.
          d_belnr  = e_intern.
    Assign 'd_gjahr1' to 'd_gjahr' as this is relevant here.
          d_gjahr = d_gjahr1. " 15.07.2005 Rakesh
    Get BSEG data
          SELECT SINGLE bukrs belnr gjahr buzei augbl rebzg rebzj
                        sgtxt qbshb umskz bschl xref1 ebeln
           FROM bseg INTO CORRESPONDING FIELDS OF t_bseg
            WHERE bukrs EQ d_bukrs
              AND belnr EQ d_belnr
              AND gjahr EQ d_gjahr
           AND buzei EQ d_buzei      " not relevant
           AND bschl EQ '25'           " Posting key - Outgoing payment
              AND koart EQ 'S'           " G/L accounts
              AND shkzg EQ 'S'.          " Debit
        ELSE. " No
    For all other cases, we use this final logic, even though we have
    taken ‘d_belnr’ value from accounting doc no., in some earlier cases.
    Put document number of the payment document(vblnr) value to 'd_belnr',
        IF d_bschl = '29'
            and wa_augbl <> wa_belnr.  "18.05.2006
          IF d_umskz EQ 'A'
            OR d_umskz EQ 'B'
            OR d_umskz EQ 'I'
            OR d_umskz EQ 'M'
            OR d_umskz EQ 'D'.
         else.
          CLEAR tab_in-value.
          READ TABLE tab_in WITH KEY 'REGUP-VBLNR'.
          d_belnr = tab_in-value.
         endif.
       else.
          CLEAR tab_in-value.
          READ TABLE tab_in WITH KEY 'REGUP-VBLNR'.
          d_belnr = tab_in-value.
       endif.
    -->15.07.2005
    Convert 'd_belnr' to 10 digits,if required
          CLEAR: l_intern,e_intern.
          CALL 'CONVERSION_EXIT_ALPHA_INPUT' ID 'INPUT'  FIELD d_belnr
                                             ID 'OUTPUT' FIELD l_intern.
          e_intern = l_intern+10.
          CLEAR d_belnr.
          d_belnr  = e_intern.
    Get BSEG data
          SELECT SINGLE bukrs belnr gjahr buzei augbl rebzg rebzj
                        sgtxt qbshb umskz bschl xref1 ebeln shkzg
           FROM bseg INTO CORRESPONDING FIELDS OF t_bseg
            WHERE bukrs EQ d_bukrs
              AND belnr EQ d_belnr
              AND gjahr EQ d_gjahr1
    "changed by Bharat 15.04.2006 - d_gjahr1 contains payment doc fiscal yea
    "r and d_gjahr contains invoice fiscal year
         AND buzei EQ d_buzei      "amisha 10-10-2005
         AND bschl EQ '25'           " Posting key - Outgoing payment
              AND shkzg EQ 'S'       "added by Bharat 17.04.2006
              AND ( koart EQ 'K' OR koart EQ 'D').
    <--15.07.2005 Rakesh
    18.05.2006
    in clearing document payment advice not consider TDS amount of
    payment document
         if wa_belnr <> wa_augbl and d_shkzg = 'S'.
            t_bseg-qbshb = t_bseg-qbshb * '-1'.
            d_netamt = d_netamt + t_bseg-qbshb.
            t_bseg-qbshb = 0.
         endif.
    18.05.2006
        ENDIF.
    -->15.07.2005
    Get corresponding BKPF data
    If Down payment
        IF d_bschl = '29'.
          IF d_umskz EQ 'A'
          OR d_umskz EQ 'B'
          OR d_umskz EQ 'I'
          OR d_umskz EQ 'M'
          OR d_umskz EQ 'D'.
            SELECT SINGLE bukrs belnr gjahr budat bktxt xblnr
             FROM bkpf INTO CORRESPONDING FIELDS OF t_bkpf
              WHERE bukrs EQ d_bukrs
                AND belnr EQ d_belnr
                AND gjahr EQ d_gjahr1.
    "changed by Bharat 15.04.2006 - d_gjahr1 contains payment doc fiscal yea
    "r and d_gjahr contains invoice fiscal year
          ENDIF.
        ELSE.
    If Partial payment/On-account payment
          IF t_bseg-augbl IS INITIAL.
    On-Account case
         IF t_bseg-augbl IS INITIAL.
            IF t_bseg-rebzg IS INITIAL.
              t_bseg-rebzg = t_bseg-belnr.   " Payment Document Number
              t_bseg-rebzj = t_bseg-gjahr.   " Fiscal year
            ENDIF.
         ENDIF.
            SELECT SINGLE bukrs belnr gjahr budat bktxt xblnr
             FROM bkpf INTO CORRESPONDING FIELDS OF t_bkpf
              WHERE bukrs EQ t_bseg-bukrs
                AND belnr EQ t_bseg-rebzg
                AND gjahr EQ t_bseg-rebzj.
          ELSE.
    FULL PAYMENT with Discount
           clear d_tdsamt.
    Following code added for GSPL by bharat06092006
    if d_bukrs eq 'GSPL'.
    DATA : V_TEXT(20) TYPE C.
        V_TEXT = 'W%'.
        SELECT SINGLE wrbtr FROM bseg
               INTO d_tdsamt
               WHERE bukrs EQ d_bukrs  AND
                     belnr EQ d_belnr1 AND
                     gjahr EQ d_gjahr  AND         " Added by bharat on 10-04-2006 fiscal year
                     bschl EQ '50'     AND
                     KTOSL EQ 'WIT'    AND
                     QSSKZ NOT LIKE V_TEXT.
        SELECT single wrbtr FROM bseg
           INTO d_other
           WHERE bukrs EQ d_bukrs  AND
                 belnr EQ d_belnr1 AND
                 gjahr EQ d_gjahr  AND         " Added by bharat on 10-04-2006 fiscal year
                 bschl EQ '50'     AND
                 KTOSL EQ 'WIT'    AND
                 QSSKZ LIKE V_TEXT.
    ELSE.
    Added by bharat 06.09.2006
            SELECT SINGLE wrbtr
             FROM bseg INTO d_tdsamt
              WHERE bukrs EQ d_bukrs
                AND belnr EQ d_belnr1
                AND gjahr EQ d_gjahr
                " Added by bharat on 10-04-2006 fiscal year
                AND bschl EQ '50' AND KTOSL EQ 'WIT'.
    ENDIF.
            SELECT SINGLE bukrs belnr gjahr budat bktxt xblnr
             FROM bkpf INTO CORRESPONDING FIELDS OF t_bkpf
              WHERE bukrs EQ d_bukrs
                AND belnr EQ d_belnr
                AND gjahr EQ d_gjahr1.
    "changed by Bharat 15.04.2006 - d_gjahr1 contains payment doc fiscal yea
    "r and d_gjahr contains invoice fiscal year
           tab_in-value
    *d_tdsamt_tot
          ENDIF.
        ENDIF.
        IF sy-subrc = 0.
    Sr. no. for Line items
          d_count = d_count + 1.
        ELSE.
    Else, this line item is not considered
          d_result = 'X'.
        ENDIF.
      ENDIF. " if d_bschl = '39'
    Pass the output to the outgoing table 'tab_out', if 'd_result' is not
    *set.
      IF d_result = ' '.
    Line count
        CLEAR: tab_out-value,tab_out-name.
        READ TABLE tab_out WITH KEY 'COUNT'.
        tab_out-value = d_count.
        MODIFY tab_out INDEX sy-tabix.
    Result flag
        CLEAR: tab_out-value,tab_out-name.
        READ TABLE tab_out WITH KEY 'RESULT1'.
        tab_out-value = d_result.
        MODIFY tab_out INDEX sy-tabix.
    Clearing document
        CLEAR: tab_out-value,tab_out-name.
        READ TABLE tab_out WITH KEY 'AUGBL'.
        tab_out-value = t_bseg-augbl.
        MODIFY tab_out INDEX sy-tabix.
        IF d_bschl <> '29'.
    On-Account case
          IF t_bseg-augbl IS INITIAL.
            IF t_bseg-rebzg IS INITIAL.
              t_bseg-rebzg = t_bseg-belnr.   " Payment Document Number
            ENDIF.
          ENDIF.
        ELSE.
    Payment document request
          IF NOT t_bseg-ebeln IS INITIAL. " First preference
            t_bseg-rebzg = t_bseg-ebeln.
          ELSE.
            t_bseg-rebzg = d_belnr.
          ENDIF.
        ENDIF.
    Number of the Invoice the Transaction Belongs to
        CLEAR: tab_out-value,tab_out-name.
        READ TABLE tab_out WITH KEY 'REBZG'.
        SHIFT t_bseg-rebzg LEFT DELETING LEADING num.
        tab_out-value = t_bseg-rebzg.
        MODIFY tab_out INDEX sy-tabix.
    Document number - check use??
       CLEAR: tab_out-value,tab_out-name.
       READ TABLE tab_out WITH KEY 'BELNR'.
       tab_out-value = d_belnr.                   " check!!
       MODIFY tab_out INDEX sy-tabix.
    Net amount
       d_netamt = d_gross - d_discamt - t_bseg-qbshb.
    Gross amount
        d_gross = d_netamt + d_tdsamt + t_bseg-qbshb.
        CLEAR: tab_out-value,tab_out-name.
        READ TABLE tab_out WITH KEY 'NETAMT'.
        tab_out-value = d_netamt.
        SHIFT tab_out-value LEFT DELETING LEADING space.
        MODIFY tab_out INDEX sy-tabix.
    TDS Amount
        IF t_bseg-qbshb IS INITIAL.
          CLEAR: tab_out-value,tab_out-name.
          READ TABLE tab_out WITH KEY 'TDSAMT'.
          tab_out-value = d_tdsamt.
          SHIFT tab_out-value LEFT DELETING LEADING space.
          MODIFY tab_out INDEX sy-tabix.
    Added by bharat 06.09.2006
         CLEAR: tab_out-value,tab_out-name.
         READ TABLE tab_out WITH KEY 'D_OTHER'.
         tab_out-value = d_other.
         SHIFT tab_out-value LEFT DELETING LEADING space.
         MODIFY tab_out INDEX sy-tabix.
    Added by bharat 06.09.2006
        ELSE.
          CLEAR: tab_out-value,tab_out-name.
          READ TABLE tab_out WITH KEY 'TDSAMT'.
          tab_out-value = t_bseg-qbshb.
          SHIFT tab_out-value LEFT DELETING LEADING space.
          MODIFY tab_out INDEX sy-tabix.
        ENDIF.
    *Calculate Total Net amount & TDS amount which is stored in Global
    *memory and accessed later
    from 'FORM Z_TOTAL_AMT'
        d_netamt_tot = d_netamt_tot + d_netamt.
        d_grossamt_tot = d_grossamt_tot + d_gross.
        d_tdsamt_tot = d_tdsamt_tot + t_bseg-qbshb + d_tdsamt + d_other.
    CLEAR: tab_OUT-value,tab_out-name.
    READ TABLE tab_OUT WITH KEY 'NETAMT_TOT'.
    tab_out-value = d_netamt_tot.
    shift tab_out-value left deleting leading space.
    MODIFY tab_out INDEX sy-tabix.
    Document Header text
        CLEAR: tab_out-value,tab_out-name.
        READ TABLE tab_out WITH KEY 'BKTXT'.
        IF d_bschl = '29'.
          tab_out-value = t_bseg-sgtxt.    " for down payment request
        ELSE.
          tab_out-value = t_bkpf-bktxt.
        ENDIF.
        MODIFY tab_out INDEX sy-tabix.
    Reference Document Number - Header
        CLEAR: tab_out-value,tab_out-name.
        READ TABLE tab_out WITH KEY 'XBLNR'.
        IF d_bschl = '29'.
          tab_out-value = t_bseg-xref1.   " for down payment request
        ELSE.
          tab_out-value = t_bkpf-xblnr.
        ENDIF.
        MODIFY tab_out INDEX sy-tabix.
    Symbol '/'
        CLEAR: tab_out-value,tab_out-name.
        READ TABLE tab_out WITH KEY 'D_SYMBOL'.
        tab_out-value = d_symbol.
        MODIFY tab_out INDEX sy-tabix.
    Text 'Payment for'
       CLEAR: tab_out-value,tab_out-name.
       READ TABLE tab_out WITH KEY 'D_TEXT'.
       tab_out-value = d_text.
       MODIFY tab_out INDEX sy-tabix.
      CLEAR: tab_out-value,tab_out-name.
        READ TABLE tab_out WITH KEY 'D_TEXT'.
        tab_out-value = d_text.
        tab_out-xblnr = t_bkpf-xblnr.
        MODIFY tab_out by bkpf-xblnr INDEX sy-tabix.
    Posting date
        CLEAR d_budat.
        CALL FUNCTION 'CONVERT_DATE_TO_EXTERNAL'
          EXPORTING
            date_internal            = t_bkpf-budat
          IMPORTING
            date_external            = d_budat
          EXCEPTIONS
            date_internal_is_invalid = 1
            OTHERS                   = 2.
        IF sy-subrc <> 0.
         MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
                 WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
        ENDIF.
        CLEAR: tab_out-value,tab_out-name.
        READ TABLE tab_out WITH KEY 'BUDAT'.
        tab_out-value = d_budat.
        MODIFY tab_out INDEX sy-tabix.
      ELSE.
    Result flag
        CLEAR: tab_out-value,tab_out-name.
        READ TABLE tab_out WITH KEY 'RESULT1'.
        tab_out-value = d_result.
        MODIFY tab_out INDEX sy-tabix.
      ENDIF.
    d_gross = d_netamt + d_tdsamt + t_bseg-qbshb.
      CLEAR: tab_out-value,tab_out-name.
      READ TABLE tab_out WITH KEY 'GROSSAMT'.
      tab_out-value = d_gross.
      SHIFT tab_out-value LEFT DELETING LEADING space.
      MODIFY tab_out INDEX sy-tabix.
    ENDFORM.                    " ZPAYMENT
    *&      Form  zamtwords
          text - If only 'Net Amount' is required
    -->  p1        text
    <--  p2        text
    FORM zamtwords TABLES tab_in  STRUCTURE itcsy
                          tab_out STRUCTURE itcsy.
      DATA: d_swnet  TYPE regud-swnes,  " take character format type
            d_swnet1 TYPE bseg-dmbtr.
           d_words(160),
           d_words1(80),
           d_words2(80).
    <-- 21.05.2005
      DATA: d_words(160),
            d_words1(80) TYPE c,
            d_words2(80) TYPE c.
      DATA: len TYPE i.
    <-- 21.05.2005
    *zamtwords
      CLEAR tab_in-value.
      READ TABLE tab_in WITH KEY 'REGUD-SWNET'.
      d_swnet = tab_in-value.
    REPLACE ALL OCCURRENCES OF '*' IN d_swnes WITH ' '.
      REPLACE ALL OCCURRENCES OF ',' IN d_swnet WITH ' '.
      d_swnet1 = d_swnet.
      CLEAR d_swnet.
      PERFORM z_conv_amt USING d_swnet1 CHANGING d_swnet.
    Return 'SWNET1' without commas
      CLEAR: tab_out-value,tab_out-name.
      READ TABLE tab_out WITH KEY 'SWNET1'.
    d_swnet = d_swnet1.
      tab_out-value = d_swnet.
      MODIFY tab_out INDEX sy-tabix.
    Amount in Indian currency format
      CALL FUNCTION 'Z_SPELL_AMOUNT_INR'
        EXPORTING
          amount     = d_swnet1
        IMPORTING
          rupees_str = d_words.
    Adjust 'd_words' left aligned
      SHIFT d_words LEFT DELETING LEADING space.
    <-- 21.05.2005
    Split 'd_words' into 'd_words1' and 'd_words2'
    since 'DEFINE' in SAPScript displays maximum 80 characters
    d_words1 = d_words(80).
    d_words2 = d_words+80(80).
    *CALL FUNCTION 'C147_STRING_SPLIT_AT_POSITION'
    EXPORTING
       i_string         = d_words
       i_position       = 80
    IMPORTING
       E_HEAD           = d_words1
       E_TAIL           = d_words2.
      len = STRLEN( d_words ).
    Only 80 characters can be passed to one SAPScript symbol
    Split 'd_words' into 'd_words1' and 'd_words2'
    since 'DEFINE' in SAPScript displays maximum 80 characters
    Check if string split is required
      IF len > 80.
        d_words1 = d_words(80).                                 " First 80
        d_words2 = d_words+80(80).                              " Next 80
    Introduce an initial space in 'd_words2' if 80th character in
    'd_words1' is space, as this space is not considered when 'd_words1'
    value is passed to the SAPScript, since we need proper spacing
    between 'd_words1' and 'd_words2' in the Cheque layout.
        IF d_words1+79(1) = ' '.
          SHIFT d_words2 RIGHT BY 1 PLACES.
        ENDIF.
      ELSE.
        d_words1 = d_words.
        d_words2 = space.
      ENDIF.
    21.05.2005 -->
    Amount in words1
      CLEAR: tab_out-value,tab_out-name.
      READ TABLE tab_out WITH KEY 'WORDS'.
      tab_out-value = d_words1.
      MODIFY tab_out INDEX sy-tabix.
    Amount in words2
      CLEAR: tab_out-value,tab_out-name.
      READ TABLE tab_out WITH KEY 'WORDS1'.
      tab_out-value = d_words2.
      MODIFY tab_out INDEX sy-tabix.
    ENDFORM.                    " zamtwords
    *&      Form  z_address
          text
    -->  p1        text
    <--  p2        text
    FORM z_address TABLES tab_in  STRUCTURE itcsy
                          tab_out STRUCTURE itcsy.
      DATA d_bukrs TYPE bseg-bukrs.
      DATA: t_t001 TYPE t001,
            t_adrc TYPE adrc.
      CLEAR: tab_in-value,t_t001,t_adrc.
      READ TABLE tab_in WITH KEY 'REGUP-BUKRS'.
      d_bukrs = tab_in-value.
      CALL FUNCTION 'K_READ_T001'
        EXPORTING
          i_bukrs   = d_bukrs
        IMPORTING
          e_t001    = t_t001
        EXCEPTIONS
          not_found = 1
          OTHERS    = 2.
      IF sy-subrc <> 0.
    MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
            WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
      ELSE.
        CALL FUNCTION 'RTP_US_DB_ADRC_READ'
          EXPORTING
            i_address_number       = t_t001-adrnr
      I_NATION               = ' '
      I_READ_DB              =
         IMPORTING
            e_adrc                 = t_adrc
         EXCEPTIONS
           not_found              = 1
           OTHERS                 = 2
        IF sy-subrc <> 0.
    MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
            WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
        ENDIF.
        CLEAR: tab_out-value,tab_out-name.
        READ TABLE tab_out WITH KEY 'ADRC-NAME1'.
        tab_out-value = t_adrc-name1.
        MODIFY tab_out INDEX sy-tabix.
        CLEAR: tab_out-value,tab_out-name.
        READ TABLE tab_out WITH KEY 'ADRC-STREET'.
        tab_out-value = t_adrc-street.
        MODIFY tab_out INDEX sy-tabix.
        CLEAR: tab_out-value,tab_out-name.
        READ TABLE tab_out WITH KEY 'ADRC-CITY1'.
        tab_out-value = t_adrc-city1.
        MODIFY tab_out INDEX sy-tabix.
        CLEAR: tab_out-value,tab_out-name.
        READ TABLE tab_out WITH KEY 'ADRC-POST_CODE1'.
        tab_out-value = t_adrc-post_code1.
        MODIFY tab_out INDEX sy-tabix.
      ENDIF.
    ENDFORM.                    " z_address
    *&      Form  z_total_amt
          text
    -->  p1        text
    <--  p2        text
    FORM z_total_amt TABLES tab_in STRUCTURE itcsy
                          tab_out STRUCTURE itcsy.
      CLEAR: tab_out-value,tab_out-name.
      READ TABLE tab_out WITH KEY 'NETAMT_TOT'.
      tab_out-value = d_netamt_tot.
      SHIFT tab_out-value LEFT DELETING LEADING space.
      MODIFY tab_out INDEX sy-tabix.
      CLEAR: tab_out-value,tab_out-name.
      READ TABLE tab_out WITH KEY 'TDSAMT_TOT'.
      tab_out-value = d_tdsamt_tot.
      SHIFT tab_out-value LEFT DELETING LEADING space.
      MODIFY tab_out INDEX sy-tabix.
      CLEAR: tab_out-value,tab_out-name.
      READ TABLE tab_out WITH KEY 'GROSSAMT_TOT'.
      tab_out-value = d_grossamt_tot.
      SHIFT tab_out-value LEFT DELETING LEADING space.
      MODIFY tab_out INDEX sy-tabix.
    ENDFORM.                    " z_total_amt
    *&      Form  Z_NET_AMT
          text
         -->TAB_IN     text
         -->TAB_OUT    text
    FORM z_net_amt TABLES tab_in  STRUCTURE itcsy
                          tab_out STRUCTURE itcsy.
      TYPES: imis_type_c20(20) TYPE c,
             imis_type_c10(10) TYPE c.
      DATA: l_intern TYPE imis_type_c20.
      DATA: e_intern TYPE imis_type_c10.
      DATA: i_extern TYPE imis_type_c10.
      DATA: d_belnr(10),
            d_bukrs TYPE bseg-bukrs,
            d_gjahr TYPE bseg-gjahr,
            d_umskz TYPE bseg-umskz,
            d_bschl TYPE bseg-bschl,
            d_dmbtr1 TYPE bseg-dmbtr,
            d_dmbtr(17). "prb16052006
           d_dmbtr1 like regud-swnes. "prb16052006
      DATA d_amt TYPE char18.
      DATA: BEGIN OF t_bseg OCCURS 10,
             bukrs TYPE bseg-bukrs,
             belnr TYPE bseg-belnr,
             gjahr TYPE bseg-gjahr,
             buzei TYPE bseg-buzei,
             dmbtr TYPE bseg-dmbtr,
             wrbtr TYPE bseg-wrbtr,
            umskz TYPE bseg-umskz,
             bschl TYPE bseg-bschl,
             qbshb TYPE bseg-qbshb,
             NEbtR TYPE BSEG-nebtr, "prb13052006
            END OF t_bseg.
    <-- 21.05.2005
      DATA: d_words(160),
            d_words1(80) TYPE c,
            d_words2(80) TYPE c.
      DATA: len TYPE i.
    <-- 21.05.2005
      DATA wa_payrq TYPE payrq. " 15.07.2005 Rakesh
    Read incoming data
      CLEAR: d_belnr,d_bukrs,d_gjahr.
    Company code
      CLEAR tab_in-value.
      READ TABLE tab_in WITH KEY 'REGUP-BUKRS'.
      d_bukrs = tab_in-value.
    Document number
      CLEAR tab_in-value.
      READ TABLE tab_in WITH KEY 'REGUP-BELNR'.
      d_belnr(10) = tab_in-value(10).
    Posting Key
      CLEAR tab_in-value.
      READ TABLE tab_in WITH KEY 'REGUP-BSCHL'.
      d_bschl = tab_in-value.
    Special G/L Indicator
      CLEAR tab_in-value.
      READ TABLE tab_in WITH KEY 'REGUP-UMSKZ'.
      d_umskz = tab_in-value.
    Fiscal Year
      CLEAR tab_in-value.
      READ TABLE tab_in WITH KEY 'REGUD-GJAHR'.  " Note
      CHECK sy-subrc = 0.
      d_gjahr = tab_in-value.
    Check if Down payment
      IF d_bschl = '29'.
        IF d_umskz EQ 'A'
        OR d_umskz EQ 'B'
        OR d_umskz EQ 'I'
        OR d_umskz EQ 'M'
        OR d_umskz EQ 'D'.
    Put Document Number of the Payment Document value to 'd_belnr'
          CLEAR tab_in-value.
          READ TABLE tab_in WITH KEY 'REGUP-VBLNR'.
          d_belnr = tab_in-value.
        ENDIF.
      ENDIF.
    Check if Down payment request
      IF d_bschl = '39'.
    Put Document Number of the Payment Document value to 'd_belnr'
        CLEAR tab_in-value.
        READ TABLE tab_in WITH KEY 'REGUP-VBLNR'.
        d_belnr = tab_in-value.
      ENDIF.
    <--15.07.2005 Rakesh
    Check if Payment request exists in 'payrq'
      CLEAR wa_payrq.
    Convert 'd_belnr' to 10 digits,if required
      CLEAR: l_intern,e_intern.
      CALL 'CONVERSION_EXIT_ALPHA_INPUT' ID 'INPUT'  FIELD d_belnr
                                         ID 'OUTPUT' FIELD l_intern.
      e_intern = l_intern+10.
      CLEAR d_belnr.
      d_belnr  = e_intern.
      SELECT SINGLE keyno augbl FROM payrq
       INTO CORRESPONDING FIELDS OF wa_payrq
        WHERE keyno = d_belnr.
      IF sy-subrc = 0. " Yes
    Take clearing document which is the required accounting document
        d_belnr = wa_payrq-augbl.
    Convert 'd_belnr' to 10 digits,if required
        CLEAR: l_intern,e_intern.
        CALL 'CONVERSION_EXIT_ALPHA_INPUT' ID 'INPUT'  FIELD d_belnr
                                           ID 'OUTPUT' FIELD l_intern.
        e_intern = l_intern+10.
        CLEAR d_belnr.
        d_belnr  = e_intern.
    Get BSEG data
        SELECT bukrs belnr gjahr buzei augbl rebzg rebzj sgtxt dmbtr wrbtr
        qbshb nebtr
         FROM bseg INTO CORRESPONDING FIELDS OF TABLE t_bseg
          WHERE bukrs EQ d_bukrs
            AND belnr EQ d_belnr
            AND gjahr EQ d_gjahr
         AND buzei EQ d_buzei
         AND bschl EQ '25'
              AND koart EQ 'S'           " G/L accounts
              AND shkzg EQ 'S'.          " Debit
      ELSE. " No
    For all other cases, we use this final logic, even though we have
    taken ‘d_belnr’ value from accounting doc no., in some earlier cases.
    Put document number of the payment document(vblnr) value to 'd_belnr',
        CLEAR tab_in-value.
        READ TABLE tab_in WITH KEY 'REGUP-VBLNR'.
        d_belnr = tab_in-value.
    -->15.07.2005
    Convert 'd_belnr' to 10 digits.
        CLEAR: l_intern,e_intern.
        CALL 'CONVERSION_EXIT_ALPHA_INPUT' ID 'INPUT'  FIELD d_belnr
                                           ID 'OUTPUT' FIELD l_intern.
        e_intern = l_intern+10.
        CLEAR d_belnr.
        d_belnr  = e_intern.
    *BSEG - PARTIAL PAYMT/FULL PAYMT WT DISC - Get Total Amount without Bank
    *charges
        SELECT bukrs belnr gjahr buzei augbl rebzg rebzj sgtxt dmbtr wrbtr
        qbshb bschl NEBTR
         FROM bseg INTO CORRESPONDING FIELDS OF TABLE t_bseg
          WHERE bukrs EQ d_bukrs
            AND belnr EQ d_belnr
            AND gjahr EQ d_gjahr
         AND buzei EQ d_buzei
         AND bschl EQ '25'
            AND ( koart EQ 'K' OR koart EQ 'D' ).
    <--15.07.2005 Rakesh
      ENDIF.
    -->20.10.2005 By Yogesh.
      CLEAR d_dmbtr.
    IF sy-subrc = 0.
      LOOP AT t_bseg.
       d_dmbtr = d_dmbtr + t_bseg-dmbtr - t_bseg-qbshb.
        SELECT SINGLE shkzg INTO (shkzg) FROM tbsl
        WHERE bschl = t_bseg-bschl.
    *BREAK-POINT.
    *******prb13052
       IF shkzg = 'S'.
         d_dmbtr = d_dmbtr + t_bseg-wrbtr - t_bseg-qbshb.
       ELSEIF shkzg = 'H'.
         d_dmbtr = d_dmbtr - t_bseg-wrbtr - t_bseg-qbshb.
       ENDIF.
       IF shkzg = 'S'.
         d_dmbtr = d_dmbtr + t_bseg-nebtr." - t_bseg-qbshb.
       ELSEIF shkzg = 'H'.
         if t_bseg-nebtr <> ''.
         d_dmbtr = d_dmbtr - t_bseg-nebtr." - t_bseg-qbshb.
         else.
         d_dmbtr = d_dmbtr - t_bseg-wrbtr." - t_bseg-qbshb.
         endif.
       ENDIF.
    *tables: reguh, payr.
      select single * from reguh where zbukr = d_bukrs
                           and vblnr = d_belnr
                           and LAUFD+6(4) = d_gjahr.
    *select single * from payr where zbukr = d_bukrs
    *and vblnr = d_belnr
    *and hbkid = reguh-hbkid
    *and hktid = reguh-hktid.
    *d_dmbtr = payr-rwbtr.
    *if d_dmbtr < 0.
    *d_dmbtr = d_dmbtr * ( -1 ).
    *endif.
    CLEAR tab_in-value.
        READ TABLE tab_in WITH KEY 'REGUD-SWNES'.
        d_DMBTR  =  tab_in-value.
    translate d_dmbtr using '* '.
    translate d_dmbtr using ', '.
    condense d_dmbtr no-gaps.
    d_dmbtr1 = d_dmbtr.
    *******prb13052
      ENDLOOP.
      PERFORM z_conv_amt USING d_dmbtr CHANGING d_amt.
    *Pass the Net Amount output to the outgoing table tab_out-name =
    *'NETAMT_TOT'.
      CLEAR: tab_out-value,tab_out-name.
      READ TABLE tab_out WITH KEY 'NETAMT_TOT'.
      tab_out-value = d_amt.
      " For inserting '*' ahead of value
    SHIFT tab_out-value LEFT DELETING LEADING space.  """"????IMP
      MODIFY tab_out INDEX sy-tabix.
    Get the Amount in words for Indian currency
    Can also use the FM 'HR_IN_CHG_INR_WRDS'
      CALL FUNCTION 'Z_SPELL_AMOUNT_INR'
        EXPORTING
          amount     = d_dmbtr1
        IMPORTING
          rupees_str = d_words.
    Shift left since above Function Module introduces an initial space
      SHIFT d_words LEFT DELETING LEADING space.
    <-- 21.05.2005
    *CALL FUNCTION 'C147_STRING_SPLIT_AT_POSITION'
    EXPORTING
       i_string         = d_words
       i_position       = 80
    IMPORTING
       E_HEAD           = d_words1
       E_TAIL           = d_words2.
      len = STRLEN( d_words ).
    Only 80 characters can be passed to one SAPScript symbol
    Split 'd_words' into 'd_words1' and 'd_words2'
    since 'DEFINE' in SAPScript displays maximum 80 charact

  • How to add join conditions in ABAP Query.

    Hi,
    I need a help on "ABAP Query".
    How to add join conditions in ABAP Query.
    Thanks in advance.
    Thanks & Regards,
    Ramana

    Hi,
    See below code,
    *& Report  ZRNP_ALV_SO
    REPORT  zrnp_alv_so MESSAGE-ID z7rnp .
    INCLUDE zrnp_include .
    *SELECTION SCREEN                                                      *
    SELECTION-SCREEN BEGIN OF BLOCK blk1 WITH FRAME TITLE text-004 .
    SELECT-OPTIONS: s_vbeln FOR vbak-vbeln ,
                    s_auart FOR v_auart ,
                    s_vkorg FOR v_vkorg ,
                    s_spart FOR v_spart ,
                    s_kunnr FOR v_kunnr ,
                    s_matnr FOR v_matnr .
    SELECTION-SCREEN END OF BLOCK blk1 .
    *AT SELECTION SCREEN                                                   *
    AT SELECTION-SCREEN.
      SELECT SINGLE vbeln
                     FROM vbak INTO vbak-vbeln
                     WHERE vbeln IN s_vbeln.
      IF sy-subrc <> 0.
        MESSAGE e202.
      ENDIF.
    *START OF SELECTION                                                    *
    START-OF-SELECTION .
      PERFORM data_select.
      PERFORM t_sort USING i_sort .
      PERFORM event_cat USING i_event .
      PERFORM fld_cat USING i_fldcat[] .
      PERFORM t_layout USING i_layout .
      PERFORM fld_cat2 USING i_fldcat2[] .
      PERFORM call_alv.
    * DATA SELECT                                                          *
    *&      Form  DATA_SELECT
    *       text
    *  -->  p1        text
    *  <--  p2        text
    FORM data_select .
      REFRESH: it_vbfa, it_so1, it_del_ful ,it_del1 .
      BREAK-POINT.
      *SELECT*
            *a~vbeln*
            *a~auart*
            *a~vkorg*
            *a~spart*
            *a~kunnr*
            *b~posnr*
            *b~matnr*
            *c~maktx*
            *b~kwmeng*
            *b~vrkme*
            *INTO TABLE it_so1 FROM vbak AS a*
                  *INNER JOIN vbap AS b ON b~vbeln = a~vbeln*
                  *INNER JOIN makt AS c ON c~matnr = b~matnr*
                  *AND c~spras = sy-langu*
                  *WHERE a~vbeln IN s_vbeln .*
      IF sy-subrc = 0.
        SORT it_so1 BY vbeln.
        DELETE ADJACENT DUPLICATES FROM it_so1.
      ENDIF.
    * COLURING DISPLAY                                                     *
      DATA: ld_color(1) TYPE  c .
    *  LOOP AT it_so1 INTO wa_so.
    * Populate color variable with colour properties
    * Char 1 = C (This is a color property)
    * Char 2 = 3 (Color codes: 1 - 7)
    * Char 3 = Intensified on/off ( 1 or 0 )
    * Char 4 = Inverse display on/off ( 1 or 0 )
    *           i.e. wa_ekko-line_color = 'C410'
    *    ld_color = ld_color + 1.
    * Only 7 colours so need to reset color value
    *    IF ld_color = 8.
    *      ld_color = 1.
    *    ENDIF.
    *    CONCATENATE 'C' ld_color '10' INTO wa_so-line_color.
    **  wa_ekko-line_color = 'C410'.
    *    MODIFY it_so1 FROM wa_so.
    *  ENDLOOP .
    *  IF sy-subrc = 0.
      IF NOT it_so1[] IS INITIAL.
        SELECT vbelv
               posnv
               vbeln
               posnn
               vbtyp_n
               INTO TABLE it_vbfa
               FROM vbfa
               FOR ALL ENTRIES IN it_so1
               WHERE vbelv = it_so1-vbeln
               AND   posnn = it_so1-posnr
               AND vbtyp_n ='J' .
        IF sy-subrc = 0.
          SELECT vbeln
                 posnr
                 matnr
                 werks
                 lgort
                 charg
                 lfimg
                 vrkme
                 FROM lips INTO TABLE it_del_ful
                 FOR ALL ENTRIES IN it_vbfa
                 WHERE vbeln = it_vbfa-vbeln
                 AND   posnr = it_vbfa-posnn.
        ENDIF.
      ENDIF.
    ENDFORM.                    " DATA_SELECT
    **************** EVENT CATALOG ****************************************
    *&      Form  EVENT_CAT
    *       text
    *      -->P_I_EVENT  text
    FORM event_cat  USING    p_i_event TYPE slis_t_event .
      REFRESH p_i_event .
      CALL FUNCTION 'REUSE_ALV_EVENTS_GET'
    * EXPORTING
    *   I_LIST_TYPE           = 0
       IMPORTING
          et_events             = p_i_event
    * EXCEPTIONS
    *   LIST_TYPE_WRONG       = 1
    *   OTHERS                = 2
    *  IF sy-subrc <> 0.
    ** MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
    **         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
    *  ENDIF.
      READ TABLE p_i_event WITH KEY name = slis_ev_top_of_page INTO t_event.
      IF sy-subrc = 0.
        MOVE 'TOP_OF_PAGE' TO t_event-form.
        MODIFY p_i_event FROM t_event INDEX sy-tabix TRANSPORTING form.
      ENDIF.
      CLEAR t_event .
    ENDFORM.                    " EVENT_CAT
    **********FORM FOR EVENT TOP_OF_PAGE**********************************
    FORM top_of_page .
      REFRESH i_listheader.
      DATA: t_header TYPE slis_listheader.
      DATA: v_text(50).
      WRITE sy-datum TO v_text.
      CLEAR t_header.
      t_header-typ = 'S'.
      t_header-key = "Date".
      t_header-info = v_text.
      APPEND t_header TO i_listheader.
      CLEAR t_header.
      CLEAR v_text.
      WRITE:  'SALES ORDER & DELIVERY DETAILS REPORT  ' TO v_text .
      t_header-typ = 'S'.
      t_header-key = 'TITLE'.
      t_header-info = v_text.
      APPEND t_header TO i_listheader.
      CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
        EXPORTING
          it_list_commentary = i_listheader.
    *      I_LOGO             = 'ALV_BACKGROUND'.
    *   I_END_OF_LIST_GRID       =
    ENDFORM.                    "TOP_OF_PAGE
    ********  FIRST ALV GRID DISPLAY ***************************************
    FORM call_alv .
    * FORM TO MAKE THE CELL CONTENTS INVISIBLE.
    * PERFORM INVISIBLE_CELL_CONTENTS. *
    *EXCLUDE-DECLARATION.
      CLEAR wa_exclude.
      wa_exclude-fcode = '&VEXCEL'.
      APPEND wa_exclude TO i_exclude.
    *&      Form  CALL_ALV
    *       text
    *  -->  p1        text
    *  <--  p2        text
      CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
       EXPORTING
         i_callback_program                = sy-repid
    *    I_CALLBACK_PF_STATUS_SET          = 'SET_PF_STATUS'
         i_callback_user_command           = 'USER_COMMAND1'
         i_callback_top_of_page            = 'TOP_OF_PAGE'
         i_background_id                   = 'ALV_BACKGROUND'
         is_layout                         = i_layout
         it_fieldcat                       = i_fldcat[]
         it_excluding                      = i_exclude
         it_sort                           = i_sort
         it_events                         = i_event
        TABLES
         t_outtab                          = it_so1
    *  IF sy-subrc <> 0.
    ** MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
    **         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
    *  ENDIF.
    ENDFORM.                    " CALL_ALV
    ************** FIRST FIELDCATALOG *************************************
    *&      Form  FLD_CAT
    *       text
    *      -->P_I_FLDCAT[]  text
    FORM fld_cat  USING    p_i_fldcat TYPE slis_t_fieldcat_alv.
      CLEAR i_fldcat.
      i_fldcat-fieldname   = 'VBELN'. "FIELD FOR WHICH CATALOG ID FILLED
      i_fldcat-tabname     = 'IT_SO1'."TABLE NAME
      i_fldcat-seltext_m   = 'SALES ORDER NO.'.
      i_fldcat-col_pos     = 1.        " POSITION OF THE COLUMN.
      i_fldcat-outputlen   = 20.       " SET THE OUTPUT LENGTH.
      i_fldcat-emphasize   = 'X'.      " COLOR OF THIS COLUMN.
      i_fldcat-key         = 'X'.      " SO THAT THIS FIELD IS NOT
      "SCROLLABLE AND HIDDABLE.
      i_fldcat-hotspot     = 'X'.
      i_fldcat-just(1)     = 'C'.
      APPEND i_fldcat.
      CLEAR i_fldcat.
      i_fldcat-fieldname   = 'AUART'. "FIELD FOR WHICH CATALOG ID FILLED
      i_fldcat-tabname     = 'IT_SO1'."TABLE NAME
      i_fldcat-seltext_m   = 'SALES DOC. TYPE'.
      i_fldcat-col_pos     = 2.        " POSITION OF THE COLUMN.
      i_fldcat-outputlen   = 15.       " SET THE OUTPUT LENGTH.
      i_fldcat-emphasize   = 'X'.      " COLOR OF THIS COLUMN.
      i_fldcat-key         = 'X'.      " SO THAT THIS FIELD IS NOT
      "SCROLLABLE AND HIDDABLE.
      i_fldcat-just(1)     = 'C'.
      APPEND i_fldcat.
      CLEAR i_fldcat.
      i_fldcat-fieldname   = 'VKORG'. "FIELD FOR WHICH CATALOG ID FILLED
      i_fldcat-tabname     = 'IT_SO1'.
      i_fldcat-seltext_m   = 'SALES ORG.'.
      i_fldcat-col_pos     = 3.        " POSITION OF THE COLUMN.
      i_fldcat-outputlen   = 12.       " SET THE OUTPUT LENGTH.
      i_fldcat-emphasize   = 'X'.      " COLOR OF THIS COLUMN.
      i_fldcat-key         = 'X'.      " SO THAT THIS FIELD IS NOT
      "SCROLLABLE AND HIDDABLE.
      i_fldcat-just(1)     = 'C'.
      APPEND i_fldcat.
      CLEAR i_fldcat.
      i_fldcat-fieldname   = 'SPART'. "FIELD FOR WHICH CATALOG ID FILLED
      i_fldcat-tabname     = 'IT_SO1'.
      i_fldcat-seltext_m   = 'DIVISION'.
      i_fldcat-col_pos     = 4.        " POSITION OF THE COLUMN.
      i_fldcat-outputlen   = 10.       " SET THE OUTPUT LENGTH.
      i_fldcat-emphasize   = 'X'.      " COLOR OF THIS COLUMN.
      i_fldcat-key         = 'X'.      " SO THAT THIS FIELD IS NOT
      "SCROLLABLE AND HIDDABLE.
      i_fldcat-just(1)     = 'C'.
      APPEND i_fldcat.
      CLEAR i_fldcat.
      i_fldcat-fieldname   = 'KUNNR'. "FIELD FOR WHICH CATALOG ID FILLED
      i_fldcat-tabname     = 'IT_SO1'.
      i_fldcat-seltext_m   = 'SOLD TO PARTY'.
      i_fldcat-col_pos     = 5.        " POSITION OF THE COLUMN.
      i_fldcat-outputlen   = 15.       " SET THE OUTPUT LENGTH.
      i_fldcat-emphasize   = 'X'.      " COLOR OF THIS COLUMN.
      i_fldcat-key         = 'X'.      " SO THAT THIS FIELD IS NOT
      "SCROLLABLE AND HIDDABLE.
      i_fldcat-just(1)     = 'C'.
      APPEND i_fldcat.
      CLEAR i_fldcat.
      i_fldcat-fieldname   = 'POSNR'. "FIELD FOR WHICH CATALOG ID FILLED
      i_fldcat-tabname     = 'IT_SO1'.
      i_fldcat-seltext_m   = 'SALES DOC. ITEM'.
      i_fldcat-col_pos     = 6.        " POSITION OF THE COLUMN.
      i_fldcat-outputlen   = 17.       " SET THE OUTPUT LENGTH.
      i_fldcat-emphasize   = 'X'.      " COLOR OF THIS COLUMN.
      i_fldcat-key         = 'X'.      " SO THAT THIS FIELD IS NOT
      "SCROLLABLE AND HIDDABLE.
      i_fldcat-just(1)     = 'C'.
      APPEND i_fldcat.
      CLEAR i_fldcat.
      i_fldcat-fieldname   = 'MATNR'. "FIELD FOR WHICH CATALOG ID FILLED
      i_fldcat-tabname     = 'IT_SO1'.
      i_fldcat-seltext_m   = 'MATERIAL NO.'.
      i_fldcat-col_pos     = 7.        " POSITION OF THE COLUMN.
      i_fldcat-outputlen   = 20.       " SET THE OUTPUT LENGTH.
      i_fldcat-emphasize   = 'X'.      " COLOR OF THIS COLUMN.
      i_fldcat-key         = 'X'.      " SO THAT THIS FIELD IS NOT
      "SCROLLABLE AND HIDDABLE.
      i_fldcat-just(1)     = 'C'.
      APPEND i_fldcat.
      CLEAR i_fldcat.
      i_fldcat-fieldname   = 'MAKTX'. "FIELD FOR WHICH CATALOG ID FILLED
      i_fldcat-tabname     = 'IT_SO1'.
      i_fldcat-seltext_m   = 'DESCRIPTION'.
      i_fldcat-col_pos     = 8.        " POSITION OF THE COLUMN.
      i_fldcat-outputlen   = 20.       " SET THE OUTPUT LENGTH.
      i_fldcat-emphasize   = 'X'.      " COLOR OF THIS COLUMN.
      i_fldcat-key         = 'X'.      " SO THAT THIS FIELD IS NOT
      "SCROLLABLE AND HIDDABLE.
      i_fldcat-just(1)     = 'C'.
      APPEND i_fldcat.
      CLEAR i_fldcat.
      i_fldcat-fieldname   = 'KWMENG'. "FIELD FOR WHICH CATALOG ID FILLED
      i_fldcat-tabname     = 'IT_SO1'.
      i_fldcat-seltext_m   = 'QUANTITY'.
      i_fldcat-col_pos     = 9.        " POSITION OF THE COLUMN.
      i_fldcat-outputlen   = 15.       " SET THE OUTPUT LENGTH.
      i_fldcat-emphasize   = 'X'.      " COLOR OF THIS COLUMN.
      i_fldcat-key         = 'X'.      " SO THAT THIS FIELD IS NOT
      "SCROLLABLE AND HIDDABLE.
      i_fldcat-do_sum    = 'X'.        " For doing "SUM"
      i_fldcat-just(1)     = 'C'.
      APPEND i_fldcat.
      CLEAR i_fldcat.
      i_fldcat-fieldname   = 'VRKME'. "FIELD FOR WHICH CATALOG ID FILLED
      i_fldcat-tabname     = 'IT_SO1'.
      i_fldcat-seltext_m   = 'SALES UNIT'.
      i_fldcat-col_pos     = 10.       " POSITION OF THE COLUMN.
      i_fldcat-outputlen   = 10.       " SET THE OUTPUT LENGTH.
      i_fldcat-emphasize   = 'X'.      " COLOR OF THIS COLUMN.
      i_fldcat-key         = 'X'.      " SO THAT THIS FIELD IS NOT
      "SCROLLABLE AND HIDDABLE.
      i_fldcat-just(1)     = 'C'.
      APPEND i_fldcat.
    ENDFORM.                    " FLD_CAT
    ****************** ALV SORTING  ***************************************
    *&      Form  SORT
    *       text
    *      -->P_I_SORT  text
    FORM t_sort  USING    p_i_sort TYPE slis_t_sortinfo_alv .
      DATA: i_sort TYPE slis_sortinfo_alv .
      REFRESH p_i_sort .
      CLEAR i_sort.
      i_sort-spos = 1.
      i_sort-tabname = 'IT_SO1'.
      i_sort-fieldname = 'VBELN'.
      i_sort-up = 'X'.
      i_sort-subtot = 'X'.
      i_sort-group = '*'.
      APPEND i_sort TO p_i_sort.
    ENDFORM.                    " SORT
    *FORM SET_PF_STATUS USING rt_extab TYPE slis_t_extab.
    *  SET PF-STATUS 'ZSTANDARD'.
    *ENDFORM. "Set_pf_status
    ***********FORM FOR EVENT USER_COMMAND1********************************
    FORM user_command1 USING r_ucomm LIKE sy-ucomm
                             rs_selfield TYPE slis_selfield.
    *CASE R_UCOMM .
    *    WHEN '&IC1' .
    *    IF rs_selfield-FIELDNAME = 'VBELN' .
    *    ENDIF .
    * WHEN OTHERS .
    * ENDCASE .
      CLEAR wa_so.
      REFRESH: it_del1 .
      IF r_ucomm = '&IC1' AND rs_selfield-fieldname = 'VBELN' AND
      rs_selfield-value IS NOT INITIAL.
        READ TABLE it_so1 INTO wa_so INDEX rs_selfield-tabindex.
        IF sy-subrc = 0.
          LOOP AT it_vbfa INTO wa_vbfa WHERE vbelv = wa_so-vbeln
                                       AND   posnv = wa_so-posnr.
            READ TABLE it_del_ful INTO wa_it_del_ful
                 WITH KEY vbeln = wa_vbfa-vbelv
                          posnr = wa_vbfa-posnn.
            IF sy-subrc = 0.
              CLEAR wa_del.
              MOVE wa_it_del_ful TO wa_del.
              APPEND wa_del TO it_del1.
            ENDIF.
          ENDLOOP.
        ENDIF.
      ENDIF.
    ********* SECOND ALV GRID DISPLAY ***********************************
      CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
       EXPORTING
        i_callback_program                = sy-repid
    *   I_CALLBACK_PF_STATUS_SET          = 'SET_PF_STATUS'
        i_callback_user_command           = 'USER_COMMAND2'
        i_callback_top_of_page            = 'TOP_OF_PAGE'
        i_background_id                   = 'ALV_BACKGROUND'
        it_fieldcat                       = i_fldcat2[]
        it_sort                           = i_sort
        TABLES
        t_outtab                          = it_del_ful
    *  IF sy-subrc <> 0.
    ** MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
    **         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
    *  ENDIF.
    ENDFORM .                    "USER_COMMAND1
    ********** FORM FOR EVENT USER_COMMAND 2 ******************************
    FORM user_command2 USING r_ucomm LIKE sy-ucomm
                             rs_selfield TYPE slis_selfield.
      CLEAR wa_so.
      REFRESH: it_del1 .
      IF r_ucomm = '&IC1' AND rs_selfield-fieldname = 'VBELN' AND
      rs_selfield-value IS NOT INITIAL.
        READ TABLE it_so1 INTO wa_so INDEX rs_selfield-tabindex.
    *    IF SY-SUBRC = 0.
    *       LOOP AT it_vbfa INTO wa_vbfa WHERE vbelv = WA_SO-vbeln
    *                                    AND   posnv = WA_SO-posnr.
        READ TABLE it_del_ful INTO wa_it_del_ful
             WITH KEY vbeln = rs_selfield-value
                     " vbeln = wa_vbfa-vbeln
                      posnr = wa_vbfa-posnn.
        IF rs_selfield-fieldname = 'VBELN'.
          SET PARAMETER ID 'VL' FIELD wa_vbfa-vbeln .
          CALL TRANSACTION 'VL03' AND SKIP FIRST SCREEN.
        ENDIF .
    *       ENDLOOP.
    *    ENDIF.
      ENDIF.
    ENDFORM .                    "USER_COMMAND2
    ********* SECOND FIELDCATALOG ******************************************
    *&      Form  FLD_CAT2
    *       text
    *      -->P_I_FLDCAT2[]  text
    FORM fld_cat2  USING    p_i_fldcat2 TYPE slis_t_fieldcat_alv .
      CLEAR i_fldcat2.
      i_fldcat2-fieldname   = 'VBELN'. "FIELD FOR WHICH CATALOG ID FILLED
      i_fldcat2-tabname     = 'IT_DEL_FUL'."TABLE NAME
      i_fldcat2-seltext_m   = 'DELIVERY NO.'.
      i_fldcat2-col_pos     = 1.        " POSITION OF THE COLUMN.
      i_fldcat2-outputlen   = 20.       " SET THE OUTPUT LENGTH.
      i_fldcat2-emphasize   = 'X'.      " COLOR OF THIS COLUMN.
      i_fldcat2-key         = 'X'.      " SO THAT THIS FIELD IS NOT
      "SCROLLABLE AND HIDDABLE.
      i_fldcat2-hotspot     = 'X'.
      i_fldcat2-just(1)     = 'C'.
      APPEND i_fldcat2.
      CLEAR i_fldcat2.
      i_fldcat2-fieldname   = 'POSNR'. "FIELD FOR WHICH CATALOG ID FILLED
      i_fldcat2-seltext_m   = 'DELIVERY ITEM'.
      i_fldcat2-col_pos     = 2.        " POSITION OF THE COLUMN.
      i_fldcat2-outputlen   = 20.       " SET THE OUTPUT LENGTH.
      i_fldcat2-emphasize   = 'X'.      " COLOR OF THIS COLUMN.
      i_fldcat2-key         = 'X'.      " SO THAT THIS FIELD IS NOT
      "SCROLLABLE AND HIDDABLE.
      i_fldcat2-just(1)     = 'C'.
      APPEND i_fldcat2.
      CLEAR i_fldcat2.
      i_fldcat2-fieldname   = 'MATNR'. "FIELD FOR WHICH CATALOG ID FILLED
      i_fldcat2-seltext_m   = 'MATERIAL NO.'.
      i_fldcat2-col_pos     = 3.        " POSITION OF THE COLUMN.
      i_fldcat2-outputlen   = 20.       " SET THE OUTPUT LENGTH.
      i_fldcat2-emphasize   = 'X'.      " COLOR OF THIS COLUMN.
      i_fldcat2-key         = 'X'.      " SO THAT THIS FIELD IS NOT
      "SCROLLABLE AND HIDDABLE.
      i_fldcat2-just(1)     = 'C'.
      APPEND i_fldcat2.
      CLEAR i_fldcat2.
      i_fldcat2-fieldname   = 'WERKS'. "FIELD FOR WHICH CATALOG ID FILLED
      i_fldcat2-seltext_m   = 'PLANT.'.
      i_fldcat2-col_pos     = 4.        " POSITION OF THE COLUMN.
      i_fldcat2-outputlen   = 20.       " SET THE OUTPUT LENGTH.
      i_fldcat2-emphasize   = 'X'.      " COLOR OF THIS COLUMN.
      i_fldcat2-key         = 'X'.      " SO THAT THIS FIELD IS NOT
      "SCROLLABLE AND HIDDABLE.
      i_fldcat2-just(1)     = 'C'.
      APPEND i_fldcat2.
      CLEAR i_fldcat2.
      i_fldcat2-fieldname   = 'LGORT'. "FIELD FOR WHICH CATALOG ID FILLED
      i_fldcat2-seltext_m   = 'ST. LOCATION'.
      i_fldcat2-col_pos     = 5.        " POSITION OF THE COLUMN.
      i_fldcat2-outputlen   = 20.       " SET THE OUTPUT LENGTH.
      i_fldcat2-emphasize   = 'X'.      " COLOR OF THIS COLUMN.
      i_fldcat2-key         = 'X'.      " SO THAT THIS FIELD IS NOT
      "SCROLLABLE AND HIDDABLE.
      i_fldcat2-just(1)     = 'C'.
      APPEND i_fldcat2.
      CLEAR i_fldcat2.
      i_fldcat2-fieldname   = 'CHARG'. "FIELD FOR WHICH CATALOG ID FILLED
      i_fldcat2-seltext_m   = 'BATCH NO.'.
      i_fldcat2-col_pos     = 6.        " POSITION OF THE COLUMN.
      i_fldcat2-outputlen   = 20.       " SET THE OUTPUT LENGTH.
      i_fldcat2-emphasize   = 'X'.      " COLOR OF THIS COLUMN.
      i_fldcat2-key         = 'X'.      " SO THAT THIS FIELD IS NOT
      "SCROLLABLE AND HIDDABLE.
      i_fldcat2-just(1)     = 'C'.
      APPEND i_fldcat2.
      CLEAR i_fldcat2.
      i_fldcat2-fieldname   = 'LFIMG'. "FIELD FOR WHICH CATALOG ID FILLED
      i_fldcat2-seltext_m   = 'ACT. DEL. QTY.'.
      i_fldcat2-col_pos     = 7.        " POSITION OF THE COLUMN.
      i_fldcat2-outputlen   = 20.       " SET THE OUTPUT LENGTH.
      i_fldcat2-emphasize   = 'X'.      " COLOR OF THIS COLUMN.
      i_fldcat2-key         = 'X'.      " SO THAT THIS FIELD IS NOT
      "SCROLLABLE AND HIDDABLE.
      i_fldcat2-just(1)     = 'C'.
      APPEND i_fldcat2.
      CLEAR i_fldcat2.
      i_fldcat2-fieldname   = 'VRKME'. "FIELD FOR WHICH CATALOG ID FILLED
      i_fldcat2-seltext_m   = 'SALES UNIT.'.
      i_fldcat2-col_pos     = 8.        " POSITION OF THE COLUMN.
      i_fldcat2-outputlen   = 20.       " SET THE OUTPUT LENGTH.
      i_fldcat2-emphasize   = 'X'.      " COLOR OF THIS COLUMN.
      i_fldcat2-key         = 'X'.      " SO THAT THIS FIELD IS NOT
      "SCROLLABLE AND HIDDABLE.
      i_fldcat2-just(1)     = 'C'.
      APPEND i_fldcat2.
    ENDFORM.                                                    " FLD_CAT2
    ***************** ALV LAYOUT *******************************************
    *&      Form  LAYOUT
    *       text
    *      -->P_I_LAYOUT  text
    FORM t_layout  USING    p_i_layout TYPE slis_layout_alv .
      p_i_layout-zebra  = 'X'.
      p_i_layout-totals_text = 'GRAND TOTAL ='.
    *  p_i_layout-CONFIRMATION_PROMPT = 'X'.
    *  p_i_layout-DEF_STATUS  = ' '.
      p_i_layout-info_fieldname = 'LINE_COLOR'.
    ENDFORM.                    " LAYOUT

  • SQL Query results to CSV as two worksheets

    I'm trying to take two SQL queries and get the results sent to a CSV file on two worksheets.  Looking online I have not found a solid example of using the Excel ComObject to create a CSV then add a new worksheet to the CSV file.  An added bonus
    would be using AutoFit on the columns so everything is easily visible.
    Code found online got me the following script which does work, however it takes 12 minutes to pipe the SQL queries to Excel.  Switching to a CSV and the script executes in 5 seconds.
    This is another nice to have, I was also looking the best way to look at the results (only 1 column) and depending on the length of the data, insert what Excel would call a Cell thereby shifting cells RIGHT but so far have found no clear examples of how
    to accomplish that.  My guess would be modifying my SQL queries but I've posted a question on StackOverFlow and someone suggested modifying the PowerShell Table created from the SQL dataset.Tables
    Code:
    $docs = "C:\Scripts\Output.xlsx"
    If (Test-Path $docs){Remove-Item $docs}
    Function Run-Query {
    param([string[]]$queries,[string[]]$sheetnames)
    ## - Create an Excel Application instance:
    $xlsObj = New-Object -ComObject Excel.Application
    $xlsObj.DisplayAlerts = $false
    $xlsWb = $xlsobj.Workbooks.Add(1)
    ## - Create new Workbook and Sheet (Visible = 1 / 0 not visible)
    $xlsObj.Visible = 0
    $time = 2
    for ($i = 0; $i -lt $queries.Count; $i++){
    $percentage = $i / $time
    $remaining = New-TimeSpan -Seconds ($time - $i)
    $message = "{0:p0} complete" -f $percentage, $remaining
    Write-Progress -Activity "Creating Daily Reboot Spreadsheet" -status $message -PercentComplete ($percentage * 100)
    $query = $queries[$i]
    $sheetname = $sheetnames[$i]
    $xlsSh = $xlsWb.Worksheets.Add([System.Reflection.Missing]::Value, $xlsWb.Worksheets.Item($xlsWb.Worksheets.Count))
    $xlsSh.Name = $sheetname
    ### SQL query results sent to Excel
    $SQLServer = 'ServerName'
    $Database = 'DataBase'
    ## - Connect to SQL Server using non-SMO class 'System.Data':
    $SqlConnection = New-Object System.Data.SqlClient.SqlConnection
    $SqlConnection.ConnectionString = "Server = $SQLServer; Database = $Database; Integrated Security = True"
    $SqlCmd = New-Object System.Data.SqlClient.SqlCommand
    $SqlCmd.CommandText = $query
    $SqlCmd.Connection = $SqlConnection
    ## - Extract and build the SQL data object '$Table2':
    $SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
    $SqlAdapter.SelectCommand = $SqlCmd
    $DataSet = New-Object System.Data.DataSet
    $SqlAdapter.Fill($DataSet)
    $SqlConnection.Close()
    $Table1 = $DataSet.Tables["Table"]
    ## - Build the Excel column heading:
    [Array] $getColumnNames = $Table1.Columns | SELECT ColumnName
    ## - Build column header:
    [Int] $RowHeader = 1
    foreach ($ColH in $getColumnNames)
    $xlsSh.Cells.item(1, $RowHeader).font.bold = $true
    $xlsSh.Cells.item(1, $RowHeader) = $ColH.ColumnName
    $RowHeader++
    ## - Adding the data start in row 2 column 1:
    [Int] $rowData = 2
    [Int] $colData = 1
    foreach ($rec in $Table1.Rows)
    foreach ($Coln in $getColumnNames)
    ## - Next line convert cell to be text only:
    $xlsSh.Cells.NumberFormat = "@"
    ## - Populating columns:
    $xlsSh.Cells.Item($rowData, $colData) = $rec.$($Coln.ColumnName).ToString()
    $ColData++
    $rowData++; $ColData = 1
    ## - Adjusting columns in the Excel sheet:
    $xlsRng = $xlsSH.usedRange
    [void] $xlsRng.EntireColumn.AutoFit()
    }#End For loop.
    #Delete unwanted Sheet1.
    $xlsWb.Sheets.Item('Sheet1').Delete()
    #Set Monday to Active Sheet upon opening Workbook.
    $xlsWb.Sheets.Item('Cert').Activate()
    ## ---------- Saving file and Terminating Excel Application ---------- ##
    $xlsFile = "C:\Scripts\Output.xlsx"
    [void] $xlsObj.ActiveWorkbook.SaveAs($xlsFile)
    $xlsObj.Quit()
    ## - End of Script - ##
    start-sleep 2
    While ([System.Runtime.Interopservices.Marshal]::ReleaseComObject($xlsRng)) {'cleanup xlsRng'}
    While ([System.Runtime.Interopservices.Marshal]::ReleaseComObject($xlsSh)) {'cleanup xlsSh'}
    While ([System.Runtime.Interopservices.Marshal]::ReleaseComObject($xlsWb)) {'cleanup xlsWb'}
    While ([System.Runtime.Interopservices.Marshal]::ReleaseComObject($xlsObj)) {'cleanup xlsObj'}
    [gc]::collect() | Out-Null
    [gc]::WaitForPendingFinalizers() | Out-Null
    }#End Function
    $queries = @()
    $queries += "Query1"
    $queries += "Query2"
    $sheetnames = @('Cert','Prod')
    Run-Query -queries $queries -sheetnames $sheetnames

    Here's what I ended up with that accomplishes my goal.  I learned it's not possible to create a CSV with two worksheets since Excel will allow this but the CSV cannot be saved with the second worksheet.  Instead, I create two CSV files then merge
    them into one Excel workbook, one CSV per worksheet.  In my case, this happens in 5 seconds.  There is one thing which must be mentioned, I've seen this script fail the first time it is run but will successfully run the second time.
    Also note, after much trial and error, this code correctly cleans up the Excel ComObject!!  -Thanks go to JRV.
    $docs = "D:\Scripts\MonthlyReboots.xlsx"
    IF (Test-Path $docs){Remove-Item $docs}
    $csv1 = "D:\Scripts\Cert.csv"
    IF (Test-Path $csv1){Remove-Item $csv1}
    $csv2 = "D:\Scripts\Prod.csv"
    IF (Test-Path $csv2){Remove-Item $csv2}
    Function Run-Query {
    param([string[]]$queries,[string[]]$sheetnames,[string[]]$filenames)
    Begin{
    $SQLServer = 'ServerName'
    $Database = 'DataBase'
    $SqlConnection = New-Object System.Data.SqlClient.SqlConnection
    $SqlConnection.ConnectionString = "Server = $SQLServer; Database = $Database; Integrated Security = True"
    $Excel = New-Object -ComObject Excel.Application
    $Excel.Visible = 0
    $dest = $Excel.Workbooks.Add(1)
    }#End Begin
    Process{
    For($i = 0; $i -lt $queries.Count; $i++){
    $SqlCmd = New-Object System.Data.SqlClient.SqlCommand
    $SqlCmd.CommandText = $queries[$i]
    $SqlCmd.Connection = $SqlConnection
    $SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
    $SqlAdapter.SelectCommand = $SqlCmd
    $DataSet = New-Object System.Data.DataSet
    $SqlAdapter.Fill($DataSet)
    $DataSet.Tables[0] | Export-Csv -NoTypeInformation -Path "D:\Scripts\$($sheetnames[$i]).csv" -Force
    }#end for loop.
    }#End Process
    End{
    $SqlConnection.Close()
    #Excel magic test!
    For($i = 0; $i -lt $queries.Count; $i++){
    $loopy = (Resolve-Path -Path $filenames[$i]).ProviderPath
    $Book = $Excel.Workbooks.Open($loopy)
    $next = $Excel.workbooks.Open($loopy)
    $next.ActiveSheet.Move($dest.ActiveSheet)
    $xlsRng = $dest.ActiveSheet.UsedRange
    $xlsRng.EntireColumn.AutoFit() | Out-Null
    $dest.sheets.item('Sheet1').Delete()
    $xlsFile = "D:\Scripts\MonthlyReboots.xlsx"
    [void] $Excel.ActiveWorkbook.SaveAs($xlsFile)
    $Excel.Quit()
    While ([System.Runtime.Interopservices.Marshal]::ReleaseComObject($xlsRng)) {'cleanup xlsRng'}
    While ([System.Runtime.Interopservices.Marshal]::ReleaseComObject($next)) {'cleanup xlsSh'}
    While ([System.Runtime.Interopservices.Marshal]::ReleaseComObject($Book)) {'cleanup xlsWb'}
    While ([System.Runtime.Interopservices.Marshal]::ReleaseComObject($Excel)) {'cleanup xlsObj'}
    [gc]::collect() | Out-Null
    [gc]::WaitForPendingFinalizers() | Out-Null
    }#End end block.
    }#End function run-query.
    $queries = @()
    $queries += @'
    Select * from table1
    $queries += @'
    Select * from table2
    $sheetnames = @("Cert","Prod")
    $filenames = @("D:\Scripts\Prod.csv","D:\Scripts\Cert.csv")
    Run-Query -queries $queries -sheetnames $sheetnames -filenames $filenames
    Start-Sleep -Milliseconds 50
    Invoke-Item D:\Scripts\MonthlyReboots.xlsx

Maybe you are looking for