Home All Groups Group Topic Archive Search About

Setting the database to"'use" dynamically...

Author
1 Dec 2005 6:41 PM
RSH
I am scripting a stored procedure and I need to change the perspective to a
database that was just created in the script...kind of like this:

use master
go

@db varchar(50);
begin

create database @db
go

use @db -- DOESNT WORK



How do I script this??

Thanks,
Ron

Author
1 Dec 2005 6:54 PM
Aaron Bertrand [SQL Server MVP]
Most DDL statements cannot take parameters as variables, for a variety of
reasons.

http://www.sommarskog.se/dynamic_sql.html





Show quote
"RSH" <way_beyond_o***@yahoo.com> wrote in message
news:ey3BSbq9FHA.3048@TK2MSFTNGP10.phx.gbl...
>I am scripting a stored procedure and I need to change the perspective to a
>database that was just created in the script...kind of like this:
>
> use master
> go
>
> @db varchar(50);
> begin
>
> create database @db
> go
>
> use @db -- DOESNT WORK
>
>
>
> How do I script this??
>
> Thanks,
> Ron
>
>
>

AddThis Social Bookmark Button