SQL
How could I write this in sql stored procedure?
Hi All,
I need to append my queries based on the condition how could i do this in stored procedure
StringBuilder query = new StringBuilder();
**query.AppendLine("Select * from xxx Where 1 =1");**
if(Id != 0)
query.AppendLine("And Id = @Id");
if(Nemae != null)
query.AppendLine("And Name = @Name");
Thanks in Advance..
Raja KrishnamurthyRank #3,615 · 403 reputationPosted Mar 24, 2011, 3:46 AM EDT
SELECT
Id,
Name
FROM
table1
WHERE
(Id = @id or @Id = 0) --Either Id matches the given id or the given Id is 0
And (Name = @Name or @Name='') --Either the Name matches the given name or name is null
HTH.
Happy Programming!!!
Regards,
Raja
Suthish NairRank #92 · 22.8K reputationPosted Mar 24, 2011, 8:21 AM EDT