cancel
Showing results for 
Search instead for 
Did you mean: 

Select Statements

Former Member
0 Kudos

Hi All,

1.What are select statements and how to use this statements. please tell me with syntax of all different statements?

Thanks

Srinivas

Accepted Solutions (0)

Answers (5)

Answers (5)

Former Member
0 Kudos

Hi,

The best way is put a cursor on your select statement and click on f1 and you will get all select queries..

REWARD IF USEFUL

Former Member
0 Kudos

hi,

To write a report based on data inside the database, you must use either the SELECT statement or a logical database and the GET event.

Open SQL ABAP SELECT statement can only be used on database tables that have been defined in the ABAP Dictionary with a primary key.

The SELECT statement can access data by three methods:

Single access with fully qualified key

Loop processing with restriction

Loop processing without restriction

The SELECT SINGLE statement allows you to access a single table entry (one record).

You must include the full key (except “client”) of the table in the WHERE clause. Key field names do not need to be prefixed with the table name. Only the equal (= or ‘EQ’) relational operator can be used.

Notice that you do not code an ENDSELECT because only one record will be returned.

The TABLES statement is used in conjunction with SELECT to create a work area (within memory) for the SELECT statement to use to place the data record into, after it has located the record.

The return code (SY-SUBRC) will be set to either of the following values after the SELECT statement finishes:

0 = Read was successful

4 = Record does not exist

The WHERE clause is made up by comparing a table field to another field or to a value, using one of the relational operators shown above. WHERE clause is to specify selection condition.

AND is used to specify a selection condition for more than one table field or for more than one unique value on the same table field.

This type of SELECT statement requires an ENDSELECT statement. A processing loop is started with SELECT and concludes with ENDSELECT.

Several other type of comparisons ( BETWEEN, LIKE, IN) can be made in the WHERE clause to specify ranges, masked literals/templates and field lists.

The ORDER BY clause in a SELECT statement allows you to sort the table entries.

pls reward if helpful.

Thanks,

Rajyalakshmi.

Former Member
0 Kudos

Hi, Srinu.

There are many types of select. Here's one in particular - You must be careful to use that one:

 
SELECT SINGLE [field]
FROM [dbtab]
(INTO / APPENDING) TABLE   [itab]
WHERE [condition]

If you have the complete primary key, and want to select only one record, you can use this SELECT above. But if you don't have the complete primary key, use this one:


SELECT [field] UP TO 1  ROWS
FROM [dbtab]
(INTO / APPENDING) TABLE   [itab]
WHERE [condition]

For this and much more help, check out the ABAP HELP for the command SELECT!

Kind Regards,

Brian Gonsales

Edited by: Brian Gonsales on Apr 17, 2008 11:09 AM

Former Member
0 Kudos

Hi

select statements...we generally use these statements to select the data from database tables...........

SELECT * FROM -


INTO itab.

ENDSELECT.

select * from table into itab.

ENDSELECT

select*from into where="username" endselect

select * from tablename where name =" " ;

SELECT SINGLE … WHERE …

SELECT [DISTINCT] … WHERE …

Former Member
0 Kudos

Hey Fren,

SELECT statement is used to access the database to retrieve the particular record or set of records from one or more database tables.

According to SAP Library,

The Basic syntax of SELECT is as follows:

SELECT      <result> 
  INTO      <target>
  FROM      <source> 
  [ WHERE    <condition> ]
  [ GROUP BY <fields> ] 
  [ HAVING   <cond> ]
  [ ORDER BY <fields> ].

Here

  • result --> The SELECT clause defines the structure of the data you want to read, that is, whether one line or several, which columns you want to read, and whether identical entries are acceptable or not.

  • target --> The INTO clause determines the target area <target> into which the selected data is to be read.

  • source --> The FROM clause specifies the database table or view <source> from which the data is to be selected. It can also be placed before the INTO clause.

  • condition --> The WHERE clause specifies which lines are to be read by specifying conditions for the selection

  • fields --> The GROUP-BY clause produces a single line of results from groups of several lines. A group is a set of lines with identical values for each column listed in <fields>.

  • cond --> The HAVING clause sets logical conditions for the lines combined using GROUP BY.

  • fields --> The ORDER-BY clause defines a sequence <fields> for the lines resulting from the selection.

Now,

We have the following variations in SELECT statement.

Reading a Single Line

To read a single entry from the database, use the following:

SELECT SINGLE field1
              field2
              field3
        INTO  work_area
        FROM dbtab
        WHERE field1 EQ <somevalue>.

This will hit the database once per record so normally we dont use select single in a loop.

If the system finds a line with the corresponding key, SY-SUBRC is set to 0, otherwise to 4.

Reading Several Lines

To read several entries from the database, use the following:

This will retrieve the records one at a time..

SELECT [DISTINCT] field1
           field2
           field3
           field4
           .....
           .....
           field n
     INTO   work_area
     FROM dbtab
     WHERE  field1 EQ p_param1
        AND field2 IN s_selopt1.

       ENDSELECT.

Or you can have this option to retrieve the records at once into an Internal Table.

 SELECT [DISTINCT] field1
           field2
           field3
           field4
           .....
           .....
           field n
     INTO  TABLE itab
     FROM dbtab
     WHERE  field1 EQ p_param1
        AND field2 IN s_selopt1.

In WHERE clause we used p_param1 as a Parameters and s_selopt1 as Select-Options in the Selection-Screen.

In both the above variations shown, we can write the select query as follows:

SELECT [DISTINCT] *
     INTO  work_area
     FROM dbtab
     WHERE  field1 EQ p_param1
        AND field2 IN s_selopt1.


     SELECT [DISTINCT] *
     INTO  TABLE itab
     FROM dbtab
     WHERE  field1 EQ p_param1
        AND field2 IN s_selopt1.

By replacing the fieldnames with ' * ' we retrieve all the columns from the database table instead of the limited columns.

We use this options when we require almost all the columns to be processed in the program..

Here if we use DISTINCT then the SELECT query will not fetch the duplicate records for the same combination.

Reading packets into internal table

SELECT field1 field2
FROM   dbtab
INTO  TABLE ITAB
       PACKAGE SIZE 3.

Here, if we display the contents of the internal table,

records will be displayed as follows:

- - - - -- - - - -- - - - -- - - - -- - - - -- - - - -- - - - -- - - - -- - - - -

- - - - -- - - - -- - - - -- - - - -- - - - -- - - - -- - - - -- - - - -- - - - -

- - - - -- - - - -- - - - -- - - - -- - - - -- - - - -- - - - -- - - - -- - - - -

- - - - -- - - - -- - - - -- - - - -- - - - -- - - - -- - - - -- - - - -- - - - -

- - - - -- - - - -- - - - -- - - - -- - - - -- - - - -- - - - -- - - - -- - - - -

- - - - -- - - - -- - - - -- - - - -- - - - -- - - - -- - - - -- - - - -- - - - -

- - - - - - - - - -- - - - -- - - - -- - - - -- - - - -- - - - -- - - - -- - - - -

- - - - -- - - - -- - - - -- - - - -- - - - -- - - - -- - - - -- - - - -- - - - --

Reading till 'n' number of rows

SELECT <result>
    FROM <source>
    INTO TABLE itab
    UPTO n ROWS
    WHERE <condition>

This will retrieve 'n' number of records from the database table.

This was all about SELECT Statement.....

You can explore more on the SAP Library to go into details....

For online help regarding SELECT Query,

Please refer this link...

[http://help.sap.com/saphelp_nw70/helpdata/EN/66/f9545ed3654bd4b55bf7a5b9953a53/frameset.htm]

Inspire if needful,

Warm Regards,

Abhi...