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


Posted in: Technology mostly .net  Tags:

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Related posts

Comments

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview


 
September 5. 2010 20:40

no site

Calendar

«  September 2010  »
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910
View posts in large calendar

Tag Cloud

Recent Posts

Recent Comments

Banners

Theme Grabber
Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2010 Mark Kendall's Blog