Hello Techies,
Dynamic SQL is a technique that allows for the generation and execution of SQL statements at runtime based on unknown parameters or conditions until runtime. One way to implement dynamic SQL is through a stored procedure, which is a pre-written set of SQL statements that can be called and executed repeatedly.
This blog will discuss dynamic SQL stored procedures and provide an example of how they can be used.
Creating a Dynamic SQL Stored Procedure
It would be best if you first defined the input parameters and output variables to create a dynamic SQL stored procedure. These will be used to pass in the dynamic SQL statement and any additional parameters needed. For example:
CREATE PROCEDURE dynamic_sql_sp
@sql_statement nvarchar(max),
@param1 int,
@param2 varchar(50),
@output_table table (
id int,
name varchar(50)
) OUTPUT
AS
BEGIN
-- Dynamic SQL statement to be executed
DECLARE @sql nvarchar(max)
SET @sql = '
SELECT id, name
FROM my_table
WHERE col1 = @param1
AND col2 = @param2
'
-- Execute dynamic SQL statement with input parameters
EXEC sp_executesql @sql, N'@param1 int, @param2 varchar(50)', @param1, @param2
-- Insert results into output table
INSERT INTO @output_table (id, name)
SELECT id, name
FROM my_table
WHERE col1 = @param1
AND col2 = @param2
END
In this example, the dynamic SQL statement is defined as a string variable (@sql), and the sp_executesql stored procedure is used to execute the statement with the input parameters (@param1 and @param2). The results are then inserted into an output table variable (@output_table).
Using a Dynamic SQL Stored Procedure
To use the dynamic SQL stored procedure, you need to call it with the appropriate input parameters. For example:
DECLARE @results table (
id int,
name varchar(50)
)
EXEC dynamic_sql_sp
@sql_statement = '',
@param1 = 1,
@param2 = 'example',
@output_table = @results OUTPUT
SELECT * FROM @results
In this example, the dynamic SQL statement is left blank (@sql_statement = ''), as it is defined within the stored procedure itself. The input parameters (@param1 and @param2) are specified, and an output table variable (@results) is declared to receive the stored procedure results.
Let's Take another example and create a stored procedure with dynamic SQL queries to understand better how it is implemented.
Let's consider an example of a dynamic SQL stored procedure that generates a report of sales data for a given time period and product category. The stored procedure will take input parameters for the start and end dates and the product category. It will generate a report of total sales revenue and quantity sold for each product in the specified category.
Here is the code for creating the dynamic SQL stored procedure:
CREATE PROCEDURE sp_sales_report
@start_date DATE,
@end_date DATE,
@product_category NVARCHAR(50)
AS
BEGIN
DECLARE @sql NVARCHAR(MAX)
SET @sql = '
SELECT
p.product_name,
SUM(s.sales_quantity) AS total_quantity,
SUM(s.sales_quantity * p.unit_price) AS total_revenue
FROM
sales s
INNER JOIN products p ON s.product_id = p.product_id
WHERE
s.sales_date >= @start_date
AND s.sales_date <= @end_date
AND p.product_category = @product_category
GROUP BY
p.product_name
ORDER BY
total_revenue DESC
'
EXEC sp_executesql @sql, N'@start_date DATE, @end_date DATE, @product_category NVARCHAR(50)', @start_date, @end_date, @product_category
END

Saurabh TiwariPosted Apr 30, 2023, 7:05 AM
Thanx for sharing sql