You are on page 1of 18

Structured Query Language

©Copyright 2004, Cognizant Academy, All Rights Reserved 1


Overview
• This is used to communicate with data base
management system.
• With SQL data manipulation such as display, insert,
update and delete operations can be performed.
• It is an ANSII standard language.
• The SQL commands can be given on an editor which
can be obtained by STRSQL command.

©Copyright 2004, Cognizant Academy, All Rights Reserved 2


SELECT Statement
• SELECT statement is used for displaying
all/certain columns.
• A simple select statements looks like this
SELECT * FROM USRLIB/FILEA
• This would display all the records in with all the
fields in FILEA.
... contd

©Copyright 2004, Cognizant Academy, All Rights Reserved 3


SELECT Statement
• A complex SELECT statement looks like this.
SELECT ENAME, ESAL FROM
USRLIB/EMPMST WHERE EDEPT = ‘AS/400’
ORDER BY ENO
• The above statement would select the field values
of ENAME, ESAL from EMPMST file for the
records whose EDEPT value is

©Copyright 2004, Cognizant Academy, All Rights Reserved 4


SELECT Statement
“AS/400” and they would be retrieved in the
sequential order of ENO.
• The SELECT also has grouping on a field value,
defining new fields, arithmetic expressions support,
multiple conditions using logical operators like AND
and OR.
• The output of a SELECT statement can be directed
to a printer file or to a physical file. This done by
going to screen “Change Session Attributes” by
pressing <F13>.
©Copyright 2004, Cognizant Academy, All Rights Reserved 5
INSERT Statement
• INSERT is used to create a new record with the
values given.
• Ex : INSERT INTO EMPMST (ENO, ENAME,
EDEPT, ESAL) VALUES(‘4023’, ‘KAMAL’,
‘AS/400’, ‘25000’)
• The values given in values braces are stored in
corresponding fields.

©Copyright 2004, Cognizant Academy, All Rights Reserved 6


UPDATE Statements
• UPDATE is used to update one or more records in a
file.
• Ex : UPDATE EMPMST SET ESAL = ‘28500’
WHERE ENO = ‘4023’
• This query updates the employee record for ESAL
whose ENO is “4023”.

©Copyright 2004, Cognizant Academy, All Rights Reserved 7


DELETE Statement
• This statement is used to delete one or more records
in a file.
• Ex : DELETE FROM EMPMST WHERE ENO =
‘4023’
• This query deletes the record whose ENO field
value is “4023”.

©Copyright 2004, Cognizant Academy, All Rights Reserved 8


Interactive Select
• The selection of files, fields, conditions etc can
be selected by pressing <F4> on the query editor.
• The command to be used can be selected and the
corresponding screen appears.

©Copyright 2004, Cognizant Academy, All Rights Reserved 9


Join Operation
• The joining of two tables is done based on a
common column values.
• Ex : SELECT * FROM EMPMST, EMPDET
WHERE ENO = EMPNO
• This would select the records from Cartesian
product where specified key is matching in the two
tables.

©Copyright 2004, Cognizant Academy, All Rights Reserved 10


Sub Queries
• It is used for nested queries. A query selection is
based on another queries output.
• Ex : SELECT * FROM EMPMST WHERE ENO
IN (SELECT EMPNO FROM EMPDET)
• The above query gives details of all employees
from EMPMST whose details are available in
EMPDET.

©Copyright 2004, Cognizant Academy, All Rights Reserved 11


Embedded SQL
• SQL statements can be included in a high level
language.
• Ex : SQL used in RPGLE.
C/EXEC SQL
C+ DELETE FROM USRLIB/EMPMST C+
WHERE ENO = ‘4023’
C/END-EXEC
... contd
©Copyright 2004, Cognizant Academy, All Rights Reserved 12
Embedded SQL
• SQL statements would be written in ‘C’
specification.
• Between EXEC SQL and END-EXEC the SQL
statement would be written.
• An RPGLE program with embedded SQL
statements would be of type SQLRPGLE and be
compiled with CL command “CRTSQLRPGI”.

©Copyright 2004, Cognizant Academy, All Rights Reserved 13


Usage of Cursor
• The subset of records selected from an embedded
query in a high level language program is called
cursor.
• Ex :
C/EXEC SQL
C+ Declare Empcursor Cursor
C+ For Select * From EMPMST order by ENO
C/END-EXEC
... contd
©Copyright 2004, Cognizant Academy, All Rights Reserved 14
Usage of Cursor

C/EXEC SQL
C+ Open Empcursor
C/END-EXEC
C/EXEC SQL
C+ Fetch Next
C+ from Empcursor
C+ Into :ENUMB, :ENAM, :EDEPAT,
C+ :ESALAR
C/END-EXEC
... contd
©Copyright 2004, Cognizant Academy, All Rights Reserved 15
Usage of Cursor
C/EXEC SQL
C+ Close Empcursor
C/END-EXEC

• Declare is used to declare the name of the cursor


followed by the SQL statement.
• Open statement is used to make the cursor ready
to use in the program.

©Copyright 2004, Cognizant Academy, All Rights Reserved 16


Usage of Cursor
• Fetch statement is used to read the cursor records
into the program fields which are given with “:”
before them.
• Close statement is used to close the cursor. After a
cursor is closed, records cannot be fetched from the
cursor.

©Copyright 2004, Cognizant Academy, All Rights Reserved 17


Thank You

©Copyright 2004, Cognizant Academy, All Rights Reserved 18

You might also like