tags:

views:

89

answers:

2

I want to do this

(s.Reference = @Reference or @Reference = '')

but I get an sql error saying unable to convert nvarchar to bigint. What's going on there. I just want to skip that query on the Refernce if the input parameter is an empty string.

+1  A: 

It looks as though Reference is a numeric field. In this case, you are probably looking to do:

(s.Reference = @Reference or @Reference is null)

When you put ='' into the test condition, you make an assumption that it's a string.

Pete OHanlon
A: 

For SQL Server, the best decision would be this:

SELECT  *
FROM    mytable
WHERE   s.Reference = @Reference
UNION ALL
SELECT  *
FROM    mytable
WHERE   @Reference IS NULL

It will optimize one of the queries away and execute only the remaining one (using the index if needed).

Quassnoi