SlideShare a Scribd company logo
UNIT II
EMBEDDED SQL – STATIC & DYNAMIC SQL
Embedded SQL
 Most SQL statements can be embedded in a general-
purpose host programming language such as COBOL, C,
Java
 An embedded SQL statement is distinguished from the
host language statements by enclosing it between EXEC
SQL or EXEC SQL BEGIN and a matching END-EXEC or
EXEC SQL END (or semicolon)
Syntax may vary with language
Shared variables (used in both languages) usually
prefixed with a colon (:) in SQL
COMMUNICATION BETWEEN THE PROGRAM AND
THE DBMS USING SQL CODE
The two communication variables that are used to communicate error
condition to the program are SQLCODE and SQLSTATE.
After each database command is executed, the DBMS returns a value in
SQLCODE, such that
SQLCODE=0 statement executed successfully
SQLCODE>0 no more data available
SQLCODE<0 some error has occured
Example: Variable Declaration
in Language C
 Variables inside DECLARE are shared and can appear (while
prefixed by a colon) in SQL statements
 SQLCODE is used to communicate errors/exceptions between the
database and the program
int loop;
EXEC SQL BEGIN DECLARE SECTION;
varchar dname[16], fname[16], …;
char ssn[10], bdate[11], …;
int dno, dnumber, SQLCODE, …;
EXEC SQL END DECLARE SECTION;
RETRIVING MULTIPLE TUPELS
 The query result table may contain more than one row. Consequently,
we must treat this as a multi-row query and use a cursor to retrieve the
data.
For that we have to set up a cursor to select the corresponding rows
from the table. After opening the cursor, we loop over each row of the
result table and print out the corresponding columns.
 When there are no more rows to be processed, we close the cursor and
terminate.
 If an error occurs at any point, we generate a suitable error message and
stop.
SQL Commands for
Connecting to a Database
 Connection (multiple connections are possible but only one is active)
CONNECT TO server-name AS connection-name
AUTHORIZATION user-account-info;
 Change from an active connection to another one
SET CONNECTION connection-name;
 Disconnection
DISCONNECT connection-name;
Embedded SQL in C
Programming Examples
loop = 1;
while (loop) {
prompt (“Enter SSN: “, ssn);
EXEC SQL
select FNAME, LNAME, ADDRESS, SALARY
into :fname, :lname, :address, :salary
from EMPLOYEE where SSN == :ssn;
if (SQLCODE == 0) printf(fname, …);
else printf(“SSN does not exist: “, ssn);
prompt(“More SSN? (1=yes, 0=no): “, loop);
END-EXEC
}
Embedded SQL in C
Programming Examples
 A cursor (iterator) is needed to process multiple tuples
 FETCH commands move the cursor to the next tuple
 CLOSE CURSOR indicates that the processing of query
results has been completed
prompt("Enter the Department Name: ", dname) ;
EXEC SQL
select Dnumber into :dnumber from DEPARTMENT where Dname = :dname ;
EXEC SQL DECLARE EMP CURSOR FOR
select Ssn, Fname, Minit, Lname, Salary from EMPLOYEE where Dno = :dnumber
FOR UPDATE OF Salary ;
EXEC SQL OPEN EMP ;
EXEC SQL FETCH from EMP into :ssn, :fname, :minit, :lname, :salary ;
while (SQLCODE == 0) {
printf("Employee name is:", Fname, Minit, Lname) ;
prompt("Enter the raise amount: ", raise) ;
EXEC SQL
update EMPLOYEE
set Salary = Salary + :raise
where CURRENT OF EMP ;
EXEC SQL FETCH from EMP into :ssn, :fname, :minit, :lname, :salary ;
}
EXEC SQL CLOSE EMP ;
STATIC SQL VS DYNAMIC SQL
 Static SQL is SQL statements in an application that do not change at runtime
and, therefore, can be hard-coded into the application.
 Static SQL provides performance advantages over dynamic SQL because static
SQL is pre-processed, which means the statements are parsed, validated, and
optimized only once.
 The full text of static SQL statements are known at compilation, which provides
the following benefits:
 Successful compilation verifies that the SQL statements reference valid
database objects.
 Successful compilation verifies that the necessary privileges are in place to
access the database objects.
 Performance of static SQL is generally better than dynamic SQL.
STATIC SQL VS DYNAMIC SQL
 Static SQL has limitations that can be overcome with dynamic SQL. You may not
always know the full text of the SQL statements that must be executed in a PL/SQL
procedure. Your program may accept user input that defines the SQL statements to
execute, or your program may need to complete some processing work to determine
the correct course of action. In such cases, you should use dynamic SQL.
 For example, consider a reporting application that performs standard queries on
tables in a data warehouse environment where the exact table name is unknown
until runtime.
 To accommodate the large amount of data in the data warehouse efficiently, you
create a new table every quarter to store the invoice information for the quarter.
These tables all have exactly the same definition and are named according to the
starting month and year of the quarter, for
example INV_01_1997, INV_04_1997, INV_07_1997, INV_10_1997, INV_01_1998, etc.
In such a case, you can use dynamic SQL in your reporting application to specify
the table name at runtime.
Dynamic SQL
 Dynamic SQL is SQL statements that are constructed at runtime; for
example, the application may allow users to enter their own queries.
Thus, the SQL statements cannot be hard-coded into the application.
 Objective:
 Composing and executing new (not previously compiled) SQL statements
at run-time
 a program accepts SQL statements from the keyboard at run-time
 a point-and-click operation translates to certain SQL query
 Dynamic update is relatively simple; dynamic query can be complex
 because the type and number of retrieved attributes are unknown at
compile time
DIFFERENCE BETWEEN STATIC AND
DYNAMIC SQL
 Static (embedded) SQL
 In static SQL how database
will be accessed is
predetermined in the
embedded SQL statement.
 It is more swift and efficient.
 Dynamic (interactive) SQL
 In dynamic SQL, how
database will be accessed is
determined at run time.
 It is less swift and efficient.
 Static (embedded) SQL
 SQL statements are compiled at
compile time.
 Parsing, validation, optimization,
and generation of application plan
are done at compile time.
 It is generally used for situations
where data is distributed uniformly.
 Execution IMMEDIATE,EXECUTE
and PREPARE statements are not
used.
 Dynamic (interactive) SQL
 SQL statements are compiled
at run time.
 Parsing, validation,
optimization, and generation
of application plan are done
at run time.
 It is generally used for
situations where data is
distributed non-uniformly.
 EXECUTE IMMEDIATE,
EXECUTE and PREPARE
statements are used.
Ad

More Related Content

What's hot (20)

Java: GUI
Java: GUIJava: GUI
Java: GUI
Tareq Hasan
 
1 - Introduction to PL/SQL
1 - Introduction to PL/SQL1 - Introduction to PL/SQL
1 - Introduction to PL/SQL
rehaniltifat
 
Packages in PL/SQL
Packages in PL/SQLPackages in PL/SQL
Packages in PL/SQL
Pooja Dixit
 
Java package
Java packageJava package
Java package
CS_GDRCST
 
Sql fundamentals
Sql fundamentalsSql fundamentals
Sql fundamentals
Ravinder Kamboj
 
Entity Relationship Diagram
Entity Relationship DiagramEntity Relationship Diagram
Entity Relationship Diagram
Shakila Mahjabin
 
GUI Programming In Java
GUI Programming In JavaGUI Programming In Java
GUI Programming In Java
yht4ever
 
Generics
GenericsGenerics
Generics
Ravi_Kant_Sahu
 
Event handling
Event handlingEvent handling
Event handling
swapnac12
 
QSpiders - Jdk Jvm Jre and Jit
QSpiders - Jdk Jvm Jre and JitQSpiders - Jdk Jvm Jre and Jit
QSpiders - Jdk Jvm Jre and Jit
Qspiders - Software Testing Training Institute
 
Triggers and active database
Triggers and active databaseTriggers and active database
Triggers and active database
BalaMuruganSamuthira
 
Packages in java
Packages in javaPackages in java
Packages in java
Elizabeth alexander
 
Dbms
DbmsDbms
Dbms
sevtap87
 
Timestamp protocols
Timestamp protocolsTimestamp protocols
Timestamp protocols
Prashant Saini
 
Adbms lab manual
Adbms lab manualAdbms lab manual
Adbms lab manual
RAKESH KUMAR
 
joins in database
 joins in database joins in database
joins in database
Sultan Arshad
 
Data types in java
Data types in javaData types in java
Data types in java
HarshitaAshwani
 
GUI components in Java
GUI components in JavaGUI components in Java
GUI components in Java
kirupasuchi1996
 
Common language runtime clr
Common language runtime clrCommon language runtime clr
Common language runtime clr
SanSan149
 
Syntax directed translation
Syntax directed translationSyntax directed translation
Syntax directed translation
Akshaya Arunan
 

Similar to embedded-static-&dynamic (20)

Chapter09
Chapter09Chapter09
Chapter09
sasa_eldoby
 
Dynamic and Embedded SQL for db practices.pptx
Dynamic and Embedded SQL for db practices.pptxDynamic and Embedded SQL for db practices.pptx
Dynamic and Embedded SQL for db practices.pptx
angelinjeba6
 
Cursors, triggers, procedures
Cursors, triggers, proceduresCursors, triggers, procedures
Cursors, triggers, procedures
Vaibhav Kathuria
 
PL/SQL Complete Tutorial. All Topics Covered
PL/SQL Complete Tutorial. All Topics CoveredPL/SQL Complete Tutorial. All Topics Covered
PL/SQL Complete Tutorial. All Topics Covered
Danish Mehraj
 
Ebook11
Ebook11Ebook11
Ebook11
kaashiv1
 
Oracle etl openworld
Oracle etl openworldOracle etl openworld
Oracle etl openworld
Rodrigo Bastos
 
introduction to SQL query language beginner.ppt
introduction to SQL query language beginner.pptintroduction to SQL query language beginner.ppt
introduction to SQL query language beginner.ppt
PatriceRochon1
 
Advanced SQL - Database Access from Programming Languages
Advanced SQL - Database Access  from Programming LanguagesAdvanced SQL - Database Access  from Programming Languages
Advanced SQL - Database Access from Programming Languages
S.Shayan Daneshvar
 
Database Systems Design Implementation and Management 11th Edition Coronel So...
Database Systems Design Implementation and Management 11th Edition Coronel So...Database Systems Design Implementation and Management 11th Edition Coronel So...
Database Systems Design Implementation and Management 11th Edition Coronel So...
hmuraratgai
 
PROCEDURAL LANGUAGE/ STRUCTURED QUERY LANGUAGE.pdf
PROCEDURAL LANGUAGE/ STRUCTURED QUERY LANGUAGE.pdfPROCEDURAL LANGUAGE/ STRUCTURED QUERY LANGUAGE.pdf
PROCEDURAL LANGUAGE/ STRUCTURED QUERY LANGUAGE.pdf
rajeswaria21
 
Sql server
Sql serverSql server
Sql server
Puja Gupta
 
Database Systems Design Implementation and Management 11th Edition Coronel So...
Database Systems Design Implementation and Management 11th Edition Coronel So...Database Systems Design Implementation and Management 11th Edition Coronel So...
Database Systems Design Implementation and Management 11th Edition Coronel So...
libogdelcy
 
Database Systems Design Implementation and Management 11th Edition Coronel So...
Database Systems Design Implementation and Management 11th Edition Coronel So...Database Systems Design Implementation and Management 11th Edition Coronel So...
Database Systems Design Implementation and Management 11th Edition Coronel So...
gazangyuones
 
[Www.pkbulk.blogspot.com]dbms07
[Www.pkbulk.blogspot.com]dbms07[Www.pkbulk.blogspot.com]dbms07
[Www.pkbulk.blogspot.com]dbms07
AnusAhmad
 
Database Systems Design Implementation and Management 11th Edition Coronel So...
Database Systems Design Implementation and Management 11th Edition Coronel So...Database Systems Design Implementation and Management 11th Edition Coronel So...
Database Systems Design Implementation and Management 11th Edition Coronel So...
meeritmoral56
 
Kaashiv SQL Server Interview Questions Presentation
Kaashiv SQL Server Interview Questions PresentationKaashiv SQL Server Interview Questions Presentation
Kaashiv SQL Server Interview Questions Presentation
kaashiv1
 
What does PL_SQL stand for and what is the functioning of PL_SQL.docx
What does PL_SQL stand for and what is the functioning of PL_SQL.docxWhat does PL_SQL stand for and what is the functioning of PL_SQL.docx
What does PL_SQL stand for and what is the functioning of PL_SQL.docx
shivanikaale214
 
MODULE 5.pptx
MODULE 5.pptxMODULE 5.pptx
MODULE 5.pptx
lathass5
 
Introduction to sql server
Introduction to sql serverIntroduction to sql server
Introduction to sql server
Vinay Thota
 
L9 l10 server side programming
L9 l10  server side programmingL9 l10  server side programming
L9 l10 server side programming
Rushdi Shams
 
Dynamic and Embedded SQL for db practices.pptx
Dynamic and Embedded SQL for db practices.pptxDynamic and Embedded SQL for db practices.pptx
Dynamic and Embedded SQL for db practices.pptx
angelinjeba6
 
Cursors, triggers, procedures
Cursors, triggers, proceduresCursors, triggers, procedures
Cursors, triggers, procedures
Vaibhav Kathuria
 
PL/SQL Complete Tutorial. All Topics Covered
PL/SQL Complete Tutorial. All Topics CoveredPL/SQL Complete Tutorial. All Topics Covered
PL/SQL Complete Tutorial. All Topics Covered
Danish Mehraj
 
introduction to SQL query language beginner.ppt
introduction to SQL query language beginner.pptintroduction to SQL query language beginner.ppt
introduction to SQL query language beginner.ppt
PatriceRochon1
 
Advanced SQL - Database Access from Programming Languages
Advanced SQL - Database Access  from Programming LanguagesAdvanced SQL - Database Access  from Programming Languages
Advanced SQL - Database Access from Programming Languages
S.Shayan Daneshvar
 
Database Systems Design Implementation and Management 11th Edition Coronel So...
Database Systems Design Implementation and Management 11th Edition Coronel So...Database Systems Design Implementation and Management 11th Edition Coronel So...
Database Systems Design Implementation and Management 11th Edition Coronel So...
hmuraratgai
 
PROCEDURAL LANGUAGE/ STRUCTURED QUERY LANGUAGE.pdf
PROCEDURAL LANGUAGE/ STRUCTURED QUERY LANGUAGE.pdfPROCEDURAL LANGUAGE/ STRUCTURED QUERY LANGUAGE.pdf
PROCEDURAL LANGUAGE/ STRUCTURED QUERY LANGUAGE.pdf
rajeswaria21
 
Database Systems Design Implementation and Management 11th Edition Coronel So...
Database Systems Design Implementation and Management 11th Edition Coronel So...Database Systems Design Implementation and Management 11th Edition Coronel So...
Database Systems Design Implementation and Management 11th Edition Coronel So...
libogdelcy
 
Database Systems Design Implementation and Management 11th Edition Coronel So...
Database Systems Design Implementation and Management 11th Edition Coronel So...Database Systems Design Implementation and Management 11th Edition Coronel So...
Database Systems Design Implementation and Management 11th Edition Coronel So...
gazangyuones
 
[Www.pkbulk.blogspot.com]dbms07
[Www.pkbulk.blogspot.com]dbms07[Www.pkbulk.blogspot.com]dbms07
[Www.pkbulk.blogspot.com]dbms07
AnusAhmad
 
Database Systems Design Implementation and Management 11th Edition Coronel So...
Database Systems Design Implementation and Management 11th Edition Coronel So...Database Systems Design Implementation and Management 11th Edition Coronel So...
Database Systems Design Implementation and Management 11th Edition Coronel So...
meeritmoral56
 
Kaashiv SQL Server Interview Questions Presentation
Kaashiv SQL Server Interview Questions PresentationKaashiv SQL Server Interview Questions Presentation
Kaashiv SQL Server Interview Questions Presentation
kaashiv1
 
What does PL_SQL stand for and what is the functioning of PL_SQL.docx
What does PL_SQL stand for and what is the functioning of PL_SQL.docxWhat does PL_SQL stand for and what is the functioning of PL_SQL.docx
What does PL_SQL stand for and what is the functioning of PL_SQL.docx
shivanikaale214
 
MODULE 5.pptx
MODULE 5.pptxMODULE 5.pptx
MODULE 5.pptx
lathass5
 
Introduction to sql server
Introduction to sql serverIntroduction to sql server
Introduction to sql server
Vinay Thota
 
L9 l10 server side programming
L9 l10  server side programmingL9 l10  server side programming
L9 l10 server side programming
Rushdi Shams
 
Ad

More from Saranya Natarajan (9)

cns unit 1.pptx
cns unit 1.pptxcns unit 1.pptx
cns unit 1.pptx
Saranya Natarajan
 
Asymptotic notation
Asymptotic notationAsymptotic notation
Asymptotic notation
Saranya Natarajan
 
Fundamentals of the Analysis of Algorithm Efficiency
Fundamentals of the Analysis of Algorithm EfficiencyFundamentals of the Analysis of Algorithm Efficiency
Fundamentals of the Analysis of Algorithm Efficiency
Saranya Natarajan
 
mutidimensional database
mutidimensional databasemutidimensional database
mutidimensional database
Saranya Natarajan
 
ER-Model-ER Diagram
ER-Model-ER DiagramER-Model-ER Diagram
ER-Model-ER Diagram
Saranya Natarajan
 
Query-porcessing-& Query optimization
Query-porcessing-& Query optimizationQuery-porcessing-& Query optimization
Query-porcessing-& Query optimization
Saranya Natarajan
 
concurrency-control
concurrency-controlconcurrency-control
concurrency-control
Saranya Natarajan
 
deadlock
deadlockdeadlock
deadlock
Saranya Natarajan
 
serializability in dbms
serializability in dbmsserializability in dbms
serializability in dbms
Saranya Natarajan
 
Ad

Recently uploaded (20)

International Journal of Distributed and Parallel systems (IJDPS)
International Journal of Distributed and Parallel systems (IJDPS)International Journal of Distributed and Parallel systems (IJDPS)
International Journal of Distributed and Parallel systems (IJDPS)
samueljackson3773
 
fluke dealers in bangalore..............
fluke dealers in bangalore..............fluke dealers in bangalore..............
fluke dealers in bangalore..............
Haresh Vaswani
 
211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf
211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf
211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf
inmishra17121973
 
Process Parameter Optimization for Minimizing Springback in Cold Drawing Proc...
Process Parameter Optimization for Minimizing Springback in Cold Drawing Proc...Process Parameter Optimization for Minimizing Springback in Cold Drawing Proc...
Process Parameter Optimization for Minimizing Springback in Cold Drawing Proc...
Journal of Soft Computing in Civil Engineering
 
Value Stream Mapping Worskshops for Intelligent Continuous Security
Value Stream Mapping Worskshops for Intelligent Continuous SecurityValue Stream Mapping Worskshops for Intelligent Continuous Security
Value Stream Mapping Worskshops for Intelligent Continuous Security
Marc Hornbeek
 
Degree_of_Automation.pdf for Instrumentation and industrial specialist
Degree_of_Automation.pdf for  Instrumentation  and industrial specialistDegree_of_Automation.pdf for  Instrumentation  and industrial specialist
Degree_of_Automation.pdf for Instrumentation and industrial specialist
shreyabhosale19
 
introduction to machine learining for beginers
introduction to machine learining for beginersintroduction to machine learining for beginers
introduction to machine learining for beginers
JoydebSheet
 
five-year-soluhhhhhhhhhhhhhhhhhtions.pdf
five-year-soluhhhhhhhhhhhhhhhhhtions.pdffive-year-soluhhhhhhhhhhhhhhhhhtions.pdf
five-year-soluhhhhhhhhhhhhhhhhhtions.pdf
AdityaSharma944496
 
QA/QC Manager (Quality management Expert)
QA/QC Manager (Quality management Expert)QA/QC Manager (Quality management Expert)
QA/QC Manager (Quality management Expert)
rccbatchplant
 
The Gaussian Process Modeling Module in UQLab
The Gaussian Process Modeling Module in UQLabThe Gaussian Process Modeling Module in UQLab
The Gaussian Process Modeling Module in UQLab
Journal of Soft Computing in Civil Engineering
 
DT REPORT by Tech titan GROUP to introduce the subject design Thinking
DT REPORT by Tech titan GROUP to introduce the subject design ThinkingDT REPORT by Tech titan GROUP to introduce the subject design Thinking
DT REPORT by Tech titan GROUP to introduce the subject design Thinking
DhruvChotaliya2
 
Metal alkyne complexes.pptx in chemistry
Metal alkyne complexes.pptx in chemistryMetal alkyne complexes.pptx in chemistry
Metal alkyne complexes.pptx in chemistry
mee23nu
 
Level 1-Safety.pptx Presentation of Electrical Safety
Level 1-Safety.pptx Presentation of Electrical SafetyLevel 1-Safety.pptx Presentation of Electrical Safety
Level 1-Safety.pptx Presentation of Electrical Safety
JoseAlbertoCariasDel
 
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E..."Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
Infopitaara
 
Compiler Design Unit1 PPT Phases of Compiler.pptx
Compiler Design Unit1 PPT Phases of Compiler.pptxCompiler Design Unit1 PPT Phases of Compiler.pptx
Compiler Design Unit1 PPT Phases of Compiler.pptx
RushaliDeshmukh2
 
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptxLidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
RishavKumar530754
 
Explainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptx
Explainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptxExplainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptx
Explainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptx
MahaveerVPandit
 
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
charlesdick1345
 
IntroSlides-April-BuildWithAI-VertexAI.pdf
IntroSlides-April-BuildWithAI-VertexAI.pdfIntroSlides-April-BuildWithAI-VertexAI.pdf
IntroSlides-April-BuildWithAI-VertexAI.pdf
Luiz Carneiro
 
Avnet Silica's PCIM 2025 Highlights Flyer
Avnet Silica's PCIM 2025 Highlights FlyerAvnet Silica's PCIM 2025 Highlights Flyer
Avnet Silica's PCIM 2025 Highlights Flyer
WillDavies22
 
International Journal of Distributed and Parallel systems (IJDPS)
International Journal of Distributed and Parallel systems (IJDPS)International Journal of Distributed and Parallel systems (IJDPS)
International Journal of Distributed and Parallel systems (IJDPS)
samueljackson3773
 
fluke dealers in bangalore..............
fluke dealers in bangalore..............fluke dealers in bangalore..............
fluke dealers in bangalore..............
Haresh Vaswani
 
211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf
211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf
211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf
inmishra17121973
 
Value Stream Mapping Worskshops for Intelligent Continuous Security
Value Stream Mapping Worskshops for Intelligent Continuous SecurityValue Stream Mapping Worskshops for Intelligent Continuous Security
Value Stream Mapping Worskshops for Intelligent Continuous Security
Marc Hornbeek
 
Degree_of_Automation.pdf for Instrumentation and industrial specialist
Degree_of_Automation.pdf for  Instrumentation  and industrial specialistDegree_of_Automation.pdf for  Instrumentation  and industrial specialist
Degree_of_Automation.pdf for Instrumentation and industrial specialist
shreyabhosale19
 
introduction to machine learining for beginers
introduction to machine learining for beginersintroduction to machine learining for beginers
introduction to machine learining for beginers
JoydebSheet
 
five-year-soluhhhhhhhhhhhhhhhhhtions.pdf
five-year-soluhhhhhhhhhhhhhhhhhtions.pdffive-year-soluhhhhhhhhhhhhhhhhhtions.pdf
five-year-soluhhhhhhhhhhhhhhhhhtions.pdf
AdityaSharma944496
 
QA/QC Manager (Quality management Expert)
QA/QC Manager (Quality management Expert)QA/QC Manager (Quality management Expert)
QA/QC Manager (Quality management Expert)
rccbatchplant
 
DT REPORT by Tech titan GROUP to introduce the subject design Thinking
DT REPORT by Tech titan GROUP to introduce the subject design ThinkingDT REPORT by Tech titan GROUP to introduce the subject design Thinking
DT REPORT by Tech titan GROUP to introduce the subject design Thinking
DhruvChotaliya2
 
Metal alkyne complexes.pptx in chemistry
Metal alkyne complexes.pptx in chemistryMetal alkyne complexes.pptx in chemistry
Metal alkyne complexes.pptx in chemistry
mee23nu
 
Level 1-Safety.pptx Presentation of Electrical Safety
Level 1-Safety.pptx Presentation of Electrical SafetyLevel 1-Safety.pptx Presentation of Electrical Safety
Level 1-Safety.pptx Presentation of Electrical Safety
JoseAlbertoCariasDel
 
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E..."Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
Infopitaara
 
Compiler Design Unit1 PPT Phases of Compiler.pptx
Compiler Design Unit1 PPT Phases of Compiler.pptxCompiler Design Unit1 PPT Phases of Compiler.pptx
Compiler Design Unit1 PPT Phases of Compiler.pptx
RushaliDeshmukh2
 
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptxLidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
RishavKumar530754
 
Explainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptx
Explainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptxExplainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptx
Explainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptx
MahaveerVPandit
 
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
charlesdick1345
 
IntroSlides-April-BuildWithAI-VertexAI.pdf
IntroSlides-April-BuildWithAI-VertexAI.pdfIntroSlides-April-BuildWithAI-VertexAI.pdf
IntroSlides-April-BuildWithAI-VertexAI.pdf
Luiz Carneiro
 
Avnet Silica's PCIM 2025 Highlights Flyer
Avnet Silica's PCIM 2025 Highlights FlyerAvnet Silica's PCIM 2025 Highlights Flyer
Avnet Silica's PCIM 2025 Highlights Flyer
WillDavies22
 

embedded-static-&dynamic

  • 1. UNIT II EMBEDDED SQL – STATIC & DYNAMIC SQL
  • 2. Embedded SQL  Most SQL statements can be embedded in a general- purpose host programming language such as COBOL, C, Java  An embedded SQL statement is distinguished from the host language statements by enclosing it between EXEC SQL or EXEC SQL BEGIN and a matching END-EXEC or EXEC SQL END (or semicolon) Syntax may vary with language Shared variables (used in both languages) usually prefixed with a colon (:) in SQL
  • 3. COMMUNICATION BETWEEN THE PROGRAM AND THE DBMS USING SQL CODE The two communication variables that are used to communicate error condition to the program are SQLCODE and SQLSTATE. After each database command is executed, the DBMS returns a value in SQLCODE, such that SQLCODE=0 statement executed successfully SQLCODE>0 no more data available SQLCODE<0 some error has occured
  • 4. Example: Variable Declaration in Language C  Variables inside DECLARE are shared and can appear (while prefixed by a colon) in SQL statements  SQLCODE is used to communicate errors/exceptions between the database and the program int loop; EXEC SQL BEGIN DECLARE SECTION; varchar dname[16], fname[16], …; char ssn[10], bdate[11], …; int dno, dnumber, SQLCODE, …; EXEC SQL END DECLARE SECTION;
  • 5. RETRIVING MULTIPLE TUPELS  The query result table may contain more than one row. Consequently, we must treat this as a multi-row query and use a cursor to retrieve the data. For that we have to set up a cursor to select the corresponding rows from the table. After opening the cursor, we loop over each row of the result table and print out the corresponding columns.  When there are no more rows to be processed, we close the cursor and terminate.  If an error occurs at any point, we generate a suitable error message and stop.
  • 6. SQL Commands for Connecting to a Database  Connection (multiple connections are possible but only one is active) CONNECT TO server-name AS connection-name AUTHORIZATION user-account-info;  Change from an active connection to another one SET CONNECTION connection-name;  Disconnection DISCONNECT connection-name;
  • 7. Embedded SQL in C Programming Examples loop = 1; while (loop) { prompt (“Enter SSN: “, ssn); EXEC SQL select FNAME, LNAME, ADDRESS, SALARY into :fname, :lname, :address, :salary from EMPLOYEE where SSN == :ssn; if (SQLCODE == 0) printf(fname, …); else printf(“SSN does not exist: “, ssn); prompt(“More SSN? (1=yes, 0=no): “, loop); END-EXEC }
  • 8. Embedded SQL in C Programming Examples  A cursor (iterator) is needed to process multiple tuples  FETCH commands move the cursor to the next tuple  CLOSE CURSOR indicates that the processing of query results has been completed
  • 9. prompt("Enter the Department Name: ", dname) ; EXEC SQL select Dnumber into :dnumber from DEPARTMENT where Dname = :dname ; EXEC SQL DECLARE EMP CURSOR FOR select Ssn, Fname, Minit, Lname, Salary from EMPLOYEE where Dno = :dnumber FOR UPDATE OF Salary ; EXEC SQL OPEN EMP ; EXEC SQL FETCH from EMP into :ssn, :fname, :minit, :lname, :salary ; while (SQLCODE == 0) { printf("Employee name is:", Fname, Minit, Lname) ; prompt("Enter the raise amount: ", raise) ; EXEC SQL update EMPLOYEE set Salary = Salary + :raise where CURRENT OF EMP ; EXEC SQL FETCH from EMP into :ssn, :fname, :minit, :lname, :salary ; } EXEC SQL CLOSE EMP ;
  • 10. STATIC SQL VS DYNAMIC SQL  Static SQL is SQL statements in an application that do not change at runtime and, therefore, can be hard-coded into the application.  Static SQL provides performance advantages over dynamic SQL because static SQL is pre-processed, which means the statements are parsed, validated, and optimized only once.  The full text of static SQL statements are known at compilation, which provides the following benefits:  Successful compilation verifies that the SQL statements reference valid database objects.  Successful compilation verifies that the necessary privileges are in place to access the database objects.  Performance of static SQL is generally better than dynamic SQL.
  • 11. STATIC SQL VS DYNAMIC SQL  Static SQL has limitations that can be overcome with dynamic SQL. You may not always know the full text of the SQL statements that must be executed in a PL/SQL procedure. Your program may accept user input that defines the SQL statements to execute, or your program may need to complete some processing work to determine the correct course of action. In such cases, you should use dynamic SQL.  For example, consider a reporting application that performs standard queries on tables in a data warehouse environment where the exact table name is unknown until runtime.  To accommodate the large amount of data in the data warehouse efficiently, you create a new table every quarter to store the invoice information for the quarter. These tables all have exactly the same definition and are named according to the starting month and year of the quarter, for example INV_01_1997, INV_04_1997, INV_07_1997, INV_10_1997, INV_01_1998, etc. In such a case, you can use dynamic SQL in your reporting application to specify the table name at runtime.
  • 12. Dynamic SQL  Dynamic SQL is SQL statements that are constructed at runtime; for example, the application may allow users to enter their own queries. Thus, the SQL statements cannot be hard-coded into the application.  Objective:  Composing and executing new (not previously compiled) SQL statements at run-time  a program accepts SQL statements from the keyboard at run-time  a point-and-click operation translates to certain SQL query  Dynamic update is relatively simple; dynamic query can be complex  because the type and number of retrieved attributes are unknown at compile time
  • 13. DIFFERENCE BETWEEN STATIC AND DYNAMIC SQL  Static (embedded) SQL  In static SQL how database will be accessed is predetermined in the embedded SQL statement.  It is more swift and efficient.  Dynamic (interactive) SQL  In dynamic SQL, how database will be accessed is determined at run time.  It is less swift and efficient.
  • 14.  Static (embedded) SQL  SQL statements are compiled at compile time.  Parsing, validation, optimization, and generation of application plan are done at compile time.  It is generally used for situations where data is distributed uniformly.  Execution IMMEDIATE,EXECUTE and PREPARE statements are not used.  Dynamic (interactive) SQL  SQL statements are compiled at run time.  Parsing, validation, optimization, and generation of application plan are done at run time.  It is generally used for situations where data is distributed non-uniformly.  EXECUTE IMMEDIATE, EXECUTE and PREPARE statements are used.