如何从SQL查询中设置变量?

时间:2010-10-20 04:02:50

标签: sql sql-server tsql

我正在尝试从SQL查询中设置变量:

declare @ModelID uniqueidentifer

Select @ModelID = select modelid from models
where areaid = 'South Coast'

显然我没有这样做,因为它不起作用。有人可以提出解决方案吗?

谢谢!

9 个答案:

答案 0 :(得分:445)

使用SELECT:

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

使用SET:

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

警告

如果此select语句返回多个值(开头不好):

  • 使用SELECT时,会为变量分配最后返回的值(如womp所述),没有任何错误或警告(这可能会导致逻辑错误)
  • 使用SET时,会出现错误

答案 1 :(得分:27)

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

如果您的select语句返回多个值,则会为您的变量分配返回的最后一个值。

有关将SELECT与变量一起使用的参考:http://msdn.microsoft.com/en-us/library/aa259186%28SQL.80%29.aspx

答案 2 :(得分:26)

declare @ModelID uniqueidentifer

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

select @ModelID

答案 3 :(得分:11)

我更喜欢从声明语句中设置它

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

答案 4 :(得分:9)

如果查询返回多行,请使用TOP 1

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

答案 5 :(得分:7)

有三种方法:

  1. DECLARE
  2. SET-微软推荐的方法
  3. SELECT

以下查询详细介绍了每种方法的优缺点:

-- 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

答案 6 :(得分:3)

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

在这种情况下,如果您返回了两个或更多结果,那么您的结果就是最后一条记录。因此,如果您可能还没有返回两条记录,请注意这一点,因为您可能看不到预期的结果。

答案 7 :(得分:1)

要使用SQL ASSIGN变量选择最佳做法,如下所示

->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;

如果您必须在一行中分配多个变量,则可以使用同一SELECT INTO

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

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

--HAPPY CODING-- 

答案 8 :(得分:0)

您可以使用它,但是请记住您的查询给出1个结果,多个结果将引发异常。

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>

<label>Type</label>
<select id="type">
  <option> Type 01</option>
  <option> Type 02</option>
</select>

<label>Amount</label>
<input type="text" id="amount" />
<button id="addRow"> Add </button>

<table id="example" class="display" cellspacing="0" width="100%">
  <thead>
    <tr>
      <th>Type</th>
      <th>Amount</th>

    </tr>
  </thead>
</table>

另一种方式:

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