Mayur
Mayur

Reputation: 3

sql server query show error incorrect syntax near order

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

When I remove order by h.orderNr then the query runs successfully.

But when I add order by h.orderNr then it shows:

error incorrect syntax near order

Upvotes: 0

Views: 1064

Answers (1)

anon
anon

Reputation: 865

The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions.

Do it like this

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;

Upvotes: 1

Related Questions