TBohnen.jnr
TBohnen.jnr

Reputation: 5119

SQL Difference Between Parameter and value

Sorry if my heading was a bit misleading but not entirely sure how to phrase it.

Why would there be a difference between the below two statements:

SELECT * 
FROM tbl1 LEFT OUTER JOIN 
      tbl2 ON tbl1.num = tbl2.tbl1Num LEFT OUTER JOIN
      tbl3 ON tbl2.num = tbl3.tbl2Num
WHERE tbl2.intNum = 123 OR 123 = -1

AND

DECLARE @intNum int = 123
SELECT * 
FROM tbl1 LEFT OUTER JOIN 
      tbl2 ON tbl1.num = tbl2.tbl1Num LEFT OUTER JOIN
      tbl3 ON tbl2.num = tbl3.tbl2Num
WHERE tbl2.intNum = @intNum OR @intNum = -1

We ran the above two queries and it actually gave us different results? We ended up changing it to a Union to solve the problem but I would really like to understand why this happened.

Thanks in advance!

Upvotes: 0

Views: 286

Answers (1)

Albireo
Albireo

Reputation: 11095

You're missing the parameter type:

declare @intNum int = 123

Upvotes: 2

Related Questions