14
Embedded SQL (Cont.) Pertemuan 10 Matakuliah : T0413/Current Popular IT II Tahun : 2007

Embedded SQL (Cont.) Pertemuan 10 Matakuliah: T0413/Current Popular IT II Tahun: 2007

Embed Size (px)

Citation preview

Page 1: Embedded SQL (Cont.) Pertemuan 10 Matakuliah: T0413/Current Popular IT II Tahun: 2007

Embedded SQL (Cont.)Pertemuan 10

Matakuliah : T0413/Current Popular IT IITahun : 2007

Page 2: Embedded SQL (Cont.) Pertemuan 10 Matakuliah: T0413/Current Popular IT II Tahun: 2007

Bina Nusantara

Learning Outcomes

Pada akhir pertemuan ini, diharapkan mahasiswa akan mampu menciptakan aplikasi sistem basisdata menggunakan konsep embeded SQL di dalam program

Page 3: Embedded SQL (Cont.) Pertemuan 10 Matakuliah: T0413/Current Popular IT II Tahun: 2007

Bina Nusantara

Outline Materi

• Using host language variables with SQL• Using SQLCODE and SQLSTATE• Updating cursors• Indicator variables• Examples

Page 4: Embedded SQL (Cont.) Pertemuan 10 Matakuliah: T0413/Current Popular IT II Tahun: 2007

Bina Nusantara

Using Host language variables with SQL

• We can use variables from the host program in embedded SQL state

• Host variable must :– Be declared in a SQL DECLARE SECTION– Compatible data type

Page 5: Embedded SQL (Cont.) Pertemuan 10 Matakuliah: T0413/Current Popular IT II Tahun: 2007

Bina Nusantara

Using Host language variables with SQL (Cont.)

• Be assign a value at the time they are used in the SQL statement

• Be preceed by a colon (:)

Page 6: Embedded SQL (Cont.) Pertemuan 10 Matakuliah: T0413/Current Popular IT II Tahun: 2007

Bina Nusantara

DECLARING VARIABLE

• BEGIN DECLARE SECTION• END BEGIN SECTION

Page 7: Embedded SQL (Cont.) Pertemuan 10 Matakuliah: T0413/Current Popular IT II Tahun: 2007

Bina Nusantara

RETRIEVING VALUES INTO VARIABLE

• Use SELECT INTO• Constraint : can’t retrieves multiple

rows and inserted into the same variables at the same time

Page 8: Embedded SQL (Cont.) Pertemuan 10 Matakuliah: T0413/Current Popular IT II Tahun: 2007

Bina Nusantara

USING CURSOR• Example :

EXEC SQL DECLARE cursor londonstate FOR

SELECT * FROM salesPeopleWHERE city = ‘London’

Page 9: Embedded SQL (Cont.) Pertemuan 10 Matakuliah: T0413/Current Popular IT II Tahun: 2007

Bina Nusantara

USING CURSOR• FETCH• OPEN CURSOR• CLOSE CURSOR

Page 10: Embedded SQL (Cont.) Pertemuan 10 Matakuliah: T0413/Current Popular IT II Tahun: 2007

Bina Nusantara

USING SQL CODE AND SQL STATE

• SQL CODE• SQL STATE

Page 11: Embedded SQL (Cont.) Pertemuan 10 Matakuliah: T0413/Current Popular IT II Tahun: 2007

Bina Nusantara

UPDATING CURSOR• Use cursor to select a group of rows

from a table that can be Updated and Deleted

• Example : increasing the comission of all sales people who have customers with a rating of 300 in the following example

Page 12: Embedded SQL (Cont.) Pertemuan 10 Matakuliah: T0413/Current Popular IT II Tahun: 2007

Bina Nusantara

EXAMPLE UPDATING CURSOR• EXEC SQL OPEN CURSOR High_Cust

while SQLSTATE=‘00000’ dobeginEXECT SQL FETCH High_CustINTO : id_num, :salesperson, :loc, :comm;end;EXECT SQL UPDATE salespeopleSET comm = comm + .01WHERE CURRENT OF High_cust;

end;EXECT SQL CLOSE CURSOR High_Cust;

Page 13: Embedded SQL (Cont.) Pertemuan 10 Matakuliah: T0413/Current Popular IT II Tahun: 2007

Bina Nusantara

INDICATOR VARIABLES• SQLNULL• SQLSTATE• SQLCODE• SQL EXACT NUMERIC

Page 14: Embedded SQL (Cont.) Pertemuan 10 Matakuliah: T0413/Current Popular IT II Tahun: 2007

Bina Nusantara

OTHER USES OF INDICATOR VARIABLES

• USED TO INDICATE TRUNCATION• TO ASSIGN NULL VALUES