This document provides an overview of SQL programming and PL/SQL. It discusses pros and cons of SQL, introduces basic PL/SQL features like variables, branches, loops, and data modification statements. It also covers PL/SQL concepts like cursors, stored procedures, embedded SQL, and dynamic SQL. Examples are provided to illustrate key points.
This document provides an agenda and overview for a two-day PL/SQL Fundamentals training course. The agenda covers key PL/SQL concepts like language features, the DECLARE, BEGIN, and EXCEPTION clauses, explicit cursors, nested blocks, and declared subprograms. Students will learn to program with PL/SQL using a sample COMPANY database that will be set up on Oracle Database Express Edition. The course is a prerequisite for more advanced PL/SQL trainings.
Molteplici esempi di arredamenti per mansarde, da appartamenti a singoli locali.
Camere, camerette, zone giorno, cucine, bagni, soluzioni salva spazio.
Armadi su misura, per sfruttare al meglio gli spazi disponibili
This document summarizes new federal regulations affecting the mortgage industry as of 2009. It outlines four key changes: 1) the Home Value Code of Conduct establishes appraiser independence; 2) the Housing & Economic Recovery Act requires earlier disclosures and limits last-minute changes; 3) the Home Ownership and Equity Protection Act covers high-cost loans; 4) the Real Estate Settlement Procedures Act changes disclosures and fees. Lenders must comply with these regulations, which eliminate rush closings and severely limit closing changes.
Digital Media Solutions: The Business Benefits (03 2014)Dario Mulassano
This document discusses how digital media solutions from Hughes can help businesses increase revenues, improve customer experience, engage employees, and maintain brand consistency. Key benefits highlighted include increased sales from targeted messaging, reduced employee turnover through motivational videos, and a more cohesive customer experience across locations. Hughes provides turnkey services including installation, hosting, maintenance and financing to easily launch digital signage networks that are scalable and require no upfront costs.
Esempi di arredamenti di camerette, con soluzioni originali e versatili.
Armadi a ponte, letti a castello e traslabili,letti singoli, armadi lineari ed angolari, in laminato, pratiche, solide e"simpatiche".
This document provides an agenda and overview for a two-day PL/SQL Fundamentals training course. The agenda covers key PL/SQL concepts like language features, the DECLARE, BEGIN, and EXCEPTION clauses, explicit cursors, nested blocks, and declared subprograms. Students will learn to program with PL/SQL using a sample COMPANY database that will be set up on Oracle Database Express Edition. The course is a prerequisite for more advanced PL/SQL trainings.
Molteplici esempi di arredamenti per mansarde, da appartamenti a singoli locali.
Camere, camerette, zone giorno, cucine, bagni, soluzioni salva spazio.
Armadi su misura, per sfruttare al meglio gli spazi disponibili
This document summarizes new federal regulations affecting the mortgage industry as of 2009. It outlines four key changes: 1) the Home Value Code of Conduct establishes appraiser independence; 2) the Housing & Economic Recovery Act requires earlier disclosures and limits last-minute changes; 3) the Home Ownership and Equity Protection Act covers high-cost loans; 4) the Real Estate Settlement Procedures Act changes disclosures and fees. Lenders must comply with these regulations, which eliminate rush closings and severely limit closing changes.
Digital Media Solutions: The Business Benefits (03 2014)Dario Mulassano
This document discusses how digital media solutions from Hughes can help businesses increase revenues, improve customer experience, engage employees, and maintain brand consistency. Key benefits highlighted include increased sales from targeted messaging, reduced employee turnover through motivational videos, and a more cohesive customer experience across locations. Hughes provides turnkey services including installation, hosting, maintenance and financing to easily launch digital signage networks that are scalable and require no upfront costs.
Esempi di arredamenti di camerette, con soluzioni originali e versatili.
Armadi a ponte, letti a castello e traslabili,letti singoli, armadi lineari ed angolari, in laminato, pratiche, solide e"simpatiche".
This document provides an introduction to PL/SQL, including what PL/SQL is, why it is used, its basic structure and components like blocks, variables, and types. It also covers key PL/SQL concepts like conditions, loops, cursors, stored procedures, functions, and triggers. Examples are provided to illustrate how to write and execute basic PL/SQL code blocks, programs with variables, and stored programs that incorporate cursors, exceptions, and other features.
What is PL/SQL
Procedural Language – SQL
An extension to SQL with design features of programming languages (procedural and object oriented)
PL/SQL and Java are both supported as internal host languages within Oracle products.
This document provides an overview of PL/SQL and embedded SQL. It outlines the key components of PL/SQL including blocks, procedures, cursors, functions, and triggers. It also provides examples of how to write PL/SQL code for these different components. Additionally, it demonstrates how to embed SQL statements in a Java program using JDBC. The document concludes by reminding students to use the course website and provides details on the first assignment.
The document discusses functions and stored procedures in SQL. Functions are logical grouped SQL/PL statements that perform a specific task and return a value, while stored procedures perform a specific task without returning a value. The document provides examples of creating simple SQL functions that return values and a stored procedure that updates employee salaries and bonuses.
The document discusses functions and stored procedures in SQL. Functions are logical grouped SQL/PL statements that perform a specific task and return a value, while stored procedures perform a specific task without returning a value. The document provides examples of creating simple SQL functions that return values and a stored procedure that updates employee salaries and bonuses.
Cursors in SQL procedures allow defining a result set that can be iterated through row by row. A cursor acts as a pointer to each row in the result set. To use a cursor, it must be declared to define the result set, opened to establish the result set, rows must be fetched from the cursor one at a time into variables, and the cursor closed once complete. The example demonstrates declaring a cursor for a SELECT statement, fetching rows and summing a value, and closing the cursor to return the result.
Cursors in SQL procedures allow defining a result set that can be iterated through row by row. A cursor acts as a pointer to each row in turn. To use a cursor, it must be declared to define the result set, opened to establish the set, individual rows can then be fetched and processed one at a time using variables, and the cursor is closed once complete. Basic cursor usage involves the DECLARE, OPEN, FETCH and CLOSE statements. An example demonstrates summing the salaries from an employee table by declaring a cursor over it, fetching rows into a variable and accumulating the sum in a loop.
The document provides an overview of PL/SQL and server-side database programming. It discusses:
1) What PL/SQL is and how it extends SQL with programming features like variables, loops, and conditionals to allow for procedural logic on the server-side.
2) The advantages of server-side programming using PL/SQL over client-side programming, including improved performance due to reduced network traffic and ability to reuse code.
3) Key PL/SQL concepts like blocks, data types, scope rules, operators, program flow control structures, exceptions, and subprograms like procedures and functions.
PL/SQL is a procedural language designed specifically to embrace SQL statements within its syntax. PL/SQL program units are compiled by the Oracle Database server and stored inside the database.
The document discusses stored procedures and embedded SQL. Stored procedures allow functions to be stored on the database server and reduce network traffic by being invoked multiple times. They can be written in languages like SQL, C, or Java. Embedded SQL is used to interface SQL with a host programming language by embedding SQL statements directly into program code using syntax like EXEC SQL.
This document discusses dynamic SQL and metadata in Oracle. It describes how to build and execute SQL statements dynamically using native dynamic SQL with EXECUTE IMMEDIATE statements or the DBMS_SQL package. It also explains how to use the DBMS_METADATA package to obtain metadata from the data dictionary as XML or DDL that can be used to re-create database objects. Examples are provided for dynamic SQL DDL, DML, queries and PL/SQL blocks.
PL/SQL is a combination of SQL along with the procedural features of programming languages.
It provides specific syntax for this purpose and supports exactly the same datatypes as SQL.
The document discusses PL/SQL programming concepts like procedures, cursors, and how to build and use them. It provides examples of creating procedures that update employee salaries, using cursors to loop through database records, and controlling cursor flow. The seminar exercise asks students to create a procedure that increases employee salaries by £500 or £1000 depending on the employee's grade level using concepts covered in the document.
The document provides an overview of advanced PL/SQL programming concepts including:
- Decision control structures like IF/THEN, IF/THEN/ELSE, and nested IF statements.
- Using SQL queries within PL/SQL programs.
- Implementing loops using LOOP/EXIT, WHILE, FOR, and cursor FOR loops.
- Retrieving and manipulating database data using implicit and explicit cursors.
- Handling runtime errors through the use of predefined, undefined, and user-defined exceptions.
This document discusses explicit cursors in PL/SQL. It begins by listing the objectives of the lesson, which include distinguishing between implicit and explicit cursors, describing when to use explicit cursors, listing guidelines for declaring and controlling explicit cursors, and demonstrating how to open a cursor, fetch data into variables, loop through multiple rows, and close a cursor. It then explains the purpose of explicit cursors when a SELECT statement may return multiple rows. It discusses context areas and cursors, the limitations of implicit cursors, and shows examples of declaring, opening, fetching from, and closing an explicit cursor.
This document provides an overview of PL/SQL (Procedural Language/SQL) including:
- PL/SQL acts as a host language for stored procedures and triggers and provides middle tier business logic.
- The basic structure of a PL/SQL block includes a DECLARE, BEGIN, EXCEPTION, and END section.
- It discusses PL/SQL variables, data types, control structures like loops and conditional statements.
- Cursors are also covered as a way to iterate through query results.
This document discusses SQL objects and PL/SQL datatypes in Oracle. It provides an overview of PL/SQL as an extension to SQL that allows procedural logic. It describes PL/SQL-specific and SQL object datatypes, including records, collections, and pipelined functions. It also covers SQL collection operators and comparing collections in PL/SQL.
The document discusses PL/SQL, a programming language extension of SQL that adds procedural language features. It defines PL/SQL blocks and their components. It describes advantages like better performance and error handling. It also covers conditional statements, iterative statements, cursors, stored procedures, functions, exception handling, triggers, and embedded SQL in PL/SQL.
This document discusses stored procedures in databases. It provides an overview of stored procedures, their advantages like reduced network traffic, and how they allow local variables, loops, and examination of data tuples. It describes how to write stored procedures using languages like SQL, including declaring parameters, and provides examples of stored procedures using conditional statements, loops, and cursors to process data.
La defensa del expresidente Juan Orlando Hernández, declarado culpable por narcotráfico en EE. UU., solicitó este viernes al juez Kevin Castel que imponga una condena mínima de 40 años de prisión.
La defensa del expresidente Juan Orlando Hernández, declarado culpable por narcotráfico en EE. UU., solicitó este viernes al juez Kevin Castel que imponga una condena mínima de 40 años de prisión.
This document provides an introduction to PL/SQL, including what PL/SQL is, why it is used, its basic structure and components like blocks, variables, and types. It also covers key PL/SQL concepts like conditions, loops, cursors, stored procedures, functions, and triggers. Examples are provided to illustrate how to write and execute basic PL/SQL code blocks, programs with variables, and stored programs that incorporate cursors, exceptions, and other features.
What is PL/SQL
Procedural Language – SQL
An extension to SQL with design features of programming languages (procedural and object oriented)
PL/SQL and Java are both supported as internal host languages within Oracle products.
This document provides an overview of PL/SQL and embedded SQL. It outlines the key components of PL/SQL including blocks, procedures, cursors, functions, and triggers. It also provides examples of how to write PL/SQL code for these different components. Additionally, it demonstrates how to embed SQL statements in a Java program using JDBC. The document concludes by reminding students to use the course website and provides details on the first assignment.
The document discusses functions and stored procedures in SQL. Functions are logical grouped SQL/PL statements that perform a specific task and return a value, while stored procedures perform a specific task without returning a value. The document provides examples of creating simple SQL functions that return values and a stored procedure that updates employee salaries and bonuses.
The document discusses functions and stored procedures in SQL. Functions are logical grouped SQL/PL statements that perform a specific task and return a value, while stored procedures perform a specific task without returning a value. The document provides examples of creating simple SQL functions that return values and a stored procedure that updates employee salaries and bonuses.
Cursors in SQL procedures allow defining a result set that can be iterated through row by row. A cursor acts as a pointer to each row in the result set. To use a cursor, it must be declared to define the result set, opened to establish the result set, rows must be fetched from the cursor one at a time into variables, and the cursor closed once complete. The example demonstrates declaring a cursor for a SELECT statement, fetching rows and summing a value, and closing the cursor to return the result.
Cursors in SQL procedures allow defining a result set that can be iterated through row by row. A cursor acts as a pointer to each row in turn. To use a cursor, it must be declared to define the result set, opened to establish the set, individual rows can then be fetched and processed one at a time using variables, and the cursor is closed once complete. Basic cursor usage involves the DECLARE, OPEN, FETCH and CLOSE statements. An example demonstrates summing the salaries from an employee table by declaring a cursor over it, fetching rows into a variable and accumulating the sum in a loop.
The document provides an overview of PL/SQL and server-side database programming. It discusses:
1) What PL/SQL is and how it extends SQL with programming features like variables, loops, and conditionals to allow for procedural logic on the server-side.
2) The advantages of server-side programming using PL/SQL over client-side programming, including improved performance due to reduced network traffic and ability to reuse code.
3) Key PL/SQL concepts like blocks, data types, scope rules, operators, program flow control structures, exceptions, and subprograms like procedures and functions.
PL/SQL is a procedural language designed specifically to embrace SQL statements within its syntax. PL/SQL program units are compiled by the Oracle Database server and stored inside the database.
The document discusses stored procedures and embedded SQL. Stored procedures allow functions to be stored on the database server and reduce network traffic by being invoked multiple times. They can be written in languages like SQL, C, or Java. Embedded SQL is used to interface SQL with a host programming language by embedding SQL statements directly into program code using syntax like EXEC SQL.
This document discusses dynamic SQL and metadata in Oracle. It describes how to build and execute SQL statements dynamically using native dynamic SQL with EXECUTE IMMEDIATE statements or the DBMS_SQL package. It also explains how to use the DBMS_METADATA package to obtain metadata from the data dictionary as XML or DDL that can be used to re-create database objects. Examples are provided for dynamic SQL DDL, DML, queries and PL/SQL blocks.
PL/SQL is a combination of SQL along with the procedural features of programming languages.
It provides specific syntax for this purpose and supports exactly the same datatypes as SQL.
The document discusses PL/SQL programming concepts like procedures, cursors, and how to build and use them. It provides examples of creating procedures that update employee salaries, using cursors to loop through database records, and controlling cursor flow. The seminar exercise asks students to create a procedure that increases employee salaries by £500 or £1000 depending on the employee's grade level using concepts covered in the document.
The document provides an overview of advanced PL/SQL programming concepts including:
- Decision control structures like IF/THEN, IF/THEN/ELSE, and nested IF statements.
- Using SQL queries within PL/SQL programs.
- Implementing loops using LOOP/EXIT, WHILE, FOR, and cursor FOR loops.
- Retrieving and manipulating database data using implicit and explicit cursors.
- Handling runtime errors through the use of predefined, undefined, and user-defined exceptions.
This document discusses explicit cursors in PL/SQL. It begins by listing the objectives of the lesson, which include distinguishing between implicit and explicit cursors, describing when to use explicit cursors, listing guidelines for declaring and controlling explicit cursors, and demonstrating how to open a cursor, fetch data into variables, loop through multiple rows, and close a cursor. It then explains the purpose of explicit cursors when a SELECT statement may return multiple rows. It discusses context areas and cursors, the limitations of implicit cursors, and shows examples of declaring, opening, fetching from, and closing an explicit cursor.
This document provides an overview of PL/SQL (Procedural Language/SQL) including:
- PL/SQL acts as a host language for stored procedures and triggers and provides middle tier business logic.
- The basic structure of a PL/SQL block includes a DECLARE, BEGIN, EXCEPTION, and END section.
- It discusses PL/SQL variables, data types, control structures like loops and conditional statements.
- Cursors are also covered as a way to iterate through query results.
This document discusses SQL objects and PL/SQL datatypes in Oracle. It provides an overview of PL/SQL as an extension to SQL that allows procedural logic. It describes PL/SQL-specific and SQL object datatypes, including records, collections, and pipelined functions. It also covers SQL collection operators and comparing collections in PL/SQL.
The document discusses PL/SQL, a programming language extension of SQL that adds procedural language features. It defines PL/SQL blocks and their components. It describes advantages like better performance and error handling. It also covers conditional statements, iterative statements, cursors, stored procedures, functions, exception handling, triggers, and embedded SQL in PL/SQL.
This document discusses stored procedures in databases. It provides an overview of stored procedures, their advantages like reduced network traffic, and how they allow local variables, loops, and examination of data tuples. It describes how to write stored procedures using languages like SQL, including declaring parameters, and provides examples of stored procedures using conditional statements, loops, and cursors to process data.
La defensa del expresidente Juan Orlando Hernández, declarado culpable por narcotráfico en EE. UU., solicitó este viernes al juez Kevin Castel que imponga una condena mínima de 40 años de prisión.
La defensa del expresidente Juan Orlando Hernández, declarado culpable por narcotráfico en EE. UU., solicitó este viernes al juez Kevin Castel que imponga una condena mínima de 40 años de prisión.
22062024_First India Newspaper Jaipur.pdfFIRST INDIA
Find Latest India News and Breaking News these days from India on Politics, Business, Entertainment, Technology, Sports, Lifestyle and Coronavirus News in India and the world over that you can't miss. For real time update Visit our social media handle. Read First India NewsPaper in your morning replace. Visit First India.
CLICK:- https://firstindia.co.in/
#First_India_NewsPaper
विवादास्पद फिल्म के ट्रेलर से गाली-गलौज वाले दृश्य हटा दिए गए हैं, और जुर्माना लगाया गया है। सुप्रीम कोर्ट और बॉम्बे हाई कोर्ट दोनों ने फिल्म की रिलीज पर रोक लगा दी है और उसे निलंबित कर दिया है। पहले यह फिल्म 7 जून और फिर 14 जून को रिलीज होने वाली थी, लेकिन अब यह 21 जून को रिलीज हो रही है।
projet de traité négocié à Istanbul (anglais).pdfEdouardHusson
Ceci est le projet de traité qui avait été négocié entre Russes et Ukrainiens à Istanbul en mars 2022, avant que les Etats-Unis et la Grande-Bretagne ne détournent Kiev de signer.
मद्रास उच्च न्यायालय के सेवानिवृत्त न्यायाधीश और केंद्र और राज्य सरकार के नौकरशाहों सहित आठ अन्य लोगों की अध्यक्षता वाली एक उच्च स्तरीय समिति ने 2021 में NEET परीक्षा को खत्म करने की सिफारिश की थी। महत्वपूर्ण बात यह है कि रिपोर्ट में 2010-11 में ग्रामीण पृष्ठभूमि से तमिल छात्रों की संख्या में 61.5% की भारी गिरावट को दर्शाया गया है। इसके बजाय मेट्रो छात्रों में वृद्धि दर्ज की गई है।
19 जून को बॉम्बे हाई कोर्ट ने विवादित फिल्म ‘हमारे बारह’ को 21 जून को थिएटर में रिलीज करने का रास्ता साफ कर दिया, हालांकि यह सुनिश्चित करने के बाद कि फिल्म निर्माता कुछ आपत्तिजनक अंशों को हटा दें।
Apna Punjab Media is a Punjabi newspaper that covers local and global news, cultural updates, and community events. It's a trusted source for Punjabi-speaking communities, offering a mix of traditional values and modern insights into Punjab's vibrant life and heritage.
17062024_First India Newspaper Jaipur.pdfFIRST INDIA
Find Latest India News and Breaking News these days from India on Politics, Business, Entertainment, Technology, Sports, Lifestyle and Coronavirus News in India and the world over that you can't miss. For real time update Visit our social media handle. Read First India NewsPaper in your morning replace. Visit First India.
CLICK:- https://firstindia.co.in/
#First_India_NewsPaper
16062024_First India Newspaper Jaipur.pdfFIRST INDIA
Find Latest India News and Breaking News these days from India on Politics, Business, Entertainment, Technology, Sports, Lifestyle and Coronavirus News in India and the world over that you can't miss. For real time update Visit our social media handle. Read First India NewsPaper in your morning replace. Visit First India.
CLICK:- https://firstindia.co.in/
#First_India_NewsPaper
यूजीसी-नेट और NEET परीक्षा (कई अन्य के अलावा, 2018 तक सीबीएसई द्वारा आयोजित की जाती थी, जो भारत में सार्वजनिक और निजी स्कूलों के लिए एक राष्ट्रीय शिक्षा बोर्ड था (और है), जिसे भारत सरकार द्वारा नियंत्रित और प्रबंधित किया जाता था।
Federal Authorities Urge Vigilance Amid Bird Flu Outbreak | The Lifesciences ...The Lifesciences Magazine
Federal authorities have advised the public to remain vigilant but calm in response to the ongoing bird flu outbreak of highly pathogenic avian influenza, commonly known as bird flu.
20062024_First India Newspaper Jaipur.pdfFIRST INDIA
Find Latest India News and Breaking News these days from India on Politics, Business, Entertainment, Technology, Sports, Lifestyle and Coronavirus News in India and the world over that you can't miss. For real time update Visit our social media handle. Read First India NewsPaper in your morning replace. Visit First India.
CLICK:- https://firstindia.co.in/
#First_India_NewsPaper
Why We Chose ScyllaDB over DynamoDB for "User Watch Status"ScyllaDB
Yichen Wei and Adam Drennan share the architecture and technical requirements behind "user watch status" for a major global media streaming service, what that meant for their database, the pros and cons of the many options they considered for replacing DynamoDB, why they ultimately chose ScyllaDB, and their lessons learned so far.
19 जून को बॉम्बे हाई कोर्ट ने विवादित फिल्म ‘हमारे बारह’ को 21 जून को थिएटर में रिलीज करने का रास्ता साफ कर दिया, हालांकि यह सुनिश्चित करने के बाद कि फिल्म निर्माता कुछ आपत्तिजनक अंशों को हटा दें।
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