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 2005 Forums
 Transact-SQL (2005)
 T-Sql Dynamic Query

Author  Topic 

casablanca007
Starting Member

1 Post

Posted - 2007-01-25 : 12:53:34
Hi Everybody.
My problem is this:
I have a table named T_Corrisp, and this table contain 2 columnes, the first is named "Table" and the second is named "Field" like the example below:

Table Field
--------- -----------
Employers Name
States KMQ
Books Title
Cars brand

That i would to do is to open this table in a cursor from T-SQL and for every record execute a query like this:
update Employers set Name = Null

logically, the query that i need must be dynamic like this:
update @Table set @Field = Null

is it possible to execute a dynamic query, where i can change the table or tha field with a variable?
Someone can help me please.
Thank you for your attention. (^_^)

snSQL
Master Smack Fu Yak Hacker

1837 Posts

Posted - 2007-01-25 : 13:49:03
Here's an example

declare @sql nvarchar(2000)
declare @Table varchar(20), @Field varchar(20)
select @Table = 'Employers', @Field = 'Name'
set @sql = N'UPDATE [' + @Table + '] SET [' + @Field + '] = NULL'
exec sp_executesql @sql
Go to Top of Page
   

- Advertisement -