Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

What is the difference between a dynamic and a static query in SQL?

user-image
Question added by Nidhi Goel , IOT Lead : Smart Meters , DEWA
Date Posted: 2017/09/05
NASSIM ZITOUN
by NASSIM ZITOUN , Senior database administrator. , Sonatrach petroleum corporation –Production division

Static SQL is SQL statements in an application that do not change at runtime.

Dynamic SQL is an enhanced form of Structured Query Language (SQL) that, unlike standard (or static) SQL, facilitates the automatic generation and execution of program statements

Amit Tiwari
by Amit Tiwari , Technical Solution Consultant , Statefarm Insurance

Static SQL is SQL statements in an application that do not change at runtime and, therefore, can be hard-coded into the application. Dynamic SQL is SQL statements that are constructed at runtime; for example, the application may allow users to enter their own queries. so, the SQL statements cannot be hard-coded into the application.

surajo yunusa
by surajo yunusa , webmaster , Ahmadu Bello University Zaria

When you use static SQL, you cannot change the form of SQL statements unless you make changes to the program. However, you can increase the flexibility of static statements by using host variables.

Example: In the following example, the UPDATE statement can update the salary of any employee. At bind time, you know that salaries must be updated, but you do not know until run time whose salaries should be updated, and by how much. 01 IOAREA. 02 EMPID PIC X(06). 02 NEW-SALARY PIC S9(7)V9(2) COMP-3. ⋮ (Other declarations) READ CARDIN RECORD INTO IOAREA AT END MOVE 'N' TO INPUT-SWITCH. ⋮ (Other COBOL statements) EXEC SQL UPDATE DSN8B10.EMP SET SALARY = :NEW-SALARY WHERE EMPNO = :EMPID END-EXEC. The statement (UPDATE) does not change, nor does its basic structure, but the input can change the results of the UPDATE statement. Flexibility of dynamic SQL

What if a program must use different types and structures of SQL statements? If there are so many types and structures that it cannot contain a model of each one, your program might need dynamic SQL.

You can use one of the following programs to execute dynamic SQL: Db2 Query Management Facility™ (QMF™) Provides an alternative interface to Db2 that accepts almost any SQL statement

Safyan Gulzar
by Safyan Gulzar , Software Project Manager (Project Based) , Gracesol Technologies

Static is hard codded and not changealbe during execution and it is less flexible but the dynamic query is complied run time and more flexible 

MOHAMED OMER ALHAG ALI ALHAGALI
by MOHAMED OMER ALHAG ALI ALHAGALI , Senoir Oracle Database Administrator , SABIC

static sql query is not changed during execution time is hardcodeddynamic sql query : the query structure can be changed during execution time either by end user or other object

Avaneesh Chauhan
by Avaneesh Chauhan , Transport Operations , Linfox logistics India Pvt ltd

Structured Query Language and  facilitates the automatic generation

Shahbaz Ali
by Shahbaz Ali , Oracle Developer , Masood Textile Mills Ltd

Static vs Dynamic SQL

Static SQL

its parsed, validated and optimized once before execution.

Main advantage that runtime overhead is removed.

Main Disadvantage is that the query, which was parsed, validated and optimized once may not remain when in run later.

Dynamic SQL

Its parsed, validated and optimized at runtime as its called.

Main Pro is that all required info is up to dated.

Main Cons that each time the query is to be parsed, validated and optimized before execution.

HOSNI CHERIF
by HOSNI CHERIF , Chef de projet étude et développement , Diffusion Informatique Internationale

Static SQL are SQL statements in an application that do not change at runtime and, therefore, can be hard-coded into the application. Dynamic SQL is SQL statements that are constructed at runtime; for example, the application may allow users to enter their own queries.

Dynamic SQL is a programming technique that enables you to build SQL statements dynamically at runtime. You can create more general purpose, flexible applications by using dynamic SQL because the full text of a SQL statement may be unknown at compilation.

Udayan Mukherjee
by Udayan Mukherjee , ITSM EXECUTIVE , PCCW SOLUTIONS INSYS LTD

Static SQL is fixed at runtime while Dynamic SQL gives the flexibility to customise according to needs at run time and it is not hard coded.

PHILIP ROHAN EKANAYAKA
by PHILIP ROHAN EKANAYAKA , Project Manager , Altice USA

Dynamic query allows the end-uers or a procedure to load query parameters at runtime. Static is hardcoded.

Deleted user
by Deleted user

Static SQL statements in an applications do not change at runtime so they are hard coded in application.

Dynamic SQL statements in an applications can be embeded at run time i.e users can enter their own queries.

More Questions Like This