Thread: Stored proc

    #1
  1. superficial
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2002
    Location
    Peterborough, England
    Posts
    188
    Rep Power
    13

    Stored proc


    I have written a stored procedure with a cursor, to loop a distinct set of names and grab associated data on them

    How can I insert summed columns in a stored procedure, for example,

    if i declare @total as an integer and then do select @total = sum(field)

    if i then try an insert into statement, with @total - it does not allow me to execute.

    whats the best way to get around this and can anybody explain the reasons why this doesnt work, im sure there are good ones!
    _______________
    Matt
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jan 2003
    Location
    Paris Uppland
    Posts
    2,382
    Rep Power
    391
    You probably don't need a cursor at all. It sounds like what you are trying to do could be accomplished by

    Code:
    insert into <your  table>
    select name,sum(field) from <your joined tables>
    group by name
    If you need further help you have to shed some more details.
  4. #3
  5. superficial
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2002
    Location
    Peterborough, England
    Posts
    188
    Rep Power
    13
    ok, my cursor selects 1 distinct name from a view.

    then, for each distinct name thats returned, im selecting data from another table for that name.

    this works perfectly - but, i want to insert summed fields into a new table, rather than just display them
    _______________
    Matt

IMN logo majestic logo threadwatch logo seochat tools logo