Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select statement with offset?

I'm trying to use this SELECT statement in ABAP:

  SELECT DISTINCT * FROM  dbtab
     INTO CORRESPONDING FIELDS OF TABLE itab
     WHERE  field1+7(16)  IN s_field1
     AND    field2        IN s_field2.

but I can't use offset for a dbtab column. How can I solve this problem?

I'm trying to avoid loop like

  SELECT DISTINCT * FROM  dbtab
     WHERE  field2        IN s_field2.
       IF field1+7(16)  IN s_field1
           ...
       endif.
  endselect.
like image 430
Darko Avatar asked Jan 15 '16 16:01

Darko


People also ask

How do you use offset in SELECT query?

OFFSET and FETCH Clause are used in conjunction with SELECT and ORDER BY clause to provide a means to retrieve a range of records. The OFFSET argument is used to identify the starting point to return rows from a result set. Basically, it exclude the first set of records.

What is offset in query?

The OFFSET clause in a SELECT query causes the result set to start some number of rows after the logical first item. The result set is numbered starting from zero, so OFFSET 0 produces the same result as leaving out the OFFSET clause.

How do I write an offset in SQL?

OFFSET in SQL is generally used with the ORDER BY clause with a value greater than or equal to zero. Syntax using OFFSET: SELECT column_names FROM table_name ORDER BY column_names OFFSET n ROWS //n is the number of rows to be excluded.

How do you use LIMIT and offset in query?

If a limit count is given, no more than that many rows will be returned (but possibly fewer, if the query itself yields fewer rows). LIMIT ALL is the same as omitting the LIMIT clause, as is LIMIT with a NULL argument. OFFSET says to skip that many rows before beginning to return rows.


1 Answers

You can't use offset in OPEN SQL.

I would recommend to make a SELECT into an internal table and loop over it like this.

SELECT DISTINCT * FROM dbtab
INTO CORRESPONDING FIELDS OF TABLE itab
WHERE field2 IN s_field2.

LOOP AT dbtab into wa_itab.
  IF wa_itab-field1+7(16) IN s_field1
    ...
  ENDIF.
ENDLOOP.

On the other hand I would also define the internal table as SORTED or HASHED or if you prefer try to SORT itab by the field you are making the comparison. Field symbols could be an alternative also.

Hope it helps.

like image 115
Nelson Miranda Avatar answered Nov 03 '22 22:11

Nelson Miranda