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
 General SQL Server Forums
 New to SQL Server Programming
 Stored Procedure to update a table(s)

Author  Topic 

rohan_man
Starting Member

8 Posts

Posted - 2007-06-20 : 03:21:38
Hi,

Does anyone know if it's possible to pass the table name to a stored procedure so that it can update a value in the table named.

I need to have one stored procedure which will update data in any table.

If I have stored procedure called UpdateTable, which takes 5 parameters @TableName, @PrimaryKeyName, @PrimaryKeyValue, @ColumnName, @ColumnValue

Then I would have something like

UPDATE @TableName
Set @ColumnName = @ColumnValue
Where @PrimaryKeyName = @PrimaryKeyValue

Cheers

Rohan

nr
SQLTeam MVY

12543 Posts

Posted - 2007-06-20 : 03:27:53
Have a look at dynamic sql.
Also search for articles on why this is a really bad idea.

it would be
exec ('UPDATE [' + @TableName + ']
Set [' + @ColumnName + '] = ''' + @ColumnValue + '
Where [' + @PrimaryKeyName + '] = ''' + @PrimaryKeyValue + '''')

==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-06-20 : 04:16:44
Make sure you read this fully
http://www.sommarskog.se/dynamic_sql.html


Madhivanan

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

rohan_man
Starting Member

8 Posts

Posted - 2007-06-20 : 18:36:33
Thanks for those replies.
That link is very useful and explains exactly what I needed to know

Cheers

Rohan
Go to Top of Page
   

- Advertisement -