Before the CREATE TABLE statements you should put this:
USE Budget
go
> I want my application to create a new database/tables when run for the
> first time. I have created a stored procedure to create the new
> database named "budget". When I run the stored procedure, it creates
> the budget database but the tables are created in the "master"
> database. Please help.
Shiller - 28 Feb 2007 16:07 GMT
> Before the CREATE TABLE statements you should put this:
>
[quoted text clipped - 8 lines]
>
> - Show quoted text -
I tried using the USE statement, but it's not allowed in stored
procedure: "a USE database statement is not allowed in a procedure,
function or trigger."
Greg D. Moore (Strider) - 28 Feb 2007 16:22 GMT
>> Before the CREATE TABLE statements you should put this:
>>
[quoted text clipped - 12 lines]
> procedure: "a USE database statement is not allowed in a procedure,
> function or trigger."
Correct, USE is not really transact-SQL it's a control statement for query
analyzer, etc.
Off hand I'm not sure of a solution to your problem, but google may. I seem
to recall others having similar problems.
Generally though what you want to do is done by a series of scripts, not a
single stored proc.
AlterEgo - 28 Feb 2007 17:10 GMT
Shiller,
Use a three part qualifier in your create table statement:
create table MyDB.dbo.MyTable ...
-- Bill
>>> Before the CREATE TABLE statements you should put this:
>>>
[quoted text clipped - 21 lines]
> Generally though what you want to do is done by a series of scripts, not a
> single stored proc.
Shiller - 28 Feb 2007 17:52 GMT
> Shiller,
>
[quoted text clipped - 31 lines]
>
> - Show quoted text -
Thank you so much Bill, your technique works.
> I want my application to create a new database/tables when run for the
> first time. I have created a stored procedure to create the new
> database named "budget". When I run the stored procedure, it creates
> the budget database but the tables are created in the "master"
> database. Please help.
This is a case where it's better to run statements from the client. After
all, somehow that stored procedure needs to be written somewhere, and I
don't think it's a good idea to write it to the master database.
Alternatively, you can put it a file that you run from OSQL or SQLCMD.

Signature
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx