Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Unexpected query results

Status
Not open for further replies.

Gruuka

Programmer
Aug 27, 2006
24
0
0
NL
Hello, I am facing a problem with one stored procedure that I made using a method in the WHERE clause that worked in other queries.

Code:
ALTER PROCEDURE backend_GetReferrers
	-- Add the parameters for the stored procedure here
	@Filialen_ID		varchar(6) = null,
	@Omschrijving		varchar(100) = null,
	@Actief				tinyint = null,
	@Categorie_ID		int = null
	
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	SELECT *
	From dbo.Referrers rf
	WHERE
		rf.Filialen_ID = case when @Filialen_ID is null then
							rf.Filialen_ID
						 else
							@Filialen_ID
						 end 
		AND rf.Actief = case when @Actief is null then
						rf.Actief
					else
						@actief
					end
		AND rf.Omschrijving like case when @Omschrijving is null then
								rf.Omschrijving
						   else
								 '%' + @Omschrijving + '%'
						   end
		AND rf.ReferrersCategorien_ID = case when @Categorie_ID is null  then
								rf.ReferrersCategorien_ID 
							else
								@Categorie_ID
							end
END

Table layout:
Code:
Referrers_ID	int	        not null
Omschrijving	varchar(100)	not null
ReferrersCategorien_ID	int	null
Filialen_ID	varchar(6)	not null
LinkURL	        varchar(500)	null
Tijdstip	datetime	null
Actief	        tinyint	        null
Klikprijs	smallmoney	null
Maandprijs	smallmoney	null

The problem in this query is at the last AND statement, the values of that column in the table itself is all set to NULL but when I run the stored procedure no rows from the table are returned if I remove the last part I get results of that table.

So.. kind of confused here would be awesome if someone could help me out on this problem.
 
Code:
AND (rf.ReferrersCategorien_ID is null or rf.ReferrersCategorien_ID = case when @Categorie_ID is null  then
                                rf.ReferrersCategorien_ID
                            else
                                @Categorie_ID
                            end)
or
Code:
AND isnull(rf.ReferrersCategorien_ID, 0) = case when @Categorie_ID is null  then
                                isnull(rf.ReferrersCategorien_ID, 0)
                            else
                                @Categorie_ID
                            end
 
Cheers! That worked, but just wondering why is the isnull comparison needed? I mean if the values of the table columns are null shouldn't you be able to select on those values?
 
Nulls are never equal to each other, so cannot be compared. Null means "I don't know what the information is" to the computer

"NOTHING is more important in a database than integrity." ESquared
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top