Click here to Skip to main content
16,017,881 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

In my stored procedure, user can give input as only serviceId and some time both serviceid and personId,

I tired like this, But it is not coming, Please help me.

SQL
WHERE StatusId != 3 AND

(@ServiceId IS NULL OR ServiceId=@ServiceId)

AND (PersonId IS NULL OR PersonId=@PersonId)


Regards
Nagaraj.J
Posted
Updated 26-May-14 8:52am
v2

Try another atsign.
Change
SQL
AND (PersonId IS NULL OR PersonId=@PersonId)
To
SQL
AND (@PersonId IS NULL OR PersonId=@PersonId)
 
Share this answer
 
v2
Comments
Member 10454499 26-May-14 14:51pm    
Now, only passing two values the data is coming.
Try dynamic query:

SQL
DECLARE @sql VARCHAR(MAX)

SET @sql = 'SELECT <FieldList> FROM TableName WHERE StatusId !=3'

IF (@ServiceId IS NULL) 
    SET @sql = @sql + ' AND ServiceId=' + @ServiceId
 
IF (@PersonId IS NULL) 
    SET @sql = @sql + ' AND PersonId=' + @PersonId

EXEC(@sql)


For furter information, please see:
Building Dynamic SQL In a Stored Procedure[^]
How to Generate T-SQL Code by Using Dynamic SQL?[^]
Dynamic ORDER BY with multiple criteria in T-SQL[^]
 
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