Assigning value to a variable

  • Hi

    Does anyone know how to assign the value of a compute sum(field) to a variable??

    Thanks

    Ritch

    *I didn't do anything it just got complicated*


    "I didn't do anything it just got complicated" - M Edwards

  • Hi Ritch,

    I think this is what you need :

    Declare @Store Int

    SELECT @Store = Sum(Field) from Table

    One thing to consider - assuming that Field is of data type int and the sum of all Field values exceeds the maximum value for Int data type then you might have to rewrite it as :

    Declare @Store Numeric(28,3)

    SELECT @Store = Sum (Convert(Numeric(28,3),Field)) from Table

    i.e cast the Sum and the variable to a datatype that can hold the result...

  • Thanks Winash

    *I didn't do anything it just got complicated*


    "I didn't do anything it just got complicated" - M Edwards

Viewing 3 posts - 1 through 2 (of 2 total)

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