基本SQL顺序按查询

时间:2013-04-04 16:32:05

标签: sql

以下查询正常工作,并根据需要返回正确的结果

select top 1 SID from vSTable where (ID in 
(select ID from Engine where Name='nnn' OR Name='mmm') 
and QuestionTypeID=1 and IsOfficial=1) order by vSTable.ITime desc;

但是当我像这样使用它时

declare @SVal as int

set @SVal = select top 1 SID 
            from vSTable 
            where (
              ID in 
                  (select ID from Engine where Name='nnn' OR Name='mmm') 
              and QuestionTypeID=1 
              and IsOfficial=1
            ) order by vSTable.ITime desc; 

我在select stmt

附近得到错误的错误语法

2 个答案:

答案 0 :(得分:1)

将select语句包装在括号中

declare @SVal as int

set @SVal = (select top 1 SID 
            from vSTable 
            where (
              ID in 
                  (select ID from Engine where Name='nnn' OR Name='mmm') 
              and QuestionTypeID=1 
              and IsOfficial=1
            ) order by vSTable.ITime desc); 

答案 1 :(得分:1)

你的语法有点偏。这应该有效:

  select @SVal = (select top 1 SID from vSTable where (ID in 
  (select ID from Engine where Name='nnn' OR Name='mmm') 
  and QuestionTypeID=1 and IsOfficial=1) order by vSTable.ITime desc)

请注意,您可以使用SET或SELECT进行该分配。