SlideShare una empresa de Scribd logo
1 de 5
Descargar para leer sin conexión
CS145 Lecture Notes #10
                  SQL Programming
Example schema:
   CREATE TABLE Student (SID INTEGER PRIMARY KEY,
                         name CHAR(30),
                         age INTEGER,
                         GPA FLOAT);
   CREATE TABLE Take    (SID INTEGER,
                         CID CHAR(10),
                         PRIMARY KEY(SID, CID));
   CREATE TABLE Course (CID CHAR(10) PRIMARY KEY,
                         title VARCHAR(100) UNIQUE);


Motivation
Pros and cons of SQL:
      Very high-level, possible to optimize
      Not tuned to support general-purpose computation
            Oracle as a calculator? SELECT 142857*3 FROM DUAL;
      Strictly less expressive than general-purpose languages
            SQL2 has no recursion and cannot even compute factorial!
Solutions:
      Augment SQL: Oracle’s PL/SQL
      Use SQL together with a general-purpose programming language:
      embedded SQL, dynamic SQL


Oracle PL/SQL
Basics
Rough form of a PL/SQL program:
   DECLARE

   BEGIN

   END;
   .
   RUN;

  DECLARE section is optional
  . and RUN end the program and execute it



Jun Yang                    1                    CS145 Spring 1999
Example: go through students 142–857 and set all GPA’s under 4.0 to 4.0
   DECLARE
     thisSID Student.SID%TYPE;
     thisGPA Student.GPA%TYPE;
   BEGIN
     thisSID := 142;
     LOOP
       EXIT WHEN (thisSID > 857);
       SELECT GPA INTO thisGPA
       FROM Student
       WHERE SID = thisSID;
       IF (thisGPA < 4.0) THEN
         UPDATE Student SET GPA = 4.0
         WHERE SID = thisSID;
       END IF;
       thisSID := thisSID + 1;
     END LOOP;
   END;
   .
   RUN;

Basic features:
      Local variable:
           Use %TYPE to match its type to a column in the schema
           Use := for assignment; = for comparison
      Branch: IF (...) THEN ... ELSE ... END IF;
      Loop: LOOP ... EXIT WHEN (...); ... END LOOP;
      The usual data modification statements: INSERT, DELETE, UPDATE
      Single-row SELECT: SELECT ... INTO ... FROM ...;
           Oracle raises an exception if SELECT returns no rows or more
           than one row


Cursors
Inside a PL/SQL program, the result of a SELECT must go somewhere:
      If SELECT returns one row, it can go INTO a variable
      What if SELECT returns multiple rows?
   Cursor: a variable that runs through the result of a SELECT, row by row
      Declare by: CURSOR                IS        ;
      Use inside a cursor loop:
           Fetch one result row at a time: FETCH              INTO      ;
           Break the loop when there are no more rows to return:
           EXIT WHEN              %NOTFOUND;
      OPEN/CLOSE before/after use



Jun Yang                      2                      CS145 Spring 1999
If cursor is over a single table and has no aggregates or DISTINCT, we can
also modify data through the cursor:
       Follow the declaration by FOR UPDATE
       Use WHERE CURRENT OF                       in DELETE or UPDATE
Example: go through all CS145 students and set all GPA’s under 4.0 to 4.0!
    Note it is possible to declare a “row” type in Oracle
   DECLARE
     thisStudent Student%ROWTYPE;
     CURSOR CS145Student IS
       SELECT * FROM Student WHERE SID IN
         (SELECT SID FROM Take WHERE CID = ’CS145’)
       FOR UPDATE;
   BEGIN
     OPEN CS145Student;
     LOOP
       FETCH CS145Student INTO thisStudent;
       EXIT WHEN (CS145Student%NOTFOUND);
       IF (thisStudent.GPA < 4.0) THEN
         UPDATE Student SET GPA = 4.0
         WHERE CURRENT OF CS145Student;
       END IF;
     END LOOP;
     CLOSE CS145Student;
   END;
   .
   RUN;

Stored Procedures
Creating a PL/SQL stored procedure:
   CREATE PROCEDURE                   (              ) AS

   BEGIN

   END;
   .
   RUN;

  The RUN above creates the procedure, but does not execute it
Running the procedure inside a PL/SQL program:
   BEGIN
           ...
                        (    );
           ...
   END;
   .
   RUN;


Jun Yang                      3                      CS145 Spring 1999
Dropping the procedure:
   DROP PROCEDURE                   ;

Example: a procedure to enroll students in CS145
   CREATE PROCEDURE
     CS145Enroll (thisSID IN Take.SID%TYPE) AS
   BEGIN
     INSERT INTO Take VALUES(thisSID, ’CS145’);
   END;
   .
   RUN;

Example: students 142 and 857 enroll in CS145
   BEGIN
     CS145Enroll(142);
     CS145Enroll(857);
   END;
   .
   RUN;


Embedded SQL
Instead of making SQL do more, embed it into a general-purpose program-
ming language (C in our examples):
  (1) Host program with special SQL directives and commands
      Goes through a special DBMS preprocessor to produce:
  (2) Host program with special DBMS function calls
      Gets compiled and linked with special DBMS library to produce:
  (3) Executable code that communicates with the DBMS

Main issues in embedding SQL within a program:
     Which statements are SQL?
           Those introduced with EXEC SQL
     How are values passed from the program into SQL commands?
           Shared Variables: variables that are accessible to both SQL and
           the host language
                In C, variables are used normally
                In SQL, they must be preceded by a colon
     How are results of SQL queries returned into program variables?
           If query returns a single row, use SELECT INTO
           If query returns many rows, use a cursor
                Similar to PL/SQL cursors, with minor syntactic differences
                In Oracle, WHENEVER can be used to break cursor loops


Jun Yang                       4                      CS145 Spring 1999
Example: go through all CS145 students and change all GPA’s

   EXEC SQL BEGIN DECLARE SECTION;
   int thisSID;
   float thisGPA;
   EXEC SQL END DECLARE SECTION;

   EXEC SQL DECLARE CS145Student CURSOR FOR
     SELECT SID, GPA FROM Student WHERE SID IN
       (SELECT SID FROM Take WHERE CID = ’CS145’)
     FOR UPDATE;

   EXEC SQL OPEN CS145Student;
   EXEC SQL WHENEVER NOT FOUND DO break;
   while (1) {
     EXEC SQL FETCH CS145Student INTO :thisSID, :thisGPA;
     printf("SID %d current GPA %fn", thisSID, thisGPA);
     printf("Enter new GPA: ");
     scanf("%f", &thisGPA);
     EXEC SQL UPDATE Student SET GPA = :thisGPA
       WHERE CURRENT OF CS145Student;
   }
   EXEC SQL CLOSE CS145Student;


Dynamic SQL
    Embedded SQL is fine for fixed applications (e.g., a program used by
    the registrar to enroll students in classes)
    But we cannot use it to write a program like sqlplus, because we
    do not know in advance what SQL statements the user will enter
  Two special statements in embedded SQL which make it dynamic:
    PREPARE
    EXECUTE
Example: let’s write our own sqlplus!

   EXEC SQL BEGIN DECLARE SECTION;
   char query[MAX_QUERY_LENGTH];
   EXEC SQL END DECLARE SECTION;

   while (1) {
     /* issue SQL> prompt */
     /* read user input into query */
     EXEC SQL PREPARE q FROM :query;
     EXEC SQL EXECUTE q;
   }




Jun Yang                     5                     CS145 Spring 1999

Más contenido relacionado

Similar a as (20)

SQL- Introduction to PL/SQL
SQL- Introduction to  PL/SQLSQL- Introduction to  PL/SQL
SQL- Introduction to PL/SQL
 
Plsql
PlsqlPlsql
Plsql
 
PLSQLIntroduction.ppt
PLSQLIntroduction.pptPLSQLIntroduction.ppt
PLSQLIntroduction.ppt
 
Unit 3(rdbms)
Unit 3(rdbms)Unit 3(rdbms)
Unit 3(rdbms)
 
Unit 3(rdbms)
Unit 3(rdbms)Unit 3(rdbms)
Unit 3(rdbms)
 
Cursor
CursorCursor
Cursor
 
Cursor
CursorCursor
Cursor
 
PLSQL
PLSQLPLSQL
PLSQL
 
L9 l10 server side programming
L9 l10  server side programmingL9 l10  server side programming
L9 l10 server side programming
 
PL-SQL.pdf
PL-SQL.pdfPL-SQL.pdf
PL-SQL.pdf
 
SQl
SQlSQl
SQl
 
plsql les06
 plsql les06 plsql les06
plsql les06
 
PL/SQL Introduction and Concepts
PL/SQL Introduction and Concepts PL/SQL Introduction and Concepts
PL/SQL Introduction and Concepts
 
Procedures andcursors
Procedures andcursorsProcedures andcursors
Procedures andcursors
 
Pl sql
Pl sqlPl sql
Pl sql
 
PL/SQL CURSORES
PL/SQL CURSORESPL/SQL CURSORES
PL/SQL CURSORES
 
4. plsql 1
4. plsql 14. plsql 1
4. plsql 1
 
Sql Objects And PL/SQL
Sql Objects And PL/SQLSql Objects And PL/SQL
Sql Objects And PL/SQL
 
SQL / PL
SQL / PLSQL / PL
SQL / PL
 
stored.ppt
stored.pptstored.ppt
stored.ppt
 

Último

Embed-2 (1).pdfb[k[k[[k[kkkpkdpokkdpkopko
Embed-2 (1).pdfb[k[k[[k[kkkpkdpokkdpkopkoEmbed-2 (1).pdfb[k[k[[k[kkkpkdpokkdpkopko
Embed-2 (1).pdfb[k[k[[k[kkkpkdpokkdpkopkobhavenpr
 
Transformative Leadership: N Chandrababu Naidu and TDP's Vision for Innovatio...
Transformative Leadership: N Chandrababu Naidu and TDP's Vision for Innovatio...Transformative Leadership: N Chandrababu Naidu and TDP's Vision for Innovatio...
Transformative Leadership: N Chandrababu Naidu and TDP's Vision for Innovatio...srinuseo15
 
Politician uddhav thackeray biography- Full Details
Politician uddhav thackeray biography- Full DetailsPolitician uddhav thackeray biography- Full Details
Politician uddhav thackeray biography- Full DetailsVoterMood
 
America Is the Target; Israel Is the Front Line _ Andy Blumenthal _ The Blogs...
America Is the Target; Israel Is the Front Line _ Andy Blumenthal _ The Blogs...America Is the Target; Israel Is the Front Line _ Andy Blumenthal _ The Blogs...
America Is the Target; Israel Is the Front Line _ Andy Blumenthal _ The Blogs...Andy (Avraham) Blumenthal
 
422524114-Patriarchy-Kamla-Bhasin gg.pdf
422524114-Patriarchy-Kamla-Bhasin gg.pdf422524114-Patriarchy-Kamla-Bhasin gg.pdf
422524114-Patriarchy-Kamla-Bhasin gg.pdflambardar420420
 
Job-Oriеntеd Courses That Will Boost Your Career in 2024
Job-Oriеntеd Courses That Will Boost Your Career in 2024Job-Oriеntеd Courses That Will Boost Your Career in 2024
Job-Oriеntеd Courses That Will Boost Your Career in 2024Insiger
 
06052024_First India Newspaper Jaipur.pdf
06052024_First India Newspaper Jaipur.pdf06052024_First India Newspaper Jaipur.pdf
06052024_First India Newspaper Jaipur.pdfFIRST INDIA
 
{Qatar{^🚀^(+971558539980**}})Abortion Pills for Sale in Dubai. .abu dhabi, sh...
{Qatar{^🚀^(+971558539980**}})Abortion Pills for Sale in Dubai. .abu dhabi, sh...{Qatar{^🚀^(+971558539980**}})Abortion Pills for Sale in Dubai. .abu dhabi, sh...
{Qatar{^🚀^(+971558539980**}})Abortion Pills for Sale in Dubai. .abu dhabi, sh...hyt3577
 
Gujarat-SEBCs.pdf pfpkoopapriorjfperjreie
Gujarat-SEBCs.pdf pfpkoopapriorjfperjreieGujarat-SEBCs.pdf pfpkoopapriorjfperjreie
Gujarat-SEBCs.pdf pfpkoopapriorjfperjreiebhavenpr
 
05052024_First India Newspaper Jaipur.pdf
05052024_First India Newspaper Jaipur.pdf05052024_First India Newspaper Jaipur.pdf
05052024_First India Newspaper Jaipur.pdfFIRST INDIA
 
China's soft power in 21st century .pptx
China's soft power in 21st century   .pptxChina's soft power in 21st century   .pptx
China's soft power in 21st century .pptxYasinAhmad20
 
*Navigating Electoral Terrain: TDP's Performance under N Chandrababu Naidu's ...
*Navigating Electoral Terrain: TDP's Performance under N Chandrababu Naidu's ...*Navigating Electoral Terrain: TDP's Performance under N Chandrababu Naidu's ...
*Navigating Electoral Terrain: TDP's Performance under N Chandrababu Naidu's ...anjanibaddipudi1
 
The political system of the united kingdom
The political system of the united kingdomThe political system of the united kingdom
The political system of the united kingdomlunadelior
 
04052024_First India Newspaper Jaipur.pdf
04052024_First India Newspaper Jaipur.pdf04052024_First India Newspaper Jaipur.pdf
04052024_First India Newspaper Jaipur.pdfFIRST INDIA
 
declarationleaders_sd_re_greens_theleft_5.pdf
declarationleaders_sd_re_greens_theleft_5.pdfdeclarationleaders_sd_re_greens_theleft_5.pdf
declarationleaders_sd_re_greens_theleft_5.pdfssuser5750e1
 
Group_5_US-China Trade War to understand the trade
Group_5_US-China Trade War to understand the tradeGroup_5_US-China Trade War to understand the trade
Group_5_US-China Trade War to understand the tradeRahatulAshafeen
 
THE OBSTACLES THAT IMPEDE THE DEVELOPMENT OF BRAZIL IN THE CONTEMPORARY ERA A...
THE OBSTACLES THAT IMPEDE THE DEVELOPMENT OF BRAZIL IN THE CONTEMPORARY ERA A...THE OBSTACLES THAT IMPEDE THE DEVELOPMENT OF BRAZIL IN THE CONTEMPORARY ERA A...
THE OBSTACLES THAT IMPEDE THE DEVELOPMENT OF BRAZIL IN THE CONTEMPORARY ERA A...Faga1939
 
KING VISHNU BHAGWANON KA BHAGWAN PARAMATMONKA PARATOMIC PARAMANU KASARVAMANVA...
KING VISHNU BHAGWANON KA BHAGWAN PARAMATMONKA PARATOMIC PARAMANU KASARVAMANVA...KING VISHNU BHAGWANON KA BHAGWAN PARAMATMONKA PARATOMIC PARAMANU KASARVAMANVA...
KING VISHNU BHAGWANON KA BHAGWAN PARAMATMONKA PARATOMIC PARAMANU KASARVAMANVA...IT Industry
 
Embed-4.pdf lkdiinlajeklhndklheduhuekjdh
Embed-4.pdf lkdiinlajeklhndklheduhuekjdhEmbed-4.pdf lkdiinlajeklhndklheduhuekjdh
Embed-4.pdf lkdiinlajeklhndklheduhuekjdhbhavenpr
 

Último (20)

Embed-2 (1).pdfb[k[k[[k[kkkpkdpokkdpkopko
Embed-2 (1).pdfb[k[k[[k[kkkpkdpokkdpkopkoEmbed-2 (1).pdfb[k[k[[k[kkkpkdpokkdpkopko
Embed-2 (1).pdfb[k[k[[k[kkkpkdpokkdpkopko
 
Transformative Leadership: N Chandrababu Naidu and TDP's Vision for Innovatio...
Transformative Leadership: N Chandrababu Naidu and TDP's Vision for Innovatio...Transformative Leadership: N Chandrababu Naidu and TDP's Vision for Innovatio...
Transformative Leadership: N Chandrababu Naidu and TDP's Vision for Innovatio...
 
Politician uddhav thackeray biography- Full Details
Politician uddhav thackeray biography- Full DetailsPolitician uddhav thackeray biography- Full Details
Politician uddhav thackeray biography- Full Details
 
America Is the Target; Israel Is the Front Line _ Andy Blumenthal _ The Blogs...
America Is the Target; Israel Is the Front Line _ Andy Blumenthal _ The Blogs...America Is the Target; Israel Is the Front Line _ Andy Blumenthal _ The Blogs...
America Is the Target; Israel Is the Front Line _ Andy Blumenthal _ The Blogs...
 
422524114-Patriarchy-Kamla-Bhasin gg.pdf
422524114-Patriarchy-Kamla-Bhasin gg.pdf422524114-Patriarchy-Kamla-Bhasin gg.pdf
422524114-Patriarchy-Kamla-Bhasin gg.pdf
 
Job-Oriеntеd Courses That Will Boost Your Career in 2024
Job-Oriеntеd Courses That Will Boost Your Career in 2024Job-Oriеntеd Courses That Will Boost Your Career in 2024
Job-Oriеntеd Courses That Will Boost Your Career in 2024
 
06052024_First India Newspaper Jaipur.pdf
06052024_First India Newspaper Jaipur.pdf06052024_First India Newspaper Jaipur.pdf
06052024_First India Newspaper Jaipur.pdf
 
{Qatar{^🚀^(+971558539980**}})Abortion Pills for Sale in Dubai. .abu dhabi, sh...
{Qatar{^🚀^(+971558539980**}})Abortion Pills for Sale in Dubai. .abu dhabi, sh...{Qatar{^🚀^(+971558539980**}})Abortion Pills for Sale in Dubai. .abu dhabi, sh...
{Qatar{^🚀^(+971558539980**}})Abortion Pills for Sale in Dubai. .abu dhabi, sh...
 
Gujarat-SEBCs.pdf pfpkoopapriorjfperjreie
Gujarat-SEBCs.pdf pfpkoopapriorjfperjreieGujarat-SEBCs.pdf pfpkoopapriorjfperjreie
Gujarat-SEBCs.pdf pfpkoopapriorjfperjreie
 
05052024_First India Newspaper Jaipur.pdf
05052024_First India Newspaper Jaipur.pdf05052024_First India Newspaper Jaipur.pdf
05052024_First India Newspaper Jaipur.pdf
 
China's soft power in 21st century .pptx
China's soft power in 21st century   .pptxChina's soft power in 21st century   .pptx
China's soft power in 21st century .pptx
 
9953056974 Call Girls In Pratap Nagar, Escorts (Delhi) NCR
9953056974 Call Girls In Pratap Nagar, Escorts (Delhi) NCR9953056974 Call Girls In Pratap Nagar, Escorts (Delhi) NCR
9953056974 Call Girls In Pratap Nagar, Escorts (Delhi) NCR
 
*Navigating Electoral Terrain: TDP's Performance under N Chandrababu Naidu's ...
*Navigating Electoral Terrain: TDP's Performance under N Chandrababu Naidu's ...*Navigating Electoral Terrain: TDP's Performance under N Chandrababu Naidu's ...
*Navigating Electoral Terrain: TDP's Performance under N Chandrababu Naidu's ...
 
The political system of the united kingdom
The political system of the united kingdomThe political system of the united kingdom
The political system of the united kingdom
 
04052024_First India Newspaper Jaipur.pdf
04052024_First India Newspaper Jaipur.pdf04052024_First India Newspaper Jaipur.pdf
04052024_First India Newspaper Jaipur.pdf
 
declarationleaders_sd_re_greens_theleft_5.pdf
declarationleaders_sd_re_greens_theleft_5.pdfdeclarationleaders_sd_re_greens_theleft_5.pdf
declarationleaders_sd_re_greens_theleft_5.pdf
 
Group_5_US-China Trade War to understand the trade
Group_5_US-China Trade War to understand the tradeGroup_5_US-China Trade War to understand the trade
Group_5_US-China Trade War to understand the trade
 
THE OBSTACLES THAT IMPEDE THE DEVELOPMENT OF BRAZIL IN THE CONTEMPORARY ERA A...
THE OBSTACLES THAT IMPEDE THE DEVELOPMENT OF BRAZIL IN THE CONTEMPORARY ERA A...THE OBSTACLES THAT IMPEDE THE DEVELOPMENT OF BRAZIL IN THE CONTEMPORARY ERA A...
THE OBSTACLES THAT IMPEDE THE DEVELOPMENT OF BRAZIL IN THE CONTEMPORARY ERA A...
 
KING VISHNU BHAGWANON KA BHAGWAN PARAMATMONKA PARATOMIC PARAMANU KASARVAMANVA...
KING VISHNU BHAGWANON KA BHAGWAN PARAMATMONKA PARATOMIC PARAMANU KASARVAMANVA...KING VISHNU BHAGWANON KA BHAGWAN PARAMATMONKA PARATOMIC PARAMANU KASARVAMANVA...
KING VISHNU BHAGWANON KA BHAGWAN PARAMATMONKA PARATOMIC PARAMANU KASARVAMANVA...
 
Embed-4.pdf lkdiinlajeklhndklheduhuekjdh
Embed-4.pdf lkdiinlajeklhndklheduhuekjdhEmbed-4.pdf lkdiinlajeklhndklheduhuekjdh
Embed-4.pdf lkdiinlajeklhndklheduhuekjdh
 

as

  • 1. CS145 Lecture Notes #10 SQL Programming Example schema: CREATE TABLE Student (SID INTEGER PRIMARY KEY, name CHAR(30), age INTEGER, GPA FLOAT); CREATE TABLE Take (SID INTEGER, CID CHAR(10), PRIMARY KEY(SID, CID)); CREATE TABLE Course (CID CHAR(10) PRIMARY KEY, title VARCHAR(100) UNIQUE); Motivation Pros and cons of SQL: Very high-level, possible to optimize Not tuned to support general-purpose computation Oracle as a calculator? SELECT 142857*3 FROM DUAL; Strictly less expressive than general-purpose languages SQL2 has no recursion and cannot even compute factorial! Solutions: Augment SQL: Oracle’s PL/SQL Use SQL together with a general-purpose programming language: embedded SQL, dynamic SQL Oracle PL/SQL Basics Rough form of a PL/SQL program: DECLARE BEGIN END; . RUN; DECLARE section is optional . and RUN end the program and execute it Jun Yang 1 CS145 Spring 1999
  • 2. Example: go through students 142–857 and set all GPA’s under 4.0 to 4.0 DECLARE thisSID Student.SID%TYPE; thisGPA Student.GPA%TYPE; BEGIN thisSID := 142; LOOP EXIT WHEN (thisSID > 857); SELECT GPA INTO thisGPA FROM Student WHERE SID = thisSID; IF (thisGPA < 4.0) THEN UPDATE Student SET GPA = 4.0 WHERE SID = thisSID; END IF; thisSID := thisSID + 1; END LOOP; END; . RUN; Basic features: Local variable: Use %TYPE to match its type to a column in the schema Use := for assignment; = for comparison Branch: IF (...) THEN ... ELSE ... END IF; Loop: LOOP ... EXIT WHEN (...); ... END LOOP; The usual data modification statements: INSERT, DELETE, UPDATE Single-row SELECT: SELECT ... INTO ... FROM ...; Oracle raises an exception if SELECT returns no rows or more than one row Cursors Inside a PL/SQL program, the result of a SELECT must go somewhere: If SELECT returns one row, it can go INTO a variable What if SELECT returns multiple rows? Cursor: a variable that runs through the result of a SELECT, row by row Declare by: CURSOR IS ; Use inside a cursor loop: Fetch one result row at a time: FETCH INTO ; Break the loop when there are no more rows to return: EXIT WHEN %NOTFOUND; OPEN/CLOSE before/after use Jun Yang 2 CS145 Spring 1999
  • 3. If cursor is over a single table and has no aggregates or DISTINCT, we can also modify data through the cursor: Follow the declaration by FOR UPDATE Use WHERE CURRENT OF in DELETE or UPDATE Example: go through all CS145 students and set all GPA’s under 4.0 to 4.0! Note it is possible to declare a “row” type in Oracle DECLARE thisStudent Student%ROWTYPE; CURSOR CS145Student IS SELECT * FROM Student WHERE SID IN (SELECT SID FROM Take WHERE CID = ’CS145’) FOR UPDATE; BEGIN OPEN CS145Student; LOOP FETCH CS145Student INTO thisStudent; EXIT WHEN (CS145Student%NOTFOUND); IF (thisStudent.GPA < 4.0) THEN UPDATE Student SET GPA = 4.0 WHERE CURRENT OF CS145Student; END IF; END LOOP; CLOSE CS145Student; END; . RUN; Stored Procedures Creating a PL/SQL stored procedure: CREATE PROCEDURE ( ) AS BEGIN END; . RUN; The RUN above creates the procedure, but does not execute it Running the procedure inside a PL/SQL program: BEGIN ... ( ); ... END; . RUN; Jun Yang 3 CS145 Spring 1999
  • 4. Dropping the procedure: DROP PROCEDURE ; Example: a procedure to enroll students in CS145 CREATE PROCEDURE CS145Enroll (thisSID IN Take.SID%TYPE) AS BEGIN INSERT INTO Take VALUES(thisSID, ’CS145’); END; . RUN; Example: students 142 and 857 enroll in CS145 BEGIN CS145Enroll(142); CS145Enroll(857); END; . RUN; Embedded SQL Instead of making SQL do more, embed it into a general-purpose program- ming language (C in our examples): (1) Host program with special SQL directives and commands Goes through a special DBMS preprocessor to produce: (2) Host program with special DBMS function calls Gets compiled and linked with special DBMS library to produce: (3) Executable code that communicates with the DBMS Main issues in embedding SQL within a program: Which statements are SQL? Those introduced with EXEC SQL How are values passed from the program into SQL commands? Shared Variables: variables that are accessible to both SQL and the host language In C, variables are used normally In SQL, they must be preceded by a colon How are results of SQL queries returned into program variables? If query returns a single row, use SELECT INTO If query returns many rows, use a cursor Similar to PL/SQL cursors, with minor syntactic differences In Oracle, WHENEVER can be used to break cursor loops Jun Yang 4 CS145 Spring 1999
  • 5. Example: go through all CS145 students and change all GPA’s EXEC SQL BEGIN DECLARE SECTION; int thisSID; float thisGPA; EXEC SQL END DECLARE SECTION; EXEC SQL DECLARE CS145Student CURSOR FOR SELECT SID, GPA FROM Student WHERE SID IN (SELECT SID FROM Take WHERE CID = ’CS145’) FOR UPDATE; EXEC SQL OPEN CS145Student; EXEC SQL WHENEVER NOT FOUND DO break; while (1) { EXEC SQL FETCH CS145Student INTO :thisSID, :thisGPA; printf("SID %d current GPA %fn", thisSID, thisGPA); printf("Enter new GPA: "); scanf("%f", &thisGPA); EXEC SQL UPDATE Student SET GPA = :thisGPA WHERE CURRENT OF CS145Student; } EXEC SQL CLOSE CS145Student; Dynamic SQL Embedded SQL is fine for fixed applications (e.g., a program used by the registrar to enroll students in classes) But we cannot use it to write a program like sqlplus, because we do not know in advance what SQL statements the user will enter Two special statements in embedded SQL which make it dynamic: PREPARE EXECUTE Example: let’s write our own sqlplus! EXEC SQL BEGIN DECLARE SECTION; char query[MAX_QUERY_LENGTH]; EXEC SQL END DECLARE SECTION; while (1) { /* issue SQL> prompt */ /* read user input into query */ EXEC SQL PREPARE q FROM :query; EXEC SQL EXECUTE q; } Jun Yang 5 CS145 Spring 1999