SlideShare a Scribd company logo
1 of 14
Department of Computer Science and Engineering
Name of the Faculty : Ms. Aruna T N
Subject Name & Code : CS3492/ Database Management Systems
Branch & Department : Computer Science and Engineering
Year & Semester : II / IV
CS3492/DBMS/IICSE/IVSEM/KG-KiTE
KGiSL Institute of Technology
(Approved by AICTE, New Delhi; Affiliated to Anna University, Chennai)
Recognized by UGC, Accredited by NBA (IT)
365, KGiSL Campus, Thudiyalur Road, Saravanampatti, Coimbatore – 641035.
Course Outcome
CO 1 Demonstrate fundamentals of Data models and Relational
databases - K3 LEVEL
CS3492/DBMS/IICSE/IVSEM/KG-KiTE
Syllabus
UNIT I - RELATIONAL DATABASES
Purpose of Database System – Views of data – Data Models –
Database System Architecture – Introduction to relational databases
– Relational Model – Keys – Relational Algebra – SQL
fundamentals – Advanced SQL features – Embedded SQL–
Dynamic SQL
CS3492/DBMS/IICSE/IVSEM/KG-KiTE
TOPIC
Embedded SQL
CS3492/DBMS/IICSE/IVSEM/KG-KiTE
Embedded SQL
• The Programming module in which the SQL statements are embedded is
called Embedded SQL module.
• It is possible to embed SQL statements inside a variety of programming
languages such as C, C++, Java, Fortran, and PL/1,
• A language to which SQL queries are embedded is referred to as a host
language.
• EXEC SQL statement is used in the host language to identify embedded SQL
request to the preprocessor
EXEC SQL <embedded SQL statement >;
CS3492/DBMS/IICSE/IVSEM/KG-KiTE
Embedded SQL(Cont.)
• Before executing any SQL statements, the program must first connect
to the database. This is done using:
EXEC-SQL connect to server user user-name using password;
Here, server identifies the server to which a connection is to be
established.
• Variables of the host language can be used within embedded SQL
statements. They are preceded by a colon (:) to distinguish from SQL
variables (e.g., :credit_amount )
• In some languages, like COBOL, the semicolon is replaced with END-
EXEC
• In Java embedding uses # SQL { …. };
CS3492/DBMS/IICSE/IVSEM/KG-KiTE
Embedded SQL(Cont.)
• Variables used must be declared within DECLARE section, as illustrated below.
The syntax for declaring the variables, however, follows the usual host language
syntax.
EXEC-SQL BEGIN DECLARE SECTION;
int credit-amount ;
EXEC-SQL END DECLARE SECTION;
CS3492/DBMS/IICSE/IVSEM/KG-KiTE
Embedded SQL(Cont.)
• To write an embedded SQL query, we use the
declare c cursor for <SQL query>
statement. The variable c is used to identify the query
• Example:
From within a host language, find the ID and name of students who
have completed more than the number of credits stored in variable
credit_amount in the host langue.
CS3492/DBMS/IICSE/IVSEM/KG-KiTE
Embedded SQL(Cont.)
CS3492/DBMS/IICSE/IVSEM/KG-KiTE
• Specify the query in SQL as follows:
EXEC SQL
declare c cursor for
select ID, name
from student
where tot_cred > :credit_amount
END_EXEC
Embedded SQL(Cont.)
• The open statement for our example is as follows:
EXEC SQL open c ;
• This statement causes the database system to execute the query and to save the
results within a temporary relation. The query uses the value of the host-
language variable credit-amount at the time the open statement is executed.
• The fetch statement causes the values of one tuple in the query result to be
placed on host language variables.
EXEC SQL fetch c into :si, :sn END_EXEC
CS3492/DBMS/IICSE/IVSEM/KG-KiTE
Embedded SQL(Cont.)
CS3492/DBMS/IICSE/IVSEM/KG-KiTE
• A variable called SQLSTATE in the SQL communication area (SQLCA)
gets set to '02000' to indicate no more data is available
• The close statement causes the database system to delete the temporary
relation that holds the result of the query.
EXEC SQL close c ;
Note: above details vary with language. For example, the Java
embedding defines Java iterators to step through result tuples.
Updates through Embedded SQL
• Embedded SQL expressions for database modification (update, insert, and delete)
• Can update tuples fetched by cursor by declaring that the cursor is for update
EXEC SQL
declare c cursor for
select *
from instructor
where dept_name = 'Music'
for update
CS3492/DBMS/IICSE/IVSEM/KG-KiTE
Updates through Embedded SQL
• We then iterate through the tuples by performing fetch operations on the cursor
(as illustrated earlier), and after fetching each tuple we execute the following
code:
update instructor
set salary = salary + 1000
where current of c
Dynamic SQL
• The dynamic SQL component of SQL allows programs to construct and submit
SQL queries at run time.
• In contrast, embedded SQL statements must be completely present at compile
time, they are compiled by the embedded SQL preprocessor.
• Using dynamic SQL programs can create SQL queries as strings at runtime and
can either have them executed immediately or have them prepared for sub sequent
use.

More Related Content

Similar to LM7_ Embedded Sql and Dynamic SQL in dbms

CC105-WEEK10-11-INTRODUCTION-TO-SQL.pdf
CC105-WEEK10-11-INTRODUCTION-TO-SQL.pdfCC105-WEEK10-11-INTRODUCTION-TO-SQL.pdf
CC105-WEEK10-11-INTRODUCTION-TO-SQL.pdf
ozaixyzo
 
Introduction to Threading in .Net
Introduction to Threading in .NetIntroduction to Threading in .Net
Introduction to Threading in .Net
webhostingguy
 

Similar to LM7_ Embedded Sql and Dynamic SQL in dbms (20)

Introduction to SQL, SQL*Plus
Introduction to SQL, SQL*PlusIntroduction to SQL, SQL*Plus
Introduction to SQL, SQL*Plus
 
Cursores.ppt
Cursores.pptCursores.ppt
Cursores.ppt
 
Cassandra Basics, Counters and Time Series Modeling
Cassandra Basics, Counters and Time Series ModelingCassandra Basics, Counters and Time Series Modeling
Cassandra Basics, Counters and Time Series Modeling
 
2° Ciclo Microsoft CRUI 3° Sessione: l'evoluzione delle piattaforme tecnologi...
2° Ciclo Microsoft CRUI 3° Sessione: l'evoluzione delle piattaforme tecnologi...2° Ciclo Microsoft CRUI 3° Sessione: l'evoluzione delle piattaforme tecnologi...
2° Ciclo Microsoft CRUI 3° Sessione: l'evoluzione delle piattaforme tecnologi...
 
Advanced SQL - Introduction to Databases (1007156ANR).pdf
Advanced SQL - Introduction to Databases (1007156ANR).pdfAdvanced SQL - Introduction to Databases (1007156ANR).pdf
Advanced SQL - Introduction to Databases (1007156ANR).pdf
 
Chapter09
Chapter09Chapter09
Chapter09
 
chap 9 dbms.ppt
chap 9 dbms.pptchap 9 dbms.ppt
chap 9 dbms.ppt
 
oodb.ppt
oodb.pptoodb.ppt
oodb.ppt
 
Pl sql
Pl sqlPl sql
Pl sql
 
CC105-WEEK10-11-INTRODUCTION-TO-SQL.pdf
CC105-WEEK10-11-INTRODUCTION-TO-SQL.pdfCC105-WEEK10-11-INTRODUCTION-TO-SQL.pdf
CC105-WEEK10-11-INTRODUCTION-TO-SQL.pdf
 
Sql server
Sql serverSql server
Sql server
 
Sql Summit Clr, Service Broker And Xml
Sql Summit   Clr, Service Broker And XmlSql Summit   Clr, Service Broker And Xml
Sql Summit Clr, Service Broker And Xml
 
SharePoint Security in an Insecure World - AUSPC 2012
SharePoint Security in an Insecure World - AUSPC 2012SharePoint Security in an Insecure World - AUSPC 2012
SharePoint Security in an Insecure World - AUSPC 2012
 
Oracle - Program with PL/SQL - Lession 02
Oracle - Program with PL/SQL - Lession 02Oracle - Program with PL/SQL - Lession 02
Oracle - Program with PL/SQL - Lession 02
 
Catalyst optimizer
Catalyst optimizerCatalyst optimizer
Catalyst optimizer
 
Building Report Cards and Transcripts in The Education Edge
Building Report Cards and Transcripts in The Education EdgeBuilding Report Cards and Transcripts in The Education Edge
Building Report Cards and Transcripts in The Education Edge
 
Evolutionary db development
Evolutionary db development Evolutionary db development
Evolutionary db development
 
Fast federated SQL with Apache Calcite
Fast federated SQL with Apache CalciteFast federated SQL with Apache Calcite
Fast federated SQL with Apache Calcite
 
Introduction to Threading in .Net
Introduction to Threading in .NetIntroduction to Threading in .Net
Introduction to Threading in .Net
 
Blue Phoenix Idms Migration
Blue Phoenix Idms MigrationBlue Phoenix Idms Migration
Blue Phoenix Idms Migration
 

Recently uploaded

Scouring of cotton and wool fabric with effective scouring method
Scouring of cotton and wool fabric with effective scouring methodScouring of cotton and wool fabric with effective scouring method
Scouring of cotton and wool fabric with effective scouring method
vimal412355
 
Artificial intelligence presentation2-171219131633.pdf
Artificial intelligence presentation2-171219131633.pdfArtificial intelligence presentation2-171219131633.pdf
Artificial intelligence presentation2-171219131633.pdf
Kira Dess
 
01-vogelsanger-stanag-4178-ed-2-the-new-nato-standard-for-nitrocellulose-test...
01-vogelsanger-stanag-4178-ed-2-the-new-nato-standard-for-nitrocellulose-test...01-vogelsanger-stanag-4178-ed-2-the-new-nato-standard-for-nitrocellulose-test...
01-vogelsanger-stanag-4178-ed-2-the-new-nato-standard-for-nitrocellulose-test...
AshwaniAnuragi1
 
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
AldoGarca30
 

Recently uploaded (20)

Overview of Transformation in Computer Graphics
Overview of Transformation in Computer GraphicsOverview of Transformation in Computer Graphics
Overview of Transformation in Computer Graphics
 
Scouring of cotton and wool fabric with effective scouring method
Scouring of cotton and wool fabric with effective scouring methodScouring of cotton and wool fabric with effective scouring method
Scouring of cotton and wool fabric with effective scouring method
 
Path loss model, OKUMURA Model, Hata Model
Path loss model, OKUMURA Model, Hata ModelPath loss model, OKUMURA Model, Hata Model
Path loss model, OKUMURA Model, Hata Model
 
Artificial intelligence presentation2-171219131633.pdf
Artificial intelligence presentation2-171219131633.pdfArtificial intelligence presentation2-171219131633.pdf
Artificial intelligence presentation2-171219131633.pdf
 
engineering chemistry power point presentation
engineering chemistry  power point presentationengineering chemistry  power point presentation
engineering chemistry power point presentation
 
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptxHOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
 
Circuit Breakers for Engineering Students
Circuit Breakers for Engineering StudentsCircuit Breakers for Engineering Students
Circuit Breakers for Engineering Students
 
Ground Improvement Technique: Earth Reinforcement
Ground Improvement Technique: Earth ReinforcementGround Improvement Technique: Earth Reinforcement
Ground Improvement Technique: Earth Reinforcement
 
analog-vs-digital-communication (concept of analog and digital).pptx
analog-vs-digital-communication (concept of analog and digital).pptxanalog-vs-digital-communication (concept of analog and digital).pptx
analog-vs-digital-communication (concept of analog and digital).pptx
 
NEWLETTER FRANCE HELICES/ SDS SURFACE DRIVES - MAY 2024
NEWLETTER FRANCE HELICES/ SDS SURFACE DRIVES - MAY 2024NEWLETTER FRANCE HELICES/ SDS SURFACE DRIVES - MAY 2024
NEWLETTER FRANCE HELICES/ SDS SURFACE DRIVES - MAY 2024
 
Independent Solar-Powered Electric Vehicle Charging Station
Independent Solar-Powered Electric Vehicle Charging StationIndependent Solar-Powered Electric Vehicle Charging Station
Independent Solar-Powered Electric Vehicle Charging Station
 
Involute of a circle,Square, pentagon,HexagonInvolute_Engineering Drawing.pdf
Involute of a circle,Square, pentagon,HexagonInvolute_Engineering Drawing.pdfInvolute of a circle,Square, pentagon,HexagonInvolute_Engineering Drawing.pdf
Involute of a circle,Square, pentagon,HexagonInvolute_Engineering Drawing.pdf
 
Fundamentals of Internet of Things (IoT) Part-2
Fundamentals of Internet of Things (IoT) Part-2Fundamentals of Internet of Things (IoT) Part-2
Fundamentals of Internet of Things (IoT) Part-2
 
Study of Computer Hardware System using Block Diagram
Study of Computer Hardware System using Block DiagramStudy of Computer Hardware System using Block Diagram
Study of Computer Hardware System using Block Diagram
 
01-vogelsanger-stanag-4178-ed-2-the-new-nato-standard-for-nitrocellulose-test...
01-vogelsanger-stanag-4178-ed-2-the-new-nato-standard-for-nitrocellulose-test...01-vogelsanger-stanag-4178-ed-2-the-new-nato-standard-for-nitrocellulose-test...
01-vogelsanger-stanag-4178-ed-2-the-new-nato-standard-for-nitrocellulose-test...
 
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
 
Computer Graphics Introduction To Curves
Computer Graphics Introduction To CurvesComputer Graphics Introduction To Curves
Computer Graphics Introduction To Curves
 
Databricks Generative AI Fundamentals .pdf
Databricks Generative AI Fundamentals  .pdfDatabricks Generative AI Fundamentals  .pdf
Databricks Generative AI Fundamentals .pdf
 
Autodesk Construction Cloud (Autodesk Build).pptx
Autodesk Construction Cloud (Autodesk Build).pptxAutodesk Construction Cloud (Autodesk Build).pptx
Autodesk Construction Cloud (Autodesk Build).pptx
 
Worksharing and 3D Modeling with Revit.pptx
Worksharing and 3D Modeling with Revit.pptxWorksharing and 3D Modeling with Revit.pptx
Worksharing and 3D Modeling with Revit.pptx
 

LM7_ Embedded Sql and Dynamic SQL in dbms

  • 1. Department of Computer Science and Engineering Name of the Faculty : Ms. Aruna T N Subject Name & Code : CS3492/ Database Management Systems Branch & Department : Computer Science and Engineering Year & Semester : II / IV CS3492/DBMS/IICSE/IVSEM/KG-KiTE KGiSL Institute of Technology (Approved by AICTE, New Delhi; Affiliated to Anna University, Chennai) Recognized by UGC, Accredited by NBA (IT) 365, KGiSL Campus, Thudiyalur Road, Saravanampatti, Coimbatore – 641035.
  • 2. Course Outcome CO 1 Demonstrate fundamentals of Data models and Relational databases - K3 LEVEL CS3492/DBMS/IICSE/IVSEM/KG-KiTE
  • 3. Syllabus UNIT I - RELATIONAL DATABASES Purpose of Database System – Views of data – Data Models – Database System Architecture – Introduction to relational databases – Relational Model – Keys – Relational Algebra – SQL fundamentals – Advanced SQL features – Embedded SQL– Dynamic SQL CS3492/DBMS/IICSE/IVSEM/KG-KiTE
  • 5. Embedded SQL • The Programming module in which the SQL statements are embedded is called Embedded SQL module. • It is possible to embed SQL statements inside a variety of programming languages such as C, C++, Java, Fortran, and PL/1, • A language to which SQL queries are embedded is referred to as a host language. • EXEC SQL statement is used in the host language to identify embedded SQL request to the preprocessor EXEC SQL <embedded SQL statement >; CS3492/DBMS/IICSE/IVSEM/KG-KiTE
  • 6. Embedded SQL(Cont.) • Before executing any SQL statements, the program must first connect to the database. This is done using: EXEC-SQL connect to server user user-name using password; Here, server identifies the server to which a connection is to be established. • Variables of the host language can be used within embedded SQL statements. They are preceded by a colon (:) to distinguish from SQL variables (e.g., :credit_amount ) • In some languages, like COBOL, the semicolon is replaced with END- EXEC • In Java embedding uses # SQL { …. }; CS3492/DBMS/IICSE/IVSEM/KG-KiTE
  • 7. Embedded SQL(Cont.) • Variables used must be declared within DECLARE section, as illustrated below. The syntax for declaring the variables, however, follows the usual host language syntax. EXEC-SQL BEGIN DECLARE SECTION; int credit-amount ; EXEC-SQL END DECLARE SECTION; CS3492/DBMS/IICSE/IVSEM/KG-KiTE
  • 8. Embedded SQL(Cont.) • To write an embedded SQL query, we use the declare c cursor for <SQL query> statement. The variable c is used to identify the query • Example: From within a host language, find the ID and name of students who have completed more than the number of credits stored in variable credit_amount in the host langue. CS3492/DBMS/IICSE/IVSEM/KG-KiTE
  • 9. Embedded SQL(Cont.) CS3492/DBMS/IICSE/IVSEM/KG-KiTE • Specify the query in SQL as follows: EXEC SQL declare c cursor for select ID, name from student where tot_cred > :credit_amount END_EXEC
  • 10. Embedded SQL(Cont.) • The open statement for our example is as follows: EXEC SQL open c ; • This statement causes the database system to execute the query and to save the results within a temporary relation. The query uses the value of the host- language variable credit-amount at the time the open statement is executed. • The fetch statement causes the values of one tuple in the query result to be placed on host language variables. EXEC SQL fetch c into :si, :sn END_EXEC CS3492/DBMS/IICSE/IVSEM/KG-KiTE
  • 11. Embedded SQL(Cont.) CS3492/DBMS/IICSE/IVSEM/KG-KiTE • A variable called SQLSTATE in the SQL communication area (SQLCA) gets set to '02000' to indicate no more data is available • The close statement causes the database system to delete the temporary relation that holds the result of the query. EXEC SQL close c ; Note: above details vary with language. For example, the Java embedding defines Java iterators to step through result tuples.
  • 12. Updates through Embedded SQL • Embedded SQL expressions for database modification (update, insert, and delete) • Can update tuples fetched by cursor by declaring that the cursor is for update EXEC SQL declare c cursor for select * from instructor where dept_name = 'Music' for update CS3492/DBMS/IICSE/IVSEM/KG-KiTE
  • 13. Updates through Embedded SQL • We then iterate through the tuples by performing fetch operations on the cursor (as illustrated earlier), and after fetching each tuple we execute the following code: update instructor set salary = salary + 1000 where current of c
  • 14. Dynamic SQL • The dynamic SQL component of SQL allows programs to construct and submit SQL queries at run time. • In contrast, embedded SQL statements must be completely present at compile time, they are compiled by the embedded SQL preprocessor. • Using dynamic SQL programs can create SQL queries as strings at runtime and can either have them executed immediately or have them prepared for sub sequent use.