I need to write stored procedure with dynamic where condition for filtering some records. The filter parameters are given by the front end application.
Thanks
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Anupam SinghPosted Jun 5, 2014, 4:03 AM
you can create procedure with parameter ex:
CREATE PROCEDURE GetAddress @City varchar(30)
You just need to change city in parameter.
in C# you can achieve this using:
Hope will help.
Jignesh TrivediPosted Jun 5, 2014, 3:59 AM
Hi,
To do this, I think you have to write dynamic query.
like
Create proc Test
( @where varchar(max)
)
AS
BEGIN
declare @sql varchar(max)
set @sql = 'select * from tablename where ' + @where
exec(@sql)
END
also refer
http://www.codeproject.com/Articles/21234/Implementing-Dynamic-WHERE-Clause-in-Static-SQL
hope this will help you