Dynamic SQL Tutorial – Introduction, Examples and Query
In this SQL tutorial, we are going to learn about Dynamic SQL. Moreover, we will look at the actual meaning and use of dynamic SQL. Also, we will discuss Dynamic SQL example and query.
So, let us start Dynamic Structured Query Language Tutorial.
2. What is Dynamic SQL?
Dynamic SQL explanations are put away a series of characters that are entered when the program runs. They can be entered by the developer or created by the program itself. However, not at all like static SQL explanations, they are not inserted in the source program. Likewise, as opposed to static SQL statements, dynamic SQL clause can change starting with one execution then onto the next.
Dynamic in SQL clause we can compose by individuals with nearly small programming knowledge, in light of the fact that the program does the greater part of the genuine age of the code. A potential issue decreases the execution (expanded preparing time) if there is excessively dynamic in SQL running at any given time.
Using dynamic SQL is to execute SQL that will conceivably create and execute another SQL articulation. While questioning information, you may need to dynamically set segments you might want to inquiry. Then again, you may need to parameterize tables on which you need to work.
The main point one may think of is to use factors and set them as required segment names or table names. Be that as it may, such an approach isn’t upheld by T-SQL.
DECLARE @tablename AS NVARCHAR(255) = N'dbo.Table'; SELECT * FROM @tablename -- this code will fail
- Dynamic SQL Example –
Table name (FROM clause). Database name (USE clause). Column names (SELECT, WHERE, GROUP BY, HAVING, and ORDER BY clauses). Lists (IN, PIVOT clauses).
3. Use of Dynamic Structured Query Language
Dynamic SQL is an upgraded type of Structured Query Language (SQL) that not at all like standard (or static) SQL, encourages the programmed age and execution of program explanations. This can be useful when it is important to compose code that can change in accordance with fluctuating databases, conditions, or servers. It likewise makes it simpler to robotize errands that rehashes commonly.
Recommended Reading – SQL Subquery
4. Dynamic Query in SQL Examples
An example of putting the query to the string:
DECLARE @query AS NVARCHAR(255) = N'SELECT * FROM dbo.Table'; SELECT @query AS query;
An example of executing the query, which is in the string (dynamic in SQL):
DECLARE @query AS NVARCHAR(255) = N'SELECT * FROM dbo.Table'; EXEC(@query);
- So as we can see, we use the EXEC statement to dynamically execute the query that store in the nvarchar variable.
IF OBJECT_ID('dbo.queryData', 'P') IS NOT NULL DROP PROC dbo.queryData; GO CREATE PROC dbo.queryData @tablename AS NVARCHAR(255) ,@columnnames AS NVARCHAR(255) AS BEGIN DECLARE @SQLString AS NVARCHAR(MAX); SET @SQLString = N'SELECT ' +@columnnames+N' FROM ' + @tablename; EXEC(@SQLString); END EXEC dbo.queryData 'dbo.Table', 'id, firstname, lastname, age'
As the last example, let us study WHERE clause.
USE TSQL2012; GO IF OBJECT_ID('dbo.queryData', 'P') IS NOT NULL DROP PROC dbo.queryData; GO CREATE PROC dbo.queryData @tablename AS NVARCHAR(255) ,@column AS NVARCHAR(255) ,@predicateOperator AS NVARCHAR(255) ,@predicateValue AS NVARCHAR(255) AS BEGIN DECLARE @SQLString AS NVARCHAR(MAX); SET @SQLString = N'SELECT * FROM ' + @tablename + N' WHERE ' + @column + @predicateOperator+@predicateValue ; EXEC(@SQLString); END EXEC dbo.queryData 'dbo.Table', 'age','>=','18'
So, this was all in Dynamic SQL Tutorial. Hope you liked our explanation.
In this tutorial, we learned about the dynamic in SQL. Moreover, we discussed example and query in Dynamic Structured Query Language, Still, if you have any query, ask in the comment tab.
See also –