This problem is of course me not understanding SQL 2008 on an expert level but I really don't understand this scenario.
If I have the below query - and ID 9 does not exist:
declare @tmpInt int = 5
select @tmpInt=coalesce(id,0) from MyView where id=9
print @tmpInt
Why is this printing 5
as set initially!? I would expect 0
- or maybe even null
?
If I do the same query on an existing row, it will naturally set the ID of that row.
UPDATE
There has been two answers already but maybe I need to clarify - how can I get the coalesce
value of 0
in to my variable, if no rows exists? Is it possible? Otherwise I would need to initialize the @tmpInt
variable before each query (I am using @tmpInt
in many queries).