Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
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?

Thanks!

1 Answer

0 votes
by (40.7k points)
edited by

By using SELECT you can write the query this way:

SELECT @ModelID = m.modelid 

FROM MODELS m

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 

 FROM MODELS m

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.

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...