Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
USE with a variable
Message
 
 
À
22/03/2011 12:21:15
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Scripting
Versions des environnements
SQL Server:
SQL Server 2008
Application:
Web
Divers
Thread ID:
01504526
Message ID:
01504533
Vues:
45
>The only way I have found to do this is by executing everything into a command:
>
>
>DECLARE @Command nvarchar(max)
>DECLARE @DestinationDatabase char(40)
>
>SET @DestinationDatabase='Test'
>
>SET @Command='USE '+@DestinationDatabase
>
>EXECUTE(@Command)
>
>
>If someone has a simpler way, please, let us know.

There is no simpler way (unless changing connection string to use correct database). Dynamic tables / databases require dynamic SQL. Answered the exact same question in almost the same way here
http://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/ecc2e569-5a05-41e0-9201-de0a1dcd9870

Although you may need to create the whole script dynamically, not just USE statement.
If it's not broken, fix it until it is.


My Blog
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform