Dynamic SQL in Database
Management Systems
PRESENTED BY:
Avinash K
Suriyavarson
SQL stands for Structured Query
Language
Used to create, retrieve, update, and
delete data in databases
Introduction Two main types:
to SQL:
Static SQL – query fixed at compile time
Dynamic SQL – query built and executed
at runtime
What is Dynamic SQL?
• SQL statements constructed and executed during
program execution
• Allows programs to build queries based on user
input or conditions
• Useful when structure of query is not known in
advance
o Example:
• EXECUTE IMMEDIATE 'SELECT * FROM
students WHERE marks > ' ||
user_input;
Static vs Dynamic
SQL:
Feature Static SQL Dynamic SQL
When
At compile time At runtime
defined
Flexibility Fixed query Query can change
Performance Faster Slightly slower
Search filters,
Example use Fixed reports user-driven
queries
Why Use Dynamic SQL?
• Allows flexible applications
• Handles variable search
conditions
• Enables metadata-driven
programming
• Used in stored procedures and
report generation.
How Dynamic SQL
Works?
Example (in SQL):
• DECLARE
• table_name VARCHAR2(20) := 'STUDENTS';
• sql_query VARCHAR2(200);
• BEGIN
• sql_query := 'SELECT * FROM ' || table_name;
• EXECUTE IMMEDIATE sql_query;
• END;
Advantages:
• Very flexible for dynamic data
retrieval
• Can handle different database
structures
• Useful in automated or
configurable applications
Disadvantages:
• Slightly slower performance
• Security risks (SQL Injection) if
user input is not validated
• Harder to debug and maintain
Conclusion:
• Dynamic SQL allows runtime query
generation
• Balances flexibility and security
• Widely used in modern applications
and stored procedures