I'm trying to set a variable from a SQL query:
declare @ModelID uniqueidentifer
Select @ModelID = select modelid from models
where areaid = 'South Coast'
Obviously I'm not doing this right as it doesn't work. Can somebody suggest a solution?
Thanks!
SELECT @ModelID = m.modelid
FROM MODELS m
WHERE m.areaid = 'South Coast'
SET @ModelID = (SELECT m.modelid
FROM MODELS m
WHERE m.areaid = 'South Coast')
See this question for the difference between using SELECT and SET in TSQL.
If this select statement returns multiple values (bad to begin with):
SELECT
, the variable is assigned the last value that is returned (as womp said), without any error or warning (this may cause logic bugs)SET
, an error will occur