0 votes
1 view
in SQL by (20.3k points)

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?


1 Answer

0 votes
by (40.4k points)
edited by

By using SELECT you can write the query this way:

SELECT @ModelID = m.modelid 


WHERE m.areaid = 'South Coast'

Are you interested in learning SQL from the basics! Refer to this video on SQL provided by Intellipaat:

By using SET you can write the query like this:

SET @ModelID = (SELECT m.modelid 


WHERE m.areaid = 'South Coast')

Note: Don’t use the select statement if it returns multiple values.

When you use SELECT, the last value gets assigned to the variable and that will be returned without any warning or error.

Welcome to Intellipaat Community. Get your technical queries answered by top developers !