Dynamic SQL And Embedded SQL
Dynamic SQL
- What is Dynamic SQL?
For example, dynamic SQL allows to create a procedure that operates on a table whose name is not known until runtime.
- Why need Dynamic SQL?
Static SQL statements do not change from execution to execution. 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.
- When to use Dynamic SQL?
- To Execute Dynamic DML Statements:
In PL/SQL, you cannot execute the following types of statements using static SQL:
- Data definition language (DDL) statements, such as
CREATE,DROP,GRANT, andREVOKE - Session control language (SCL) statements, such as
ALTERSESSIONandSETROLE
You can use dynamic SQL to create applications that execute dynamic queries, which are queries whose full text is not known until runtime. Many types of applications need to use dynamic queries, including:
- Applications that allow users to input or choose query search or sorting criteria at runtime
- Applications that allow users to input or choose optimizer hints at run time
- Applications that query a database where the data definitions of tables are constantly changing
- Applications that query a database where new tables are created often
- How to Use Dynamic SQL to query in a table?
- First, declare two variables,
@tablefor holding the name of the table from which you want to query and@sqlfor holding the dynamic SQL. - Second, set the value of the
@tablevariable toproduction.products(this table stores information about product such as name, brand , category, etc). - Third, construct the dynamic SQL by concatenating the
SELECTstatement with the table name parameter. - Fourth, call the
sp_executesqlstored procedure by passing the@sqlparameter.
Putting The queries together:
DECLARE@table NVARCHAR(128),@sql NVARCHAR(MAX);SET @table = N'production.products';SET @sql = N'SELECT * FROM ' + @table;EXEC sp_executesql @sql;
EMBEDDED SQL
- What is embedded SQL in DBMS?
By using the connectors we can easily run SQL queries on the existing databases in our RDBMS or we can even create new ones.
- What is embedded SQL?
We embed SQL queries into high-level languages such that they can easily perform the logic part of our analysis.
Some of the prominent examples of languages with which we embed SQL are as follows:
- C++
- Java
- Python etc.
- Why do we need embedded SQL?
Embedded SQL gives us the freedom to use databases as and when required. Once the application we develop goes into the production mode several things need to be taken care of.
We need to take care of a thousand things out of which one major aspect is the problem of authorization and fetching and feeding of data into/from the database.
With the help of the embedding of queries, we can easily use the database without creating any bulky code. With the embedded SQL, we can create API’s which can easily fetch and feed data as and when required.
- How to embed SQL in high level languages?
For using embedded SQL, we need some tools in each high-level language. In some cases, we have inbuilt libraries which provide us with the basic building block.
While in some cases we need to import or use some packages to perform the desired tasks.
For example, in Java, we need a connection class. We first create a connection by using the connection class and further we open the connection bypassing the required parameters to connect with the database.
EXAMPLE: HOW TO CONNECT TO A DATABASE (USING JAVA)
Comments
Post a Comment