Declaring a Variable using SUM()

  • What am I doing worng...? I have several SQL training scripts that use variables this way and they all work. Now all of a sudden, this one fails for no real reason... that I can see. Please help the newbie...

    DECLARE @currentBillAt int

    SET @currentBillAt = SUM(bill_at) FROM chg_item where batch_id = 9785

    PRINT @currentBillAt

    It keeps yelling at me about 'FROM'

    Msg 156, Level 15, State 1, Line 3 Incorrect syntax near the keyword 'from'.

    All I want is the total vaules of bill_at where the batch = 9785. Bill_at for that batch_id has 20+ rows that need to be added with the SUM().

    Thank you

  • DECLARE @currentBillAt int

    SELECT @currentBillAt = SUM(bill_at) FROM chg_item where batch_id = 9785

    PRINT @currentBillAt

  • SELECT @currentBillAt = SUM(bill_at) FROM chg_item where batch_id = 9785

    Scott Pletcher, SQL Server MVP 2008-2010

  • Or, if you really want to use SET:

    SET @currentBillAt = (SELECT SUM(bill_at) FROM chg_item where batch_id = 9785 )

    Scott Pletcher, SQL Server MVP 2008-2010

  • This forum is so good at response time. Thank you...

    And on a lighter note, I'm an idiot. Just as my chuckleberry notified me of a new post, I got the sql to work.

    DECLARE @currentBillAt

    SELECT @currentBillAt

    PRINT @currentBillAt

    SET changed to SELECT

    THANK YOU AGAIN for the dedicated support! 😀

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply