Using local variable to make TSQL Update query

  • Hello

    I have the following TSQL update statment and i want to use a local variable to set these value on some fields , but they don´t work :

    use focintegra

    declare @conta as varchar(15)

    set @conta = '62103'

    select @conta

    go

    update cpoo set

    /*Base Incidencia MN*/

    ve11=@conta

    ve21=ve11

    ve31=ve11

    ve41=ve11

    ve51=ve11

    from cpoo where cpoo.cpoo like '999999'

    Someone could help me

    Many thanks

    Luis Santos

  • Not too sure of what you want to do nor what is the problem, but give this version a try!

    use focintegra

    declare @conta as varchar(15)

    set @conta = '62103'

    select @conta

    go

    update cpoo set

    /*Base Incidencia MN*/

    ve21=@conta

    ve31=@conta

    ve41=@conta

    ve51=@conta

    ve11=@conta

    from cpoo where cpoo.cpoo like '999999'

  • Hello

    i try your example but the same return this following error

    Must declare the scalar variable "@conta"

    Well the purpose is that i want to replace fields ve21,ve31,ve41,ve51 and ve11 on my table cpoo with the same value for cpoo.cpoo like '999999' and to do this i was declare a local variable = "62103" to replace theses fields values with my variable.

    Thanks

    Luis Santos

  • My exemple will work, just remove the go.

     

    BTW this is why the code samples you post should also work when posted .

  • I'm thinking somebody is going to need a comma or two in there

    --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)

  • Yup, that's what happens when you want to help but don't have the time to test the final solution.

     

    Who said that if you don't have time to do it right the first time, then when will have have time to redo it right?

  • Heh... my boss' boss says that all the time... it's why our projects seem to go in so fast... who cares if it takes six months extra to fix what we put it?  After all, the user are the best testers, right?

    --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)

  • That's one way to do things... I preffer my way .

Viewing 8 posts - 1 through 7 (of 7 total)

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