Back

Explore Courses Blog Tutorials Interview Questions
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?

Thanks!

1 Answer

0 votes
by (40.4k 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.

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

28.4k questions

29.7k answers

500 comments

94.2k users

Browse Categories

...