How to add values into a variable?

  • Hi

    I have a cursor that will loop data from rows, and I need to add values into a variable.

    How can I do it?

    For example, in the cursor I have values of a variable @vNOVO_ID_CURSO with:

    1

    2

    3

    4

    5

    and I want to put this values in the variable @acumul_error, so I can returned like this example: @acumul_error = '12345'

    This is the following code:

    if @vID_CURSO_SEL <> @vNOVO_ID_CURSO

    begin

    set @acumul_error = @acumul_error + @vNOVO_ID_CURSO

    end

    The code is not working in the proper way. Can someone give me a feedback?

    Best Regads,

    André.

  • The code is not working in the proper way.

    What is it doing, are you getting an error? It would help if you could post your code, sample data, and DDL. By the way, if you've written a cursor just to do this, you don't need one as you can do this in one SQL statement.

    John Rowan

    ======================================================
    ======================================================
    Forum Etiquette: How to post data/code on a forum to get the best help[/url] - by Jeff Moden

  • it should work with some minor tweaks:

    after you've done the DECLARE @acumul_error varchar(100) or whatever it is, you need to initialize it as an empty string:

    SET @acumul_error =''

    in the cursor, i'm betting one of the values(@vNOVO_ID_CURSO) is an integer, so you might need to convert it:

    set @acumul_error = @acumul_error + CONVERT(VARCHAR(30),@vNOVO_ID_CURSO)

    then your code in your cursor should work fine...otherwise ti was concatenating NULL + '1'

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • Lowell (7/16/2009)


    it should work with some minor tweaks:

    after you've done the DECLARE @acumul_error varchar(100) or whatever it is, you need to initialize it as an empty string:

    SET @acumul_error =''

    in the cursor, i'm betting one of the values(@vNOVO_ID_CURSO) is an integer, so you might need to convert it:

    set @acumul_error = @acumul_error + CONVERT(VARCHAR(30),@vNOVO_ID_CURSO)

    then your code in your cursor should work fine...otherwise ti was concatenating NULL + '1'

    Thanks. It works!

    All I had to do was to initialize the variable outside the cursor.

    Best Regards,

    André.

  • lopes80andre (7/16/2009)


    Lowell (7/16/2009)


    it should work with some minor tweaks:

    after you've done the DECLARE @acumul_error varchar(100) or whatever it is, you need to initialize it as an empty string:

    SET @acumul_error =''

    in the cursor, i'm betting one of the values(@vNOVO_ID_CURSO) is an integer, so you might need to convert it:

    set @acumul_error = @acumul_error + CONVERT(VARCHAR(30),@vNOVO_ID_CURSO)

    then your code in your cursor should work fine...otherwise ti was concatenating NULL + '1'

    Thanks. It works!

    All I had to do was to initialize the variable outside the cursor.

    Best Regards,

    André.

    If I may... no matter what you're doing in this cursor, it is slow. It looks like you may be trying to do a running total of accumulated errors. My recommendation would be to post the table you're trying to do the running total with, some sample data in a readily consumable format, and a description of what the business process is that you're trying to accomplish.

    Please see the first link in my signature line below for how to easily do all of that.

    And, to wet your whistle, if it is a running total problem, we can show you how to do it on a million rows in somewhere between 3 and 7 seconds... all without a cursor.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.
    "Change is inevitable... change for the better is not".

    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)
    Intro to Tally Tables and Functions

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

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