Click here to Skip to main content
15,898,988 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My requirement is where clause is different based in my condition in singe SP. So that i prepared where clause in c# and i passed that Where Clause as input parameter if my Sp. how to bind select statement with where condition(Here where condition is my input parameter)
Posted

Unless you can write a SP that is generic enough to handle all possible conditions, you will have to build a SQL string somewhere.

The easiest way is to build it in C# and run it from there.

If you must use a SP, you will have to build a statement string in SQL and execute it using the EXEC statement:

http://msdn.microsoft.com/en-us/library/ms188332.aspx[^]

Nick
 
Share this answer
 
Hi Ramesh,


i think it helpful for you.


CREATE PROCEDURE DynamicWhereClause
(
@input varchar(100)
)

AS

BEGIN

DECLARE @sqlCommand varchar(1000)
SET @sqlCommand = 'SELECT * FROM Employee ' + @input
EXEC (@sqlCommand)

END

Examples

SQL
EX 1):  EXEC DynamicWhereClause @input = "WHERE FirstName = 'venky' AND LastName = 'palepu' AND Country = 'India' "

EX 2):EXEC DynamicWhereClause @input = "WHERE FirstName = 'venky'  "




Thanks
venkat
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900