Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Using variable in a UPDATE Statement

Author  Topic 

jhnegrao
Yak Posting Veteran

81 Posts

Posted - 2006-10-25 : 07:11:03
Hello, All!

I'm trying to update a specific table named Requests, it' s a dinamic table, So I cannot use the name of the columns directly in a Update statement. I need to get this information and create the update statement in the moment of the transaction.
Is it possible? My query is in a Stored Procedure.
I want something like that.

Update <Table> SET @variable = 'NomedoCliente' WHERE ......conditions

If anybody knows, I would be great.

Thanks

Juliano Horta

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2006-10-25 : 07:29:28
I think you are stuck with your dynamic SQL solution.

SET @sqlcmd = 'UPDATE ' + @MyTable + ' SET ' + @MyColumn + ' = ''NomedoCliente WHERE ' + conditions.....



Peter Larsson
Helsingborg, Sweden
Go to Top of Page

jhnegrao
Yak Posting Veteran

81 Posts

Posted - 2006-10-25 : 08:18:16
Hi, Peso!

Thank you very much for your answer. It was soo good.
How can I execute it after putting it to a variable?
I need to execute this update.

Thanks

Juliano Horta


quote:
Originally posted by Peso

I think you are stuck with your dynamic SQL solution.

SET @sqlcmd = 'UPDATE ' + @MyTable + ' SET ' + @MyColumn + ' = ''NomedoCliente WHERE ' + conditions.....



Peter Larsson
Helsingborg, Sweden

Go to Top of Page

mr_mist
Grunnio

1870 Posts

Posted - 2006-10-25 : 08:20:32
exec @sqlcmd

or use sp_executesql

-------
Moo. :)
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-10-25 : 08:20:38
http://www.sommarskog.se/dynamic_sql.html

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2006-10-25 : 08:37:29
SET @sqlcmd = 'UPDATE [' + @MyTable + '] SET [' + @MyColumn + '] = ''NomedoCliente'' WHERE conditions.....'
exec (@sqlcmd)


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

jhnegrao
Yak Posting Veteran

81 Posts

Posted - 2006-10-25 : 09:06:58
Hello, everybody!

Thank you very much. All answers was important for me.
Now, My query is executing properly.

Have a nice day

Juliano Horta
Go to Top of Page
   

- Advertisement -