James, solution will just fine. However, we use a slightly different pattern to help avoid situations where a query could return more than one row (thus assigning a random value to the variable). With this pattern SQL will error if more than one vaue is returned and it is mroe ovbious what you are trying to achomplish (i.e.: you want a zero result and not a null):DECLARE @Foo TABLE (ID INT, Val VARCHAR(20))
DECLARE @Bar INT
SET @Bar = COALESCE
(
(
SELECT ID
FROM @Foo
WHERE Val = 'Test'
)
, 0
)
SELECT @Bar