Friday, March 9, 2012

Dynamically Choose Database

I have to write a large data migration script to move data from one SQL Server database to another. Is there any way to dynamically specify the server and database name? I would like to do something like the following, (but this does not seem to work):

Delete From [@.ServerName].[@.ImportToDatabase].[MyTable]

Any help appreciatedno. you can't do that.

EXECUTE 'DELETE FROM [' + @.ServerName + '].[' + @.ImportToDatabase + '].[username].[MyTable]'|||For some reason this doesn't seem to work in my code. However using:

EXEC ('DELETE FROM [' + @.ServerName + '].[' + @.ImportToDatabase + '].[username].[MyTable]' )

Does work.

Thanks for your comments

No comments:

Post a Comment