WordPress Websites for Engineers: Elevate Your Brand
SQl
1. ICS 184: Introduction to Data Management Lecture Note 13: Stored Procedures and Embedded SQL
2.
3. Architecture Client Application DB Client Normal Database Network DB Client Client Application DB Client Stored Procedures Applications using stored procedures Network DB Client DB Server DB DB Server DB
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17. Example Void simpleInsert() { EXEC SQL BEGIN DECLARE SECTION; char productName[20], company[30]; char SQLSTATE[6]; EXEC SQL END DECLARE SECTION; /* get values for productName and company somehow */ EXEC SQL INSERT INTO Product(name, company) VALUES (:productName, :company); } Variable SQLSTATE provides error messages and status reports (e.g., 00000 means operation completed without problem).
18. Single-Row Select Statements Void getPrice() { EXEC SQL BEGIN DECLARE SECTION; char productName[20], company[30]; integer price; char SQLSTATE[6]; EXEC SQL END DECLARE SECTION; /* read value of product name */ EXEC SQL SELECT price INTO :price FROM Product WHERE Product.name = :productName; /* print out value of price */ }
19. Cursors Motivation: go through the results of a query EXEC SQL DECLARE cursorName CURSOR FOR SELECT …. FROM …. WHERE …. ; EXEC SQL OPEN cursorName ; while (true) { EXEC SQL FETCH FROM cursorName INTO :variables; if ( NO_MORE_TUPLES ) break; /* do something with values */ } EXEC SQL CLOSE cursorName ;