views:

59

answers:

2

i have an table where i am passing 5 paramter

table name=" image"
parameter
@imageID,
@imageName,
@imageDirectory,
@imageResource,

from the front end [from page that is from textbox control] if they didnot send any values to stored procedue then i should do an simple Query condition1:

 select  * From image

condition 2: if they send any value to these above parameter then collect those vlaues and retrive result based on the parameted sent

condition 3: here they might send values to any of the of the parameter in that condition select values for that parameter . for the parameter which they have not send values we can send 'null' or ' '

so based on that retrive the result

so how write an Stored procedure for such an condition. handling all the three condition

anyhelp would be really great thank you

+5  A: 

Try something along these lines:

SELECT * FROM IMAGE i
WHERE 1=1 
AND (@ImageID IS NULL OR i.ImageId = @ImageID )
AND (@ImageName IS NULL OR i.ImageName = @ImageName )
AND (@ImageDirectory IS NULL OR i.ImageDirectory = @ImageDirectory)
AND (@ImageResource IS NULL OR i.ImageResource = @ImageResource)
Stephen Wrighton
A: 

You can also use the IsNull(transact-SQL specific) or Coalesce (SQL-92 standard) function, as in:

 Select * From Table
 Where imageId = Coalesce(@ImageId, imageId)
    And imageName = Coalesce(@imageName,imageName)
    etc.
Charles Bretana