Dynamic SQL vs Embedded SQL


SQL is a structured query language, which is the ultimate computer language, to manipulate and retrieve data stored on a related website.

SQL is the standard language for Relational Database Systems. SQL queries can be of two types namely Dynamic SQL and Embedded or Static SQL.

What is Dynamic SQL?


Dynamic SQL is a programming system that allows you to create and execute dynamic SQL statements during operation. Users can answer their questions in other applications. These statements are compiled during operation. These types of SQL statements are used where there are differences in the data stored on the website. It is more flexible compared to Embedded SQL and can be used in other flexible applications because the full text of the SQL statement may not be known when merged. For example, Flexible SQL lets you create a table operating system whose name is unknown until it is run.

Since integration is done during operation, the system will only be able to access the website during operation. Therefore, no proper planning and performance plan is required in advance. This will reduce system performance. Also, if you take a website query from a user during operation, then there is a chance that users may enter the wrong queries and this is very dangerous because here you are dealing with a lot of data.

You can think of powerful SQL as statements prepared in any programming language. With that said, using this process, we can write down any question, from the simplest to the most complex. You can use this process outside of SQL to create queries and submit them for use, or within the database to create queries based on the given parameters. The biggest advantage we have here is flexibility because you can control everything — what will be the SELECT and that WHERE parts of the question, as well as the conditions — variable test that includes or excludes conditions, adjusting variable input values ​​based on specific conditions, etc.

What is Embedded SQL?

Embedded SQL

Embedded or static SQL are those SQL statements that are modified and cannot be changed during application. These statements are compiled during compilation only. The user knows how to make statements as SQL statements are with the user, so SQL queries can be improved and can be done in the best and fastest way. The data access method is pre-defined and these standard SQL statements are commonly used on those websites that are evenly distributed.

Embedded SQL involves the placement of SQL language structures in the programming language code. Using Pro * Ada, SQL statements are embedded directly into the Ada program, and the source is merged.

Pre-integration translates Embedded SQL into calls to Pro * Ada operating time library processes that manage the connection between the program and the Oracle server. After previous integration, simply merge the resulting source files using the standard, supported Ada compiler, and build the application in the normal way. Pro * Ada provides all the necessary library processes. The user should add this to the standard Ada default library.

These statements have a strong code in the application, so to build an application where there is a need for flexible or duration SQL statements, Dynamic SQL statements must be used.

Embedding queries sometimes cause problems as they will be difficult and lead to error processing when used in a large under-production application.

Embedded SQL Example

Dynamic SQL applications

If the SQL statement format is not known before writing or using the program, Dynamic SQL is a good choice.

In cases where Embedded SQL does not support performing any function, or in cases where the user is unaware of the SQL statements to be used in the PL / SQL process, Dynamic SQL should be used. These SQL statements may be based on user input, or they may be based on processing system performance.

The following are some common situations where Dynamic SQL can be used.

1. To Execute Dynamic DML Statements

2. To Execute Dynamic Queries

3. To Reference Database Objects that Do Not Exist at Compilation

4. To Optimize Execution Dynamically

5. To Invoke Dynamic PL/SQL Blocks

6. To Perform Dynamic Operations Using Invoker-Rights

Embedded SQL applications

Embedded SQL helps when in industry-level applications there is a need for well-connected systems that can download data from the site and present it to the user. SQL queries are embedded in high-level languages ​​so that they can easily make a meaningful part of the analysis.

Some of the outstanding examples of the languages ​​in which SQL is embedded are the following:

• C ++

• Java

• Python etc.

Need for Embedded SQL

Embedded SQL gives you the freedom to use the database if necessary. Once the app has been developed, it goes into production mode for a few things that need to be taken care of. One key factor is the issue of authorization and downloading and supply of data to / from the website. With the help of embedding queries, the database can be used without creating any major code. With embedded SQL, customized APIs can download and supply data if needed.

Advantages of Dynamic SQL

Predicate Optimization: The real benefit of dynamic SQL is that the execution plans generated for each invocation of the query will be optimized for the predicates that are actually being used at that moment. The main issue with the static SQL solutions, aside from maintainability, was that the additional predicates confused the query optimizer, causing it to create inefficient plans. Dynamic SQL gets around this issue by not including anything extra in the query.

Single Query Plan Caching: For every stored procedure there is one cached query plan and an additional ad hoc plan caches for each invocation of the stored procedure (this can be verified using the view sys.dm_exec_cached_plans). This means that every time a new argument is passed to the stored procedure, a compilation occurs, which is clearly going to kill performance. The dynamic query is not being parameterized and is therefore producing duplicate query plans for different arguments.

Disadvantages of Dynamic SQL

Speed: Dynamic SQL tends to be slower than embedded SQL, as SQL Server must generate an execution plan every time at runtime.

Permissions: Dynamic SQL requires the users to have direct access permissions on all accessed objects like tables and views. Generally, users are given access to the stored procedures which reference that tables, but not directly on the tables. In this case, dynamic SQL will not work.

Syntax: One distinct advantage of writing stored T-SQL procedures is that you get a syntax check directly. With dynamic SQL, a trivial syntax error may not show up until run time. Even if you test your code carefully, there may be some query or some variation of a query, that is only run in odd cases and not covered in your test suite.

Advantages of Embedded SQL

Small footprint database: As embedded SQL uses an UltraLite database engine compiled specifically for each application, the footprint is generally smaller than when using an UltraLite component, especially for a small number of tables. For a large number of tables, this benefit is lost.

High performance: Combining the high performance of C and C++ applications with the optimization of the generated code, including data access plans, makes embedded SQL a good choice for high-performance application development.

Extensive SQL support: With embedded SQL you can use a wide range of SQL in your applications.

Disadvantages of Embedded SQL

Knowledge of C or C++ required: If you are not familiar with C or C++ programming, you may wish to use one of the other UltraLite interfaces. UltraLite components provide interfaces from several popular programming languages and tools.

Complex development model: The use of a reference database to hold the UltraLite database schema, together with the need to preprocess your source code files, makes the embedded SQL development process complex. The UltraLite components provide a much simpler development process.

SQL must be specified at design time: Only SQL statements defined at compile time can be included in your application. The UltraLite components allow dynamic use of SQL statements.

The choice of development model is guided by the needs of your particular project, and by the programming skills and experience available.

Difference between Dynamic and Embedded SQL

Conclusion: If you want to create a flexible application you can use dynamic SQL, but make sure your users are professional and trained. If not, you should go to static or embedded SQL. This works very well compared to the powerful SQL.

That’s it for this blog.

Do share this blog with your friends to spread the knowledge.

Keep Learning :)

Written By:

Group -48 TY-2021–22

Group Members:

Somnath More

Ayush Patil

Mukta Pawar

Raghav Bansal



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store