Answered step by step
Verified Expert Solution
Question
1 Approved Answer
CREATE PROC uspProductSearch @ProductName VARCHAR(32) = NULL AS BEGIN DECLARE @SQL NVARCHAR(MAX) SELECT @SQL = ' SELECT ProductID, ProductName=Name, Color, ListPrice ' + CHAR(10)+ '
CREATE PROC uspProductSearch @ProductName VARCHAR(32) = NULL
AS
BEGIN
DECLARE @SQL NVARCHAR(MAX)
SELECT @SQL = ' SELECT ProductID, ProductName=Name, Color, ListPrice ' + CHAR(10)+
' FROM Production.Product' + CHAR(10)+
' WHERE 1 = 1 ' + CHAR(10)
IF @ProductName IS NOT NULL
SELECT @SQL = @SQL + ' AND Name LIKE @pProductName'
PRINT @SQL
-- parametrized execution
EXEC sp_executesql @SQL, N'@pProductName varchar(32)', @ProductName
END
GO
-- Execute dynamic SQL stored procedure with parameter
EXEC uspProductSearch '%bike%'
- What kind of dynamic SQL it is? (such as passing input / output parameters or concatenating the user inputs, etc.)
- Explain the problem?
- Is this dynamic sql efficient or not? Why?
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started