I notice a strange thing during doing some stored procedures
I can explain that by the following example :
DECLARE @FileExtensionID int
SELECT * FROM FileExtensions WHERE (Name= 'pdf')
SELECT @FileExtensionID = ID FROM FileExtensions WHERE (Name= 'pdf')
SELECT IsNULL( @FileExtensionID , 0) -- First Select
SELECT * FROM FileExtensions WHERE (Name= '')
SELECT @FileExtensionID = ID FROM FileExtensions WHERE (Name= '')
SELECT IsNULL( @FileExtensionID , 0)-- Second Select
in the above queries i have a table called “FileExtensions” with ID ,Name columns and i try to set ID in variable @FileExtensionID, the problem that in the first select
–SELECT IsNULL(@FileExtensionID,0)– @FileExtensionID have a vaild ID – for exmaple 9 -but in the second one when no row returned from
SELECT @FileExtensionID = ID FROM FileExtensions WHERE (Name= '')
it also still keep previous one – 9 – and not return 0
i don’t know why it happen so please could any tell me how it come ?
When a query returns no rows, the assignment
@FileExtensionID = IDis not performed, which is why the variable still contains the value that was assigned earlier.