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..Loading
Suthish NairPosted Mar 24, 2011, 12:21 PM
Raja KrishnamurthyPosted Mar 24, 2011, 7:46 AM
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