Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 163 Vote(s) - 3.45 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to set variable from a SQL query?

#1
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!
Reply

#2
SELECT @ModelID = modelid
FROM Models
WHERE areaid = 'South Coast'


If your select statement returns multiple values, your variable is assigned the last value that is returned.

For reference on using SELECT with variables:

[To see links please register here]

Reply

#3
declare @ModelID uniqueidentifer

--make sure to use brackets
set @ModelID = (select modelid from models
where areaid = 'South Coast')

select @ModelID
Reply

#4
I prefer just setting it from the declare statement

DECLARE @ModelID uniqueidentifer = (SELECT modelid
FROM models
WHERE areaid = 'South Coast')
Reply

#5
Select @ModelID =m.modelid
From MODELS m
Where m.areaid = 'South Coast'

In this case if you have two or more results returned then your result is the last record. So be aware of this if you might have two more records returned as you might not see the expected result.
Reply

#6
Use `TOP 1` if the query returns multiple rows.

SELECT TOP 1 @ModelID = m.modelid
FROM MODELS m
WHERE m.areaid = 'South Coast'
Reply

#7
You can use this, but remember that your query gives 1 result, multiple results will throw the exception.

declare @ModelID uniqueidentifer
Set @ModelID = (select Top(1) modelid from models where areaid = 'South Coast')

Another way:

Select Top(1)@ModelID = modelid from models where areaid = 'South Coast'

Reply

#8
There are three approaches:

1. [DECLARE](

[To see links please register here]

)
2. [SET](

[To see links please register here]

) -- Microsoft Recommended approach
3. [SELECT](

[To see links please register here]

)

Below query details the advantage and disadvantage of each:

```sql
-- First way,
DECLARE @test int = (SELECT 1)
, @test2 int = (SELECT a from (values (1),(2)) t(a)) -- throws error

-- advantage: declare and set in the same place
-- Disadvantage: can be used only during declaration. cannot be used later

-- Second way
DECLARE @test int
, @test2 int

SET @test = (select 1)
SET @test2 = (SELECT a from (values (1),(2)) t(a)) -- throws error

-- Advantage: ANSI standard.
-- Disadvantage: cannot set more than one variable at a time

-- Third way
DECLARE @test int, @test2 int
SELECT @test = (select 1)
,@test2 = (SELECT a from (values (1),(2)) t(a)) -- throws error

-- Advantage: Can set more than one variable at a time
-- Disadvantage: Not ANSI standard
```
Reply

#9
#To ASSIGN variables using a SQL select the best practice is as shown below#


->DECLARE co_id INT ;
->DECLARE sname VARCHAR(10) ;

->SELECT course_id INTO co_id FROM course_details ;
->SELECT student_name INTO sname FROM course_details;


###IF you have to assign more than one variable in a single line you can use this same SELECT INTO ###

->DECLARE val1 int;
->DECLARE val2 int;

->SELECT student__id,student_name INTO val1,val2 FROM student_details;

--HAPPY CODING--








Reply

#10
Using `SELECT`
-

SELECT @ModelID = m.modelid
FROM MODELS m
WHERE m.areaid = 'South Coast'

Using `SET`
-


SET @ModelID = (SELECT m.modelid
FROM MODELS m
WHERE m.areaid = 'South Coast');

Then you can use `SELECT` to show the value of `@ModelID` or use the variable into your code.
-
SELECT @ModelID

[See this question for the difference between using SELECT and SET in TSQL][1].

Warning
-
If this `SELECT` statement returns *multiple values* (bad to begin with):

- When using `SELECT`, the variable is assigned the last value that is returned (as womp said), without any error or warning (this may cause logic bugs)
- The `SET` query returns error only if you DON'T put the semicolon in the end of query

[1]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through