Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a dynamic SQL query, and when would I want to use one?

What is a dynamic SQL query, and when would I want to use one? I'm using SQL Server 2005.

like image 377
Surya sasidhar Avatar asked Feb 03 '10 06:02

Surya sasidhar


People also ask

When should you use dynamic SQL?

You should use dynamic SQL in cases where static SQL does not support the operation you want to perform, or in cases where you do not know the exact SQL statements that must be executed by a PL/SQL procedure. These SQL statements may depend on user input, or they may depend on processing work done by the program.

What is a dynamic SQL query?

Dynamic SQL is a programming technique that could be used to write SQL queries during runtime. Dynamic SQL could be used to create general and flexible SQL queries. Syntax for dynamic SQL is to make it string as below : 'SELECT statement';

What are the advantages of dynamic SQL?

The advantage of using dynamic SQL statements is more flexibility in coding an application, flexibility for a user to find the information they want formatted the way they need, and expansion without adding more stored procedures.

When would you want to use SQL?

SQL, or Structured Query Language, allows users to access and manipulate databases. It also able to retrieve data from a database, execute queries against a database, and create content in existing databases, such as tables, records, or views. SQL is one of the most popular programming languages in existence.


2 Answers

Here's a few articles:

  • Introduction to Dynamic SQL
  • Dynamic SQL Beginner's Guide

From Introduction to Dynamic SQL:

Dynamic SQL is a term used to mean SQL code that is generated programatically (in part or fully) by your program before it is executed. As a result it is a very flexible and powerful tool. You can use dynamic SQL to accomplish tasks such as adding where clauses to a search based on what fields are filled out on a form or to create tables with varying names.

like image 140
Kyle Rosendo Avatar answered Oct 13 '22 19:10

Kyle Rosendo


Dynamic SQL is SQL generated by the calling program. This can be through an ORM tool, or ad-hoc by concatenating strings. Non-dynamic SQL would be something like a stored procedure, where the SQL to be executed is predefined. Not all DBA's will let you run dynamic SQL against their database due to security concerns.

like image 45
Kevin Stafford Avatar answered Oct 13 '22 19:10

Kevin Stafford