View
218
Download
3
Category
Preview:
Citation preview
Embedded SQL (Cont.)Pertemuan 10
Matakuliah : T0413/Current Popular IT IITahun : 2007
Bina Nusantara
Learning Outcomes
Pada akhir pertemuan ini, diharapkan mahasiswa akan mampu menciptakan aplikasi sistem basisdata menggunakan konsep embeded SQL di dalam program
Bina Nusantara
Outline Materi
• Using host language variables with SQL• Using SQLCODE and SQLSTATE• Updating cursors• Indicator variables• Examples
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
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 (:)
Bina Nusantara
DECLARING VARIABLE
• BEGIN DECLARE SECTION• END BEGIN SECTION
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
Bina Nusantara
USING CURSOR• Example :
EXEC SQL DECLARE cursor londonstate FOR
SELECT * FROM salesPeopleWHERE city = ‘London’
Bina Nusantara
USING CURSOR• FETCH• OPEN CURSOR• CLOSE CURSOR
Bina Nusantara
USING SQL CODE AND SQL STATE
• SQL CODE• SQL STATE
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
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;
Bina Nusantara
INDICATOR VARIABLES• SQLNULL• SQLSTATE• SQLCODE• SQL EXACT NUMERIC
Bina Nusantara
OTHER USES OF INDICATOR VARIABLES
• USED TO INDICATE TRUNCATION• TO ASSIGN NULL VALUES
Recommended