Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more: , +
i tried this example it's not working properly

SQL
SELECT *
FROM YourTable
WHERE (@mParameter = 'M' OR @mParameter IS NULL)
AND (@tParameter = 'T' OR @tParameter IS NULL)
AND (@gParameter = 123 OR @gParameter IS NULL)
AND (@nParameter = 'N' OR @nParameter IS NULL)



can any one tell me how to handle null values in StoredProceures



thanks in advance
Posted
Comments
ArunRajendra 9-Jul-13 2:01am    
You need to apply this parameters to fields where are those? This query will not have any impact on the results as records are not getting filtered.

SQL
SELECT FName, LName
FROM tblAccounts
WHERE
  fname like (case when @fname = '' then fname else @fname end) and
  lname like (case when @lname = '' then lname else @lname end)



LIKE this above example i solved my Problem


thanks to all............
 
Share this answer
 
That logical condition is never going to work: WHERE operates on the row data, and you are purely asking to to decide based on parameter data which will not change from row to row - so it will either return all rows or no rows - it cannot filter the row data at all.

Probably, you want to use the parameters to compare against row data - but without knowing more about your system it's not possible to be more exact.
However, I would move the IS NULL outside my SELECT anyway:
SQL
DECLARE @P VARCHAR(10)
SET @P = ISNULL(@mParameter, 'M')

SELECT * FROM MyTable WHERE @P ='M'
But what you probably want to do is more along the lines of:
SQL
DECLARE @P VARCHAR(10)
SET @P = ISNULL(@mParameter, 'M')

SELECT * FROM MyTable WHERE Model=@P
 
Share this answer
 
You have not used the Columns in the where conditions inside the query.
SQL
WHERE (@mParameter = 'M' OR @mParameter IS NULL)
AND (@tParameter = 'T' OR @tParameter IS NULL)
AND (@gParameter = 123 OR @gParameter IS NULL)
AND (@nParameter = 'N' OR @nParameter IS NULL)


Here it should be Columns, not parameters.
SQL
WHERE (Column1 = 'M' OR Column1 IS NULL)
AND (Column2  = 'T' OR Column2  IS NULL)
AND (Column3  = 123 OR Column3  IS NULL)
AND (Column4  = 'N' OR Column4  IS NULL)
 
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