sql server查询显示错误语法接近订单

时间:2016-11-28 09:10:47

标签: sql-server

if 1=(select oneTimePayClass from tblClass where division=@CDivision) 
    print 'True';
else
    (select h.FeeHeadId,h.FeeHeadName,d.amount,d.FeeDefaultId 
    from tblFeeHead as h inner join tblFeeDefault as d on h.feeHeadId=d.feeHeadId 
    where d.InstituteId=@InstituteId and d.Standard=@Standard and d.Division=@Division and d.AcademicYear=@year and h.oneTimePay=@payFalse 
    order by h.orderNr);

当我删除order by h.orderNr时,查询会成功运行。

但是当我添加order by h.orderNr时,它会显示:

  

错误语法接近订单

1 个答案:

答案 0 :(得分:1)

ORDER BY子句在视图,内联函数,派生表,子查询和公用表表达式中无效。

这样做

if 1=(select oneTimePayClass from tblClass where division=@CDivision) 
    print 'True';
else
    select h.FeeHeadId,h.FeeHeadName,d.amount,d.FeeDefaultId 
    from tblFeeHead as h inner join tblFeeDefault as d on h.feeHeadId=d.feeHeadId 
    where d.InstituteId=@InstituteId and d.Standard=@Standard and d.Division=@Division and d.AcademicYear=@year and h.oneTimePay=@payFalse 
    order by h.orderNr;