This might help some who have had problems with Dynamic SQL and handling multiple conditions- this worked for me:
CREATE PROCEDURE MatchConditions
@id int,
@op1 varchar(75),
@op2 varchar(75),
@op3 varchar(75),
@value1 varchar(75),
@value2 varchar(75),
@value3 varchar(75)
--MatchConditions 1,'<','=','>','1600','3/23/2010','14.25'
AS
-- Dynamic SQL Example
-- with multiple parameters
DECLARE @sqlCommand nvarchar(1000)
DECLARE @columnList varchar(75)
--DECLARE @value1 varchar(75)
--DECLARE @value2 varchar(75)
--DECLARE @value3 varchar(75)
--DECLARE @op1 varchar(75)
--DECLARE @op2 varchar(75)
--DECLARE @op3 varchar(75)
--DECLARE @id int
--set @id=1
SET @columnList = 'id,Condition1, Condition1, Condition3'
SET @value1 = '1530'
SET @value2 = '3/22/2009'
SET @value3 = '14.4'
SET @op1 = '='
SET @op2 = '>'
SET @op3 = '>'
SET @sqlCommand = 'SELECT ' + @columnList + ' FROM Conditional WHERE condition1 ' + @op1 + @value1 + ' and condition2 ' + @op2 + @value2 + ' and condition3 ' + @op3 + @value3 + ' and id=' + cast(@id as varchar(50))
print @sqlCommand
EXECUTE sp_executesql @sqlCommand, N'@value1 nvarchar(75), @value2 nvarchar(75),@value3 nvarchar(75)',@value1,@value2,@value3
GO
Be the first to rate this post
- Currently 0/5 Stars.
- 1
- 2
- 3
- 4
- 5