Dynamic where clause in "delete itab where..." variant

Hi,  I need to be able to delete the contents of an internal table based on a dynamic where clause. I don't want to loop through the entire table to test each record.  Since the delete statement doesn't seem to allow a dynamic "where" clause I am trying to use a macro.  I can't get the macro to work either. 
I get the following error in my syntax check:
Statement concluding with "...FINAL_STATEMENT" ended unexpectedly.          
Is there any way to get this macro to pass the syntax text or accomplish the dynamic delete?
MACRO TO FILTER OUT UNWANTED RECORDS (RETAIL OUTLETS)
  DEFINE DELOUT.
    DELETE MY_ITAB WHERE &1.
  END-OF-DEFINITION.
Example FINAL_STATEMENT = (  ZRRDIVIS = '4' )
USING MACRO DOES NOT PASS SYNTAX CHECK
  DELOUT FINAL_STATEMENT.

Well, if you really need this kind of functionality,  here is a code sample that I just threw together that would work.  A lot of code for one little delete statement though.
report zrich_0001 .
data: begin of itab occurs 0,
      fielda type c,
      fieldb type c,
      fieldc type c,
      end of itab.
data: where_clause type string.
class lcl_source definition.
  public section.
    types: t_source(72).
    class-data: routine(32) value 'TEMP_ROUTINE',
                program(8),
                message(128),
                line type i.
    class-data: isource type table of t_source,
                xsource type t_source.
    class-methods: execute_source importing im_clause type string.
endclass.
start-of-selection.
* Build the itab.
  itab-fielda = '1'.
  itab-fieldb = '2'.
  itab-fieldc = '3'.
  append itab.
  itab-fielda = '3'.
  itab-fieldb = '2'.
  itab-fieldc = '1'.
  append itab.
* Set the where clause
  where_clause = 'FIELDA = ''1''.'.
* call the delete statement.
  call method lcl_source=>execute_source
            exporting
                im_clause = where_clause.
*       CLASS lcl_source IMPLEMENTATION
class lcl_source implementation.
  method execute_source.
    xsource = 'REPORT ZTEMP_REPORT.'.
    insert xsource  into isource index 1.
    xsource = 'FORM & tables itab.'.
    replace '&' with routine into xsource.
    insert xsource  into isource index 2.
    xsource = 'Data: begin of xitab occurs 0,'.
    append xsource to isource.
    xsource = 'fielda type c,'.
    append xsource to isource.
    xsource = 'fieldb type c,'.
    append xsource to isource.
    xsource = 'fieldc type c,'.
    append xsource to isource.
    xsource = 'end of xitab.'.
    append xsource to isource.
    xsource = 'xitab[] = itab[].'.
    append xsource to isource.
    concatenate 'DELETE xITAB WHERE' im_clause into xsource
                  separated by space.
    append xsource to isource.
    xsource = 'ENDFORM.'.
    append xsource to isource.
    generate subroutine pool isource name program
                             message message
                             line line.
    if sy-subrc = 0.
      perform (routine) in program (program) tables itab.
    else.
      write:/ message.
    endif.
  endmethod.
endclass.
Regards,
Rich Heilman

Similar Messages

  • Dynamic Where clause - from third party system

    I have internal table A with fields I am getting from legacy system
    field1
    field2
    field3
    field4
    fieldn
    and i am getting a condition table (Where clause conditions) from a legacy system thru an RFC call.
    and in the RFC i need to delete records from internal table A using this where clause
    and where clause condition table(is 72 char length single field table) from legacy system looks like the following
             ( FIELD1                          BETWEEN '01' AND '05' ) AND
         FIELD1                         NE '06'
         AND ( FIELD4                     EQ 'BU' )
         AND ( FIELD5                      BETWEEN '001' AND '009' )
    How could i delete entries from internal table A using this where clause conditon table mentioned above

    Hi a®s,
    yes, thats what I thought of: Generate subroutine Pool.
    I did not elaborate on the fact that dynamic where clauses are allowed only for database selections. That means that FUNCTION 'FREE_SELECTIONS_WHERE_2_RANGE' can't be used either.
    But if you generate a subroutine, you can generate TYPE and DATA declarations as well. This gives you the ability to have static where condition for the DELETE itab WHERE statement.
    If the type vaies during runtime, the generation may vary too.
    I think you are able to do the coding for this.
    On the other hand, Thomas' 'slick' proposal using a temporary database table and a subsequent SELECT ... WHERE (<dynamic where clause) may be easier to implement.
    BTW: Are ther release notes on ABAP720?
    Regards,
    Clemens

  • How to use a lexical variable in a WHERE clause and as a function argument.

    Hello,
    My report has a lexical variable which has values like 'and id in (1,3,4)'. I want to use this variable in two separate queries. In the first, it is used in the WHERE clause, something like:
    .. where date is not null &var;
    In the second query, the variable is used in a function call, something like:
    select * from table (my_function(&var));
    The trouble is that the first query works when the value of the variable is unquoted, but for the second query the value should be quoted.
    Does anyone have an idea how I could solve this problem?
    Thanks in advance,
    Frans
    Message was edited by:
    fransie

    http://tkyte.blogspot.com/2006/06/varying-in-lists.html
    Presents many options.

  • Cardinality estimator 2014 is off with OR in where clause

    Here is my test setup on SQL Server 2014.
    -- Create big table
    CREATE TABLE [dbo].[Store](
    Id int IDENTITY(1,1) NOT NULL,
    City int NOT NULL,
    Size int NOT NULL,
    Name varchar(max) NULL,
    CONSTRAINT [PK_Store] PRIMARY KEY CLUSTERED ([Id] ASC)
    GO
    CREATE NONCLUSTERED INDEX [IX_Store] ON [dbo].[Store] (City ASC, Size ASC)
    GO
    -- Fill with 100k rows
    INSERT Store
    SELECT i % 101, i % 11, 'Store ' + CAST(i AS VARCHAR)
    FROM
    (SELECT TOP 100000 ROW_NUMBER() OVER (ORDER BY s1.[object_id]) AS i
    FROM sys.all_objects s1, sys.all_objects s2) numbers
    GO
    -- Create small table
    CREATE TABLE #StoreRequest (City int NOT NULL, Size int NOT NULL)
    GO
    INSERT #StoreRequest values (55, 1)
    INSERT #StoreRequest values (66, 2)
    Now I execute the following query (I force the index to show statistics estimates)
    SELECT s.City
    FROM #StoreRequest AS r
    INNER JOIN Store AS s WITH(INDEX(IX_Store), FORCESEEK)
    ON s.City = r.City AND s.Size = r.Size
    WHERE s.Size <> 1 OR r.City <> 55
    Here are the estimates that I get (I'm not allowed to upload pictures):
    Index Seek IX_Store
    Actual Number of Rows: 90
    Estimated Number of Rows: 50000
    Fixing WHERE clause to use one table not two makes the estimate perfect:
    SELECT s.City
    FROM #StoreRequest AS r
    INNER JOIN Store AS s WITH(INDEX(IX_Store), FORCESEEK)
    ON s.City = r.City AND s.Size = r.Size
    WHERE s.Size <> 1 OR s.City <> 55
    Index Seek IX_Store
    Actual Number of Rows: 90
    Estimated Number of Rows: 89.74
    Switching to 2012 compatibility mode gives estimate of 1 in both cases:
    Index Seek IX_Store
    Actual Number of Rows: 90
    Estimated Number of Rows: 1
    Could anyone explain the first result? I'm a bit worried about it. The fix in this case is trivial, but this problem gave us quite some headache in more complex real life queries with multiple joins.
    Thank you!

    But not full statistics on a field basis, just sometimes some default stats like total row count that some plans will build.  Even your StoreRequest table only has one two-field index that will have a full histogram.
    But I've seen SQL Server make massively bad plans on two-field indexes.
    I've seen SQL Server go wrong one-column indexes, so that is not a very relevant point.
    Temp tables or not, the estimate here is clearly incorrect. SQL Server knows the density of Size and City. It knows the cardinality of the temp table. The density information gives how many rows the the join will produce. The WHERE clause will then remove
    a certain number of rows. With no statistics for the temp table, it does not now how many, but it will apply some standard guess.
    50000 is a completely bogus number, because the join cannot produce that many rows, and SQL Server is able to compute the join with out the WHERE clause decently. (Well, it estimates 90, when the number is 180.) No, this is obviously a case of the cardinality
    estimator giving up completely.
    It is worth noting that both these WHERE clauses gives reasonable estimates:
     WHERE r.Size <> 11 OR r.City <> 550
     WHERE s.Size <> 11 OR s.City <> 550
    Whereas these two gives the spooky 50000:
     WHERE s.Size <> 11 OR r.City <> 550
     WHERE r.Size <> 11 OR s.City <> 550
    Erland Sommarskog, SQL Server MVP, [email protected]

  • How do you use 3 Where Clauses in a query

    Hi, i am trying to figure out how to use 3 Where Clauses in a Query where 2 of the Where Clauses uses a Sub query.
    Display the OrderID of all orders that where placed after all orders placed by “Bottom-Dollar Markets”.
    Order the result by OrderID in ascending order.
    First WHERE clause checks for OrderDate and uses a sub query with ALL keyword.
    Second WHERE clause use equals and sub query.
    Third WHERE clause uses equal and company name.
    This is what i have so far but i am pretty confused on how to do this.
    My Code for NorthWind:
    Select OrderID
    From Orders o
    Where o.OrderID IN (Select OrderDate From Orders Where Orders.OrderID > ALL
    (Select CompanyName From Customers Where CompanyName = 'Bottom-Dollar Markets'));
    The book shows how to use the ALL Keyword but not in a Sub query with Multiple Where Clauses.
    Select VenderName, InvoiceNumber, InvoiceTotal
    FROM Invoices JOIN Vendors ON Invoices.VendorID = Vendors.VendorID
    WHERE InvoiceTotal > ALL (Select InvoiceTotal From Invoices Where VendorID = 34)
    ORDER BY VendorName;

    >Where Orders.OrderDate
    > ALL  (Select
    CompanyName
    The comparison operator (>) requires compatible data types.
    DATETIME is not compatible with VARCHAR string for comparison.
    Here is your homework:
    SELECT orderid
    FROM orders o
    WHERE o.orderdate > ALL (SELECT orderdate
    FROM orders
    WHERE shipvia = (SELECT Max(shipvia)
    FROM orders o
    INNER JOIN customers c
    ON c.customerid =
    o.customerid
    WHERE
    c.companyname = 'Bottom-Dollar Markets'));
    11064
    11065
    11066
    11067
    11068
    11069
    11070
    11071
    11072
    11073
    11074
    11075
    11076
    11077
    Kalman Toth Database & OLAP Architect
    SQL Server 2014 Database Design
    New Book / Kindle: Beginner Database Design & SQL Programming Using Microsoft SQL Server 2014

  • Using :case when  in where clause

    Hello,
    I need some help with using of case statement in a where clause
    Table that contains info about employees taking some coursework:
    Class (values could be SAP, ORACLE, JAVA)
    Code (if Class is SAP then CODE is not null; if class is any other CODE is NULL)
    Start Date (date they began class not null)
    End Date (date then ended the class - could be null)
    Employee Level(numbers from one through five)
    I need a single LOV in forms that should show Employee_Level for input class,code,date.
    How to do this?
    I started off with this but get 'missing statement error'
    select distinct employee_level from e_course
       where (
       case when &class='SAP' then code ='1' and start_date > to_date('20000101','YYYYMMDD') and
                                               end_date < to_date('20000101','YYYYMMDD')
               else
                   null
       end) order by employee_level;Thanks

    Hi,
    user469956 wrote:
    But all these examples have only one condition for each case.Depending on how you count, all WHERE clauses have only one condition.
    I see an example in that thread that has 6 atomic conditions, linked by AND and OR.
    I need to check date & code. This is what is causing the error.Why do you want to use a CASE statement?
    Why can't you put all your conditions directly iinto a WHERE clause, soemthing like this:
    WHERE   (   &class='SAP'
            AND code ='1'
    OR      (   start_date  > to_date('20000101','YYYYMMDD')
            AND end_date    < to_date('20000101','YYYYMMDD')
            )I said "something like this" because I don't know what you really want to do.

  • Where clause problem with sub-queries in forms 6i

    where is the best place to put a sub query?? I have been using the set block property, however when running a sub query an alternative method must be used. what are some other options?? I have tried to have the query directly in the data block property palette but to no avail...
    There are 2 tables project and assignment
    maximum resources cannot be exceeded therefore a count of employees
    on the assignment table establishes that there are open spaces on the project.
    This code Here is my code for my where clause on the data block
    project.resources_maximum >
    (Select Count(assignment.employee_ID)
    From assignment
    Where assignment.project_ID = project.project_ID
    group by assignment.project_ID
    and project.end_date>sysdate;
    I was reading that the project table must be defined in the from clause
    to have the query go row by row, otherwise multiple rows are returned
    however I cannot define the project in the from,
    could this be why this doesn't work???
    original SQL that does work
    Need to migrate this to forms
    Select project.project_ID
    from project
    Where project.resources_maximum >
    (Select Count(assignment.employee_ID)
    From assignment
    Where assignment.project_ID = project.project_ID
    group by assignment.project_ID
    and project.end_date>sysdate;

    Looking at your query that works at the bottom, I do not see any reason that you would need to include the Assignment table in the From list. Just use the same where clause in the form:
      Where project.resources_maximum
        > (Select Count(assignment.employee_ID)
           From assignment
           Where assignment.project_ID = project.project_ID
           group by assignment.project_ID  )

  • Case stament use in where clause  grive error

    i want to use this conditions but gives error. Please give me the solution of it
    WHERE
    CASE
    WHEN CANCELLED_DATE IS NULL THEN
    TO_DATE(INV_GL_DATE,'DD-MON-YY') <= TO_DATE (:P_DATE1,'DD-MON-YY')
    ELSE
    TO_DATE(INV_GL_DATE,'DD-MON-YY') >= TO_DATE(:P_DATE1,'DD-MON-YY')
    END) --- <= TO_DATE(:P_DATE1,'DD-MON-YY')

    Hi,
    Remember that a CASE expression always evaluates to a single value in one of the SQL datatypes, such as VARCHAR2, NUMBER or DATE. (There is no Boolean datatype in SQL.)
    You can't say
    WHERE   CASE ... END;for the same reason that you can't say
    WHERE   'Hello, world!';orWHERE   100;or
    WHERE   SYSDATE;CASE expressions are really valuable because they allow you do perform IF-THEN-ELSE logic in places where you normally can't, like the SELECT clause.
    The WHERE clause is a place where you can perform IF-THEN-ELSE logic anyway, so there's rarely a need for a CASE expression in a WHERE clause. It's just as efficient and just as clear (if not more so) to put all your conditions directly in the WHERE clause, like Max demonstrated.
    Edited by: Frank Kulash on Feb 26, 2010 10:13 AM
    user11995078 wrote:
    Thanks Dear But how can we use with case and decodeWhy do you want to?
    CASE does not help in this problem, any more than regular expressions or CONNECT BY or MODEL help. They are all great tools for particular jobs, but not for this job. Asking "How can I use CASE to do this?" Is like asking "How can I use a hammer to tighten a bolt?" Perhaps there is a way, but it's likely to be contrived and ridiculous.
    Here's the least ridiculous way I can think of:
    WHERE     ( TO_DATE (INV_GL_DATE, 'DD-MON-YY') 
         - TO_DATE (:P_DATE1,     'DD-MON-YY')
         ) * CASE
                 WHEN  cancelled_date  IS NULL  THEN  1
                    `                            ELSE -1
             END <= 0

  • WHERE clause sources

    Hi,
    it is said that there are four WHERE clause sources:
    1. WHERE clause block property (on datablock or programatically defined)
    2. ONETIME_WHERE clause block property (on datablock)
    3. Example Record
    4. Query/Where dialog box
    I dont have any idea where I can find/ where the process takes place for items 3&4? Please enlighten me. Thanks

    You can update some data, and you can see lots of data you shouldn't. Eg in a form based on emp and with a restriction in the block's where clause property, you can bring up the Query/Where window and type garbage. Then the "Help > Display Error" window shows something like this
    SELECT ROWID,EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO
    FROM scott.emp WHERE deptno = user_dept_no() and (garbage!)The proper Where Clause will only let you see data from your own department, but if you put
    1=2) union select ROWID,EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO
    FROM scott.emp WHERE (1=1in the Query/Where window, you can see and update the data from all departments.
    And if you try
    1=2) union select null, null, table_name, null,null, null,null, null,null
    from all_tables WHERE (1=1then you're on your way towards getting access to lots of data which you shouldn't. A form with a large text field would be better for this sort of hacking.
    Trying to update through a function gets this:
    ORA-14551: cannot perform a DML operation inside a queryWith well defined synonyms, privileges and FGAC/VPD it should be safe to keep this enabled. Putting something after the colons means you don't need to know the column names.
    It's handy functionality but I've never been keen on the user-unfriendliness of this window. I'd rather see a dialog showing all the Prompt names and next to each would be a list containing 'Between, <=, != etc', with a free text field after that (or a date picker for date fields, something else for checkboxes, multi-select lists for the list fields, etc). Another for the wish list!

  • Empty WHERE Clause with AND condition after

    I have a question concerning a SQL query that I am using for a report. I have converted an old report to Crystal Reports 2008 and the resulting SQL query no longer works.
    After the conversion all the inner joins that were previously used in the report were moved to the FROM clause.
    My resulting WHERE clause is as follows:
    WHERE   AND
    "TABLE"."TABLEKEY" IS NOT NULL
    I get this error message:
    ORA-00936 Missing expression
    As you can see there WHERE clause has nothing before the AND condition because all the inner joins have moved to the FROM clause in the new version of Crystal Reports. I am fine with these inner joins moving but how do I take care of this empty condition.
    Any help is appreciated.

    Hi Joshua
    Please go through the following SAP note which deals with similar type of issue.
    https://www.sdn.sap.com/irj/servlet/prt/portal/prtroot/com.sap.km.cm.docs/oss_notes_boj/sdn_oss_boj_erq/sap(bD1lbiZjPTAwMQ==)/bc/bsp/spn/scn_bosap/notes%7B6163636573733d36393736354636443646363436353344333933393338323636393736354637333631373036453646373436353733354636453735364436323635373233443330333033303331333233313335333533393334%7D.do
    Thanks

  • Updating one table with mult. table where clause

    I'm having problems with my update statement. I want to update one table that has a mulitple table where clause. Not sure how to accomplish this. Here is what I have so far.
    update lawson.apvenmast a
    set vendor_status = 'I'
    where ((select * from apinvoice i
    where i.due_date <= TO_DATE('20011231', 'YYYYMMDD') and
    i.vendor = a.vendor)
    ((apvenmast.ven_class = 'INS') or
    (apvenmast.ven_class = 'REF')));
    Am I on the right track?
    thanks in advance for any help.
    Lisa Mears

    A lot is missing.
    where ((select * from apinvoice iA where clause should be like
    where <something> IN (select <something> from ...)
    ((apvenmast.ven_class = 'INS') or
    (apvenmast.ven_class = 'REF')));Where does this belong? There is no AND or OR with these two lines.
    Check your table aliases too.

  • "Invalid Column" on multiple where clauses with subqueries and cfqueryparam

    I'm seeing a behavior in the coldfusion cfquery that I'd like to find an exmplanation for .  I've got a query that does a subquery in the select portion and if I have multiple where lines, I get an "invalid column name" message for my second where clause, but only when I'm using cfqueryparam
    For example on the following I get "Invalid column name 'position_id'"
    SELECT   department_staff_tbl.*,
             (   SELECT   max(bookmark_id)
                 FROM     bookmarked_items_tbl
                 WHERE    item_id = department_staff_tbl.staff_id
             ) AS bookmark_id
    FROM     department_staff_tbl
    WHERE    department_id = <cfqueryparam value="#arguments.deptid#"  cfsqltype="cf_sql_integer">
    AND     position_id   = <cfqueryparam value="#arguments.posid#"   cfsqltype="cf_sql_integer">
    AND     staff_id      = <cfqueryparam value="#arguments.staffid#" cfsqltype="cf_sql_integer">
    If I change the order of my where clause so staff_id is first, then it tells me "department_id" is an invalid column.
    If I only have one where clause, it works.  (i.e. WHERE position_id = <cfqueryparam value="#arguments.posid#" cfsqltype="cf_sql_integer">).
    If I remove the where clause from my subquery (WHERE     item_id = department_staff_tbl.staff_id) it works.
    It also works if I remove the cfqueryparam from my where clause so that my query looks like this:
    SELECT   department_staff_tbl.*,
             (   SELECT   max(bookmark_id)
                 FROM     bookmarked_items_tbl
                 WHERE    item_id = department_staff_tbl.staff_id
             ) AS bookmark_id
    FROM     department_staff_tbl
    WHERE    department_id = #arguments.deptid#
    AND     position_id   = #arguments.posid#
    AND     staff_id      = #arguments.staffid#
    Any thoughts?

    I see two tables. So can the server. So, use qualified column-names.
    SELECT   department_staff_tbl.*,
             (   SELECT   max(bookmarked_items_tbl.bookmark_id)
                 FROM     bookmarked_items_tbl
                 WHERE    bookmarked_items_tbl.item_id = department_staff_tbl.staff_id
             ) AS bookmark_id
    FROM     department_staff_tbl
    WHERE    department_staff_tbl.department_id = <cfqueryparam value="#arguments.deptid#"  cfsqltype="cf_sql_integer">
    AND      department_staff_tbl.position_id   = <cfqueryparam value="#arguments.posid#"   cfsqltype="cf_sql_integer">
    AND      department_staff_tbl.staff_id      = <cfqueryparam value="#arguments.staffid#" cfsqltype="cf_sql_integer">

  • Measures constrained by WHERE clause

    Post Author: GregPye
    CA Forum: Semantic Layer and Data Connectivity
    Hi
    We have a fact table, containing Animal Count, and a dimension table joining onto the fact table, that gives us the species.
    We want to create separate measures in the universe, such that we get SpeciesA Count, SpeciesB Count, SpeciesC Count etc etc as measure objects in our universe.
    To do this we have constrained our measure by adding a WHERE clause, i.e. : Species_Dim.SpeciesName = 'SpeciesA'
    This causes an issue, because we end up getting SQL like this:
    SELECT
    Animal_Fact.Animal_Count,
    Animal_Fact.Animal_Count,
    Animal_Fact.Animal_Count
    FROM
    Animal_Fact, Species_Dim
    WHERE
    Species_Dim.Species_Name = 'SpeciesA'
    AND Species_Dim.Species_Name = 'SpeciesB'
    AND Species_Dim.Species_Name = 'SpeciesC'
    etc etc etc
    Obviously this runs SQL that will never return any results, because of these AND statements in the Where...
    Any ideas?
    Because the measure objects are all in the same table, the "Multiple SQL statements for each measure" is NOT a solution option here...
    I'd really rather not have to alias for each species, which I think is the solution, but thought I would put this problem out there to see if there are any better resolutions that my current one!!!!
    Many thanks!

    Greg,
    The 1st problem that I see is that you are using two tables w/o joining them.
    there are two ways to join them:
    1) in the WHERE clause:
    FROM
    Animal_Fact, Species_Dim
    WHERE
    Animal_Fact.AnimalID = Species_Dim.AnimalID AND (what ever additional criteria you need)
    2) actually using an explicit join (the preferred method):
    FROM
    Animal_Fact
    INNER JOIN Species_Dim
    ON Animal_Fact.AnimalID = Species_Dim.AnimalID
    WHERE
    (what ever additional criteria you need)
    In addition to the INNER JOIN, you have LEFT OUTER JOIN, RIGHT OUTER JOIN and FULL OUTER JOIN.
    -The RIGHT and FULL aren't used too often...
    The 2nd problem I see... the reason that you aren't getting any results is this:
    WHERE
    Species_Dim.Species_Name = 'SpeciesA'
    AND Species_Dim.Species_Name = 'SpeciesB'
    AND Species_Dim.Species_Name = 'SpeciesC'
    You're saying that "Species_Dim.Species_Name"; has to 3 different things... It can't, so you get no results.
    Hope this helps,
    Jason

  • Most efficient way to get row count with a where clause

    Have not found a definitive answer on how to do this.  Is there a better way to get a row count from a large table that needs to satisfy a where clause like so:
    SELECT COUNT(*) FROM BigTable WHERE TypeName = 'ABC'
    I have seen several posts suggesting something like 
    SELECT COUNT(*) FROM BigTable(NOLOCK);
    and 
    selectOBJECT_NAME(object_id),row_count from sys.dm_db_partition_stats
    whereOBJECT_NAME(object_id)='BigTable'but I need the row count that satisfies my where clause (i.e.: WHERE TypeName = 'ABC')

    It needs index to improve the performance. 
    - create index on typename column
    - create a indexed view to do the count in advance
    -partition on type name (although it's unlikely) then get count from the system tables...
    all those 3 solutions are about indexing.
    Regards
    John Huang, MVP-SQL, MCM-SQL, http://www.sqlnotes.info

  • Dynamic itab with Dynamic Where clause

    Hi, Dear All,
    Can someone provide a code extract for Dynamic where clause, i had already done with dynamic itab for a given set of fields, and i need to add where clause dynamically for a given field for a given range of values.
    select (i_fields) into table <dyn_table>
                      from (p_table)
                     where (v_where).
    In the above except the where clause, everything is done. Please help me.
    with best regards
    Mahesh

    Hi,
    here is the code extract for your reference.Pl. correct me.
    with regards
    REPORT  Z_DYN_ITAB                              .
    TYPE-POOLS: SLIS.
    DATA:NAME(100) TYPE C.
    TYPES: BEGIN OF ITAB_TYPE,
            WORD(20),
          END   OF ITAB_TYPE.
    DATA: ITAB TYPE STANDARD TABLE OF ITAB_TYPE WITH HEADER LINE.
    DATA: vg_fields(255) TYPE c,
          i_fields LIKE TABLE OF vg_fields.
    DATA: it_fcat TYPE slis_t_fieldcat_alv,
          is_fcat LIKE LINE OF it_fcat,
          ls_layout TYPE slis_layout_alv.
    DATA: it_fieldcat TYPE lvc_t_fcat,
          is_fieldcat LIKE LINE OF it_fieldcat.
    field-symbols: <dyn_table> type standard table,
                   <dyn_wa>,
                   <dyn_field>.
    data: dy_table type ref to data.
    data: dy_line type ref to data,
          xfc type lvc_s_fcat.
    DATA: v_where TYPE string, " Variable for storing where clause.
          v_dynamic(18) TYPE c, "variable to store select option datatype
          o_field TYPE REF TO cx_root," object to catch exception
          text TYPE string. "string variable to store exception text.
    CONSTANTS: c_var(15) TYPE c VALUE ' IN S_RANGE'.
    selection-screen begin of block b1 with frame.
    parameters: p_table(30) type c default 'T001',
                p_name(100) type c,
                p_field(10) TYPE c. " Parameter to capture field name.
    SELECT-OPTIONS: s_range FOR v_dynamic. " Select-option for range.
    selection-screen end of block b1.
    start-of-selection.
    NAME = p_name.
    SPLIT NAME AT ',' INTO TABLE ITAB.
    LOOP AT ITAB.
    is_fcat-fieldname = itab-word.
    is_fcat-tabname = p_table.
    APPEND is_fcat to it_fcat.
    ENDLOOP.
    LOOP AT it_fcat INTO is_fcat.
      is_fieldcat-fieldname = is_fcat-fieldname.
      is_fieldcat-tabname = is_fcat-tabname.
      APPEND is_fieldcat TO it_fieldcat.
      CONCATENATE is_fieldcat-tabname is_fieldcat-fieldname INTO
            vg_fields SEPARATED BY '~'.
      APPEND vg_fields TO i_fields.
    ENDLOOP.
    perform create_dynamic_itab.
    perform get_data.
    Create dynamic internal table and assign to FS
    form create_dynamic_itab.
    call method cl_alv_table_create=>create_dynamic_table
    exporting
    it_fieldcatalog = it_fieldcat
    importing
    ep_table = dy_table.
    assign dy_table->* to <dyn_table>.
    Create dynamic work area and assign to FS
    create data dy_line like line of <dyn_table>.
    assign dy_line->* to <dyn_wa>.
    endform.
    form get_data.
    Select Data from table.
    CONCATENATE p_field c_var INTO v_where.
    TRY.
    select (i_fields) into table <dyn_table>
                      from (p_table)
                     where (v_where).
    if sy-dbcnt = 0.
    write : text-t02.
    endif.
    *Write out data from table.
    Loop at <dyn_table> into <dyn_wa>.
    do.
    assign component sy-index of structure <dyn_wa> to <dyn_field>.
    if sy-subrc <> 0.
      exit.
    endif.
    if sy-index = 1.
      write:/ <dyn_field>.
    else.
      write: <dyn_field>.
    endif.
    enddo.
    endloop.
    Exception Catching.
    CATCH cx_root INTO o_field.
    text = o_field->get_text( ).
    Calling Function to give information message regarding Exception
    CALL FUNCTION 'POPUP_TO_INFORM'
    EXPORTING
    titel = text-t03
    txt1 = text
    txt2 = text-t04.
    TXT3 = ' '
    TXT4 = ' '
    LEAVE TO LIST-PROCESSING.
    ENDTRY.
    endform.

Maybe you are looking for