I\'m trying to switch the current database with a SQL statement. I have tried the following, but all attempts failed:
-- If you are using a variable for the database name.
-- Try something like this.
DECLARE @DBName varchar(50)
Set @DBName = 'Database1'; /* could be passed in by a parameter. */
IF( @DBName = 'Database1')
Begin
USE [Database1];
SELECT FROM Table1;
End
IF( @DBName = 'Database2')
Begin
USE [Database2];
SELECT FROM Table2;
End
IF( @DBName is null)
Begin
USE [Database1];
End