Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is dynamic SQL?

I just asked an SQL related question, and the first answer was: "This is a situation where dynamic SQL is the way to go."

As I had never heard of dynamic SQL before, I immediately searched this site and the web for what it was. Wikipedia has no article with this title. The first Google results all point to user forums where people ask more or less related questions.

However, I didn't find a clear definition of what a 'dynamic SQL' is. Is it something vendor specific? I work with MySQL and I didn't find a reference in the MySQL handbook (only questions, mostly unanswered, in the MySQL user forums).

On the other hand, I found many references to stored procedures. I have a slightly better grasp of what stored procedures are, although I have never used any. How are the two concepts related? Are they the same thing or does one uses the other?

Basically, what is needed is a simple introduction to dynamic SQL for someone who is new to the concept.

P.S.: If you feel like it, you may have a go at answering my previous question that prompted this one: SQL: How can we make a table1 JOIN table2 ON a table given in a field in table1?

like image 666
augustin Avatar asked Nov 12 '10 13:11

augustin


People also ask

What is dynamic query in SQL with example?

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 is difference between static and dynamic SQL?

Static or Embedded 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.

What is dynamic SQL in stored procedure?

Using dynamic SQL inside stored procedures This stored procedure is used to search for products based on different columns like name, color, productid, and the product number. The dynamic SQL statement is constructed based on the input parameters passed to the stored procedure and is executed by the EXEC command.

How do I run a dynamic SQL query?

SQL Server offers a few ways of running a dynamically built SQL statement. Here are a few options: Writing a SELECT statement or SQL Query with SQL variables. Using EXEC.


1 Answers

Dynamic SQL is merely where the query has been built on the fly - with some vendors, you can build up the text of the dynamic query within one stored procedure, and then execute the generated SQL. In other cases, the term merely refers to a decision made by code on the client (this is at least vendor neutral)

like image 147
Rowland Shaw Avatar answered Oct 12 '22 22:10

Rowland Shaw