SlideShare a Scribd company logo
1 of 23
PL / SQL 
By, 
C.Janani, AP / IT
Definition 
• PL/SQL stands for Procedural Language extension of 
SQL. 
• Procedural features of programming languages. 
• Developed by Oracle Corporation in the early 90’s to 
enhance the capabilities of SQL. 
• PL/SQL Block consists of three sections: 
 The Declaration section (optional). 
 The Execution section (mandatory). 
 The Exception (or Error) Handling section (optional). 
DECLARE 
Variable declaration 
BEGIN 
Program Execution 
EXCEPTION 
Exception handling 
END;
Advantages of PL/SQL 
• Block Structures 
• Procedural language Capability 
• Better Performance 
• Error Handling
Conditional Statements in PL/SQL 
IF THEN ELSE STATEMENT 
1) IF condition 
THEN 
statement 1; 
ELSE 
statement 2; 
END IF; 
2) IF condition 1 
THEN 
statement 1; statement 2; 
ELSIF condtion2 
THEN 
statement 3; 
ELSE 
statement 4; 
END IF 
3) IF condition 1 
THEN 
statement 1; 
statement 2; 
ELSIF condtion2 
THEN 
statement 3; 
ELSE statement 4; 
END IF; 
4) IF condition1 
THEN 
ELSE IF condition2 THEN statement1; 
END IF; 
ELSIF condition3 
THEN 
statement2; 
END IF;
Iterative Statements in PL/SQL 
• Simple Loop 
• While Loop 
• For Loop 
LOOP statements; EXIT; 
{or EXIT WHEN 
condition;} 
END LOOP; 
WHILE <condition> 
LOOP statements; END 
LOOP; 
FOR counter IN 
val1..val2 LOOP 
statements; END LOOP;
Cursors 
• A temporary work area created in the system memory 
when a SQL statement is executed. 
• Contains information on a select statement and the 
rows of data accessed by it. 
• Can open a cursor, and repeatedly fetch a tuple then 
move the cursor, until all tuples have been retrieved. 
– Can use the ORDER BY clause, in queries that are accessed 
through a cursor, to control the order in which tuples are 
returned. 
• Fields in ORDER BY clause must also appear in SELECT clause. 
• Can also modify/delete tuple pointed to by a cursor.
Output:
Cursor Types: 
• Implicit cursors 
These are created by default when DML statements like, 
INSERT, UPDATE, and DELETE statements are executed. They are 
also created when a SELECT statement that returns just one row is 
executed. 
• Explicit cursors 
They must be created when you are executing a SELECT 
statement that returns more than one row. Even though the cursor 
stores multiple records, only one record can be processed at a time, 
which is called as current row. When you fetch a row the current 
row position moves to next row. 
• Both implicit and explicit cursors have the same functionality, but 
they differ in the way they are accessed.
Stored Procedures 
• A stored procedure or in simple a proc is a named PL/SQL block 
which performs one or more specific task. This is similar to a 
procedure in other programming languages. 
• Has a header and a body. 
• Header consists of the name of the procedure and the parameters 
or variables passed to the procedure. 
• The body consists or declaration section, execution section and 
exception section similar to a general PL/SQL Block. 
• A procedure is similar to an anonymous PL/SQL Block but it is 
named for repeated usage. 
• We can pass parameters to procedures in three ways. 
1) IN-parameters 
2) OUT-parameters 
3) IN OUT-parameters 
• A procedure may or may not return any value.
Syntax: 
• CREATE [OR REPLACE] PROCEDURE proc_name 
[list of parameters] 
IS 
Declaration section 
BEGIN 
Execution section 
EXCEPTION 
Exception section 
END;
PL/SQL Functions 
• The major difference between a procedure and a function is, a function must 
always return a value, but a procedure may or may not return a value. 
• General Syntax to create a function is 
CREATE [OR REPLACE] FUNCTION function_name [parameters] 
RETURN return_datatype; 
IS 
Declaration_section 
BEGIN 
Execution_section 
Return return_variable; 
EXCEPTION 
exception section 
Return 
return_variable; 
END;
Parameters in Procedure and Functions 
1) IN type parameter: These types of parameters 
are used to send values to stored procedures. 
CREATE [OR REPLACE] PROCEDURE procedure_name ( param_name1 IN 
datatype, param_name12 IN datatype ... ) 
2) OUT type parameter: These types of 
parameters are used to get values from stored 
procedures. This is similar to a return type in 
functions. 
CREATE [OR REPLACE] PROCEDURE proc2 (param_name OUT datatype) 
3) IN OUT parameter: These types of parameters 
are used to send values and get values from 
stored procedures. 
CREATE [OR REPLACE] PROCEDURE proc3 (param_name IN OUT datatype)
Exception Handling 
• Using Exception Handling we can test the 
code and avoid it from exiting abruptly. 
• When an exception occurs a messages which 
explains its cause is recieved. 
• PL/SQL Exception message consists of three 
parts. 
1) Type of Exception 
2) An Error Code 
3) A message
Structure of Exception Handling 
DECLARE 
Declaration section 
BEGIN 
Exception section 
EXCEPTION 
WHEN ex_name1 THEN 
-Error handling statements 
WHEN ex_name2 THEN 
-Error handling statements 
WHEN Others THEN 
-Error handling statements 
END;
Types of Exception 
a) Named System Exceptions 
b) Unnamed System Exceptions 
c) User-defined Exceptions 
Named System Exceptions
Trigger 
• Trigger is a pl/sql block structure which is fired 
when a DML statements like Insert, Delete, 
Update is executed on a database table. 
• A trigger is triggered automatically when an 
associated DML statement is executed.
Syntax of Triggers 
CREATE [OR REPLACE ] TRIGGER trigger_name {BEFORE | 
AFTER | INSTEAD OF } 
{INSERT [OR] | UPDATE [OR] | DELETE} 
[OF col_name] 
ON table_name 
[REFERENCING OLD AS o NEW AS n] 
[FOR EACH ROW] 
WHEN (condition) 
BEGIN 
--- sql statements 
END;
Embedded SQL & Dynamic SQL
Embedded SQL Example 
#include<stdio.h> 
#include<string.h> 
EXEC SQL BEGIN DECLARE SECTION; 
long station_id; 
long mon; 
float temp; 
float rain; 
char city_name[21]; 
long SQLCODE; 
EXEC SQL END DECLARE SECTION; 
main() 
{ 
/* the CONNECT statement, if needed, goes here */ 
strcpy(city_name,"Denver"); 
EXEC SQL SELECT ID INTO :station_id 
FROM STATION 
WHERE CITY = :city_name;
if (SQLCODE == 100) 
{ 
printf("There is no station for city %sn",city_name); 
exit(0); 
} 
printf("For the city %s, Station ID is %ldn",city_name,station_id); 
printf("And here is the weather data:n"); 
EXEC SQL DECLARE XYZ CURSOR FOR 
SELECT MONTH, TEMP_F, RAIN_I FROM STATS 
WHERE ID = :station_id 
ORDER BY MONTH; 
EXEC SQL OPEN XYZ; 
while (SQLCODE != 100) { 
EXEC SQL FETCH XYZ INTO :mon, :temp, :rain; 
if (SQLCODE == 100) 
printf("end of listn"); 
else 
printf("month = %ld, temperature = %f, rainfall = %fn",mon,temp,rain); 
} 
EXEC SQL CLOSE XYZ; 
exit(0); 
}
• Execution log: 
For the city Denver, Station ID is 44 
And here is the weather data: 
month = 1, temperature = 27.299999, rainfall = 0.180000 
month = 7, temperature = 74.800003, rainfall = 2.110000 
end of list

More Related Content

What's hot

3963066 pl-sql-notes-only
3963066 pl-sql-notes-only3963066 pl-sql-notes-only
3963066 pl-sql-notes-only
Ashwin Kumar
 
10g plsql slide
10g plsql slide10g plsql slide
10g plsql slide
Tanu_Manu
 

What's hot (20)

Procedure and Functions in pl/sql
Procedure and Functions in pl/sqlProcedure and Functions in pl/sql
Procedure and Functions in pl/sql
 
PL/SQL - CURSORS
PL/SQL - CURSORSPL/SQL - CURSORS
PL/SQL - CURSORS
 
Oracle: Procedures
Oracle: ProceduresOracle: Procedures
Oracle: Procedures
 
Cursors
CursorsCursors
Cursors
 
4. plsql
4. plsql4. plsql
4. plsql
 
Cursors
CursorsCursors
Cursors
 
Oracle: Control Structures
Oracle: Control StructuresOracle: Control Structures
Oracle: Control Structures
 
Programming in Oracle with PL/SQL
Programming in Oracle with PL/SQLProgramming in Oracle with PL/SQL
Programming in Oracle with PL/SQL
 
Packages in PL/SQL
Packages in PL/SQLPackages in PL/SQL
Packages in PL/SQL
 
Pl sql guide
Pl sql guidePl sql guide
Pl sql guide
 
PL-SQL DIFFERENT PROGRAMS
PL-SQL DIFFERENT PROGRAMSPL-SQL DIFFERENT PROGRAMS
PL-SQL DIFFERENT PROGRAMS
 
Cursors
CursorsCursors
Cursors
 
3963066 pl-sql-notes-only
3963066 pl-sql-notes-only3963066 pl-sql-notes-only
3963066 pl-sql-notes-only
 
pl/sql Procedure
pl/sql Procedurepl/sql Procedure
pl/sql Procedure
 
Oracle: PLSQL Introduction
Oracle: PLSQL IntroductionOracle: PLSQL Introduction
Oracle: PLSQL Introduction
 
PLSQL CURSOR
PLSQL CURSORPLSQL CURSOR
PLSQL CURSOR
 
10g plsql slide
10g plsql slide10g plsql slide
10g plsql slide
 
Packages - PL/SQL
Packages - PL/SQLPackages - PL/SQL
Packages - PL/SQL
 
Introduction to PL/SQL
Introduction to PL/SQLIntroduction to PL/SQL
Introduction to PL/SQL
 
PLSQL
PLSQLPLSQL
PLSQL
 

Similar to SQL / PL

plsql tutorialhub....
plsql tutorialhub....plsql tutorialhub....
plsql tutorialhub....
Abhiram Vijay
 
My cool new Slideshow!
My cool new Slideshow!My cool new Slideshow!
My cool new Slideshow!
Anas Mohammed
 

Similar to SQL / PL (20)

Dynamic websites lec3
Dynamic websites lec3Dynamic websites lec3
Dynamic websites lec3
 
PLSQL.pptx
PLSQL.pptxPLSQL.pptx
PLSQL.pptx
 
plsql tutorialhub....
plsql tutorialhub....plsql tutorialhub....
plsql tutorialhub....
 
plsql.ppt
plsql.pptplsql.ppt
plsql.ppt
 
Oracle PL/SQL online training | PL/SQL online Training
Oracle PL/SQL online training | PL/SQL online TrainingOracle PL/SQL online training | PL/SQL online Training
Oracle PL/SQL online training | PL/SQL online Training
 
4 cursors
4 cursors4 cursors
4 cursors
 
Pl sql
Pl sqlPl sql
Pl sql
 
PL_SQL - II.pptx
PL_SQL - II.pptxPL_SQL - II.pptx
PL_SQL - II.pptx
 
Oracle: Cursors
Oracle: CursorsOracle: Cursors
Oracle: Cursors
 
Oracle:Cursors
Oracle:CursorsOracle:Cursors
Oracle:Cursors
 
Rdbms chapter 1 function
Rdbms chapter 1 functionRdbms chapter 1 function
Rdbms chapter 1 function
 
My cool new Slideshow!
My cool new Slideshow!My cool new Slideshow!
My cool new Slideshow!
 
Cursors, triggers, procedures
Cursors, triggers, proceduresCursors, triggers, procedures
Cursors, triggers, procedures
 
SQL Procedures & Functions
SQL Procedures & FunctionsSQL Procedures & Functions
SQL Procedures & Functions
 
Triggers n Cursors.ppt
Triggers n Cursors.pptTriggers n Cursors.ppt
Triggers n Cursors.ppt
 
Plsql guide 2
Plsql guide 2Plsql guide 2
Plsql guide 2
 
pl_sql.ppt
pl_sql.pptpl_sql.ppt
pl_sql.ppt
 
PLSQL (1).ppt
PLSQL (1).pptPLSQL (1).ppt
PLSQL (1).ppt
 
Exceptions Triggers function in SQL by Vasant Bhabad
Exceptions Triggers function in SQL by Vasant BhabadExceptions Triggers function in SQL by Vasant Bhabad
Exceptions Triggers function in SQL by Vasant Bhabad
 
Pl sql-ch1
Pl sql-ch1Pl sql-ch1
Pl sql-ch1
 

Recently uploaded

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 

Recently uploaded (20)

Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Magic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptxMagic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptx
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 

SQL / PL

  • 1. PL / SQL By, C.Janani, AP / IT
  • 2. Definition • PL/SQL stands for Procedural Language extension of SQL. • Procedural features of programming languages. • Developed by Oracle Corporation in the early 90’s to enhance the capabilities of SQL. • PL/SQL Block consists of three sections:  The Declaration section (optional).  The Execution section (mandatory).  The Exception (or Error) Handling section (optional). DECLARE Variable declaration BEGIN Program Execution EXCEPTION Exception handling END;
  • 3. Advantages of PL/SQL • Block Structures • Procedural language Capability • Better Performance • Error Handling
  • 4. Conditional Statements in PL/SQL IF THEN ELSE STATEMENT 1) IF condition THEN statement 1; ELSE statement 2; END IF; 2) IF condition 1 THEN statement 1; statement 2; ELSIF condtion2 THEN statement 3; ELSE statement 4; END IF 3) IF condition 1 THEN statement 1; statement 2; ELSIF condtion2 THEN statement 3; ELSE statement 4; END IF; 4) IF condition1 THEN ELSE IF condition2 THEN statement1; END IF; ELSIF condition3 THEN statement2; END IF;
  • 5. Iterative Statements in PL/SQL • Simple Loop • While Loop • For Loop LOOP statements; EXIT; {or EXIT WHEN condition;} END LOOP; WHILE <condition> LOOP statements; END LOOP; FOR counter IN val1..val2 LOOP statements; END LOOP;
  • 6. Cursors • A temporary work area created in the system memory when a SQL statement is executed. • Contains information on a select statement and the rows of data accessed by it. • Can open a cursor, and repeatedly fetch a tuple then move the cursor, until all tuples have been retrieved. – Can use the ORDER BY clause, in queries that are accessed through a cursor, to control the order in which tuples are returned. • Fields in ORDER BY clause must also appear in SELECT clause. • Can also modify/delete tuple pointed to by a cursor.
  • 7.
  • 8.
  • 10. Cursor Types: • Implicit cursors These are created by default when DML statements like, INSERT, UPDATE, and DELETE statements are executed. They are also created when a SELECT statement that returns just one row is executed. • Explicit cursors They must be created when you are executing a SELECT statement that returns more than one row. Even though the cursor stores multiple records, only one record can be processed at a time, which is called as current row. When you fetch a row the current row position moves to next row. • Both implicit and explicit cursors have the same functionality, but they differ in the way they are accessed.
  • 11. Stored Procedures • A stored procedure or in simple a proc is a named PL/SQL block which performs one or more specific task. This is similar to a procedure in other programming languages. • Has a header and a body. • Header consists of the name of the procedure and the parameters or variables passed to the procedure. • The body consists or declaration section, execution section and exception section similar to a general PL/SQL Block. • A procedure is similar to an anonymous PL/SQL Block but it is named for repeated usage. • We can pass parameters to procedures in three ways. 1) IN-parameters 2) OUT-parameters 3) IN OUT-parameters • A procedure may or may not return any value.
  • 12. Syntax: • CREATE [OR REPLACE] PROCEDURE proc_name [list of parameters] IS Declaration section BEGIN Execution section EXCEPTION Exception section END;
  • 13. PL/SQL Functions • The major difference between a procedure and a function is, a function must always return a value, but a procedure may or may not return a value. • General Syntax to create a function is CREATE [OR REPLACE] FUNCTION function_name [parameters] RETURN return_datatype; IS Declaration_section BEGIN Execution_section Return return_variable; EXCEPTION exception section Return return_variable; END;
  • 14. Parameters in Procedure and Functions 1) IN type parameter: These types of parameters are used to send values to stored procedures. CREATE [OR REPLACE] PROCEDURE procedure_name ( param_name1 IN datatype, param_name12 IN datatype ... ) 2) OUT type parameter: These types of parameters are used to get values from stored procedures. This is similar to a return type in functions. CREATE [OR REPLACE] PROCEDURE proc2 (param_name OUT datatype) 3) IN OUT parameter: These types of parameters are used to send values and get values from stored procedures. CREATE [OR REPLACE] PROCEDURE proc3 (param_name IN OUT datatype)
  • 15. Exception Handling • Using Exception Handling we can test the code and avoid it from exiting abruptly. • When an exception occurs a messages which explains its cause is recieved. • PL/SQL Exception message consists of three parts. 1) Type of Exception 2) An Error Code 3) A message
  • 16. Structure of Exception Handling DECLARE Declaration section BEGIN Exception section EXCEPTION WHEN ex_name1 THEN -Error handling statements WHEN ex_name2 THEN -Error handling statements WHEN Others THEN -Error handling statements END;
  • 17. Types of Exception a) Named System Exceptions b) Unnamed System Exceptions c) User-defined Exceptions Named System Exceptions
  • 18. Trigger • Trigger is a pl/sql block structure which is fired when a DML statements like Insert, Delete, Update is executed on a database table. • A trigger is triggered automatically when an associated DML statement is executed.
  • 19. Syntax of Triggers CREATE [OR REPLACE ] TRIGGER trigger_name {BEFORE | AFTER | INSTEAD OF } {INSERT [OR] | UPDATE [OR] | DELETE} [OF col_name] ON table_name [REFERENCING OLD AS o NEW AS n] [FOR EACH ROW] WHEN (condition) BEGIN --- sql statements END;
  • 20. Embedded SQL & Dynamic SQL
  • 21. Embedded SQL Example #include<stdio.h> #include<string.h> EXEC SQL BEGIN DECLARE SECTION; long station_id; long mon; float temp; float rain; char city_name[21]; long SQLCODE; EXEC SQL END DECLARE SECTION; main() { /* the CONNECT statement, if needed, goes here */ strcpy(city_name,"Denver"); EXEC SQL SELECT ID INTO :station_id FROM STATION WHERE CITY = :city_name;
  • 22. if (SQLCODE == 100) { printf("There is no station for city %sn",city_name); exit(0); } printf("For the city %s, Station ID is %ldn",city_name,station_id); printf("And here is the weather data:n"); EXEC SQL DECLARE XYZ CURSOR FOR SELECT MONTH, TEMP_F, RAIN_I FROM STATS WHERE ID = :station_id ORDER BY MONTH; EXEC SQL OPEN XYZ; while (SQLCODE != 100) { EXEC SQL FETCH XYZ INTO :mon, :temp, :rain; if (SQLCODE == 100) printf("end of listn"); else printf("month = %ld, temperature = %f, rainfall = %fn",mon,temp,rain); } EXEC SQL CLOSE XYZ; exit(0); }
  • 23. • Execution log: For the city Denver, Station ID is 44 And here is the weather data: month = 1, temperature = 27.299999, rainfall = 0.180000 month = 7, temperature = 74.800003, rainfall = 2.110000 end of list