If you do not have an entry for ID 1589, then there will be no entry in the DELETED table, if you have one, then it should return 1589.
So, if you haven't, I think it just returns nothing, because this statement has no input line:
SELECT CAST(ISNULL([Id], -1) AS BIGINT) AS N'RetValId' FROM @result;
(If you selected * from @result, there should be no lines)
The second returns -1, because first you set a variable that gets NULL after selection.
DECLARE @mi BIGINT; SET @mi = (SELECT [Id] FROM @result)
(If you select only @mi after this, then it should be NULL)
I think this explanation
UPDATED:
Can you try a little trick to get it without another available:
SELECT CAST(ISNULL(MAX([ID]),-1) AS BIGINT) AS N'RetValId' FROM @result;
Due to MAX, the insie statement will be NULL, so here's the trick. If something has been deleted, then there will be an ID. Hope this helps.
source share