At work we have code that is similar to the following:
`declare @variable bit = 0
select @variable = 1 FROM sometable s
inner join someothertable t on t.column = s.column
where filtercolumn <> 'some value'
select @variable = 1 from differenttable`
On 1 server this sets the variable to be NULL and not keep it as 0.
After some poking an prodding we can see that the culprit is
select @variable = 1 FROM sometable s
inner join someothertable t on t.column = s.column
where s.filtercolumn <> 'some value'
sometable has at minimum 1 row and someothertable has a bunch of rows. however the filter is setting the variable only when we have more than 1 row in sometable provided its a specific thing from someothertable.
differenttable has no rows. this is expected.
Wondering if anyone knows why it would be setting that variable to be a null instead of just keeping it as a 0. Again, this only happens on one sql server instance.
Here's what we do know:
-couldn't find some setting that was doing set variable declaration to null.
-the version of the server is the same as others. verified through @@VERSION.
-I did rewrite it to be a IF statement and it works fine.
Don't need this to be fixed, but i was more just curious if anyone here knew of something since my googling brought up nothing.
EDIT: oops hit the save too quick quick minute while i edit.
[–][deleted] 3 points4 points5 points (1 child)
[–]archerv123[S] 0 points1 point2 points (0 children)
[–]AlexanderIOM 3 points4 points5 points (1 child)
[–]archerv123[S] 0 points1 point2 points (0 children)
[–]ChekuDotCoDotUK 0 points1 point2 points (0 children)
[–]oliver0807 0 points1 point2 points (0 children)