We help IT Professionals succeed at work.
Get Started

Can I use the keyword "if" whil using cte

Mehram
Mehram asked
on
295 Views
Last Modified: 2012-05-11
Sql server 2005

The code written (snippet id = 8085622) has syntax error.

Can I use if keyword while using cte.

I can hadle the situation by using temp table but I am only interested if is it possible to use if with cte directly.

My pois is , to directly use a CTE immediately followed by an IF-ELSE construct. ...
declare @trans_no varchar(100)
;with vchr_balance as ( 
 select  debit = sum(debit) , credit = sum(credit)
     from  gl_vchr_det 
     where trans_no  = @trans_no 
)
if (select sum(debit) from vchr_balance) <> (select sum(debit) from vchr_balance)
    begin
      SET @msg = 'Voucher is not balanced? ' 
             + 'Debit amt = '  + convert(varchar,convert(money,(select sum(debit) from vchr_balance)),103)
             + 'Credit amt = ' + convert(varchar,convert(money,(select sum(credit) from vchr_balance)),103)
        RAISERROR (@msg,16,1)
end

Open in new window

Comment
Watch Question
Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009
Commented:
This problem has been solved!
Unlock 1 Answer and 3 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE