Home All Groups Group Topic Archive Search About

USE variable database

Author
19 Jun 2006 3:12 AM
harris_da
How can I make an SQL script being executed by SQLCMD to change the
current database context dynamically.  The database to use is passed
through from the batch file, and therefore dynamic within the script.
The script will ultimately create a new database.

Example.

    DECLARE @Database varchar(64);
    SET @Database = '$(Database)';
    IF @Database IS NULL OR RTrim(@Database) = ''
        SET @Database = 'Testing';

    USE @Database;

Author
19 Jun 2006 4:13 PM
Greg Larsen
Here is an article that I wrote that explains how to use variables in sqlcmd
scripts. Hopefully this will get you going.  If not let me know:

http://www.databasejournal.com/features/mssql/article.php/3566401

Show quote
"harris***@yahoo.com.au" wrote:

> How can I make an SQL script being executed by SQLCMD to change the
> current database context dynamically.  The database to use is passed
> through from the batch file, and therefore dynamic within the script.
> The script will ultimately create a new database.
>
> Example.
>
>     DECLARE @Database varchar(64);
>     SET @Database = '$(Database)';
>     IF @Database IS NULL OR RTrim(@Database) = ''
>         SET @Database = 'Testing';
>
>     USE @Database;
>
>
Author
20 Jun 2006 5:28 AM
harris_da
I did try: USE $(Database) but the new [:] commands don't interect very
well within a script, which then causes other issues.  For now, I've
just gone back to XP command batch files which seem to hang most things
together.  Until SQLCMD can integrate [:] commands seemlessly into the
rest of an SQL script, it's very limiting.

Greg Larsen wrote:
Show quote
> Here is an article that I wrote that explains how to use variables in sqlcmd
> scripts. Hopefully this will get you going.  If not let me know:
>
> http://www.databasejournal.com/features/mssql/article.php/3566401
>
> "harris***@yahoo.com.au" wrote:
Author
20 Jun 2006 5:28 AM
harris_da
I did try: USE $(Database) but the new [:] commands don't interect very
well within a script, which then causes other issues.  For now, I've
just gone back to XP command batch files which seem to hang most things
together.  Until SQLCMD can integrate [:] commands seemlessly into the
rest of an SQL script, it's very limiting.

Greg Larsen wrote:
Show quote
> Here is an article that I wrote that explains how to use variables in sqlcmd
> scripts. Hopefully this will get you going.  If not let me know:
>
> http://www.databasejournal.com/features/mssql/article.php/3566401
>
> "harris***@yahoo.com.au" wrote:

AddThis Social Bookmark Button