all groups > sql server (alternate) > february 2007 >
You're in the

sql server (alternate)

group:

Stored procedure to create a new database with tables



Stored procedure to create a new database with tables Shiller
2/28/2007 7:17:23 AM
sql server (alternate): 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.
Re: Stored procedure to create a new database with tables Shiller
2/28/2007 8:07:07 AM
[quoted text, click to view]

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."
Re: Stored procedure to create a new database with tables AlterEgo
2/28/2007 9:10:52 AM
Shiller,

Use a three part qualifier in your create table statement:

create table MyDB.dbo.MyTable ...

-- Bill

[quoted text, click to view]

Re: Stored procedure to create a new database with tables Shiller
2/28/2007 9:52:35 AM
[quoted text, click to view]

Thank you so much Bill, your technique works.
Re: Stored procedure to create a new database with tables Greg D. Moore (Strider)
2/28/2007 4:22:02 PM
[quoted text, click to view]

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.

Re: Stored procedure to create a new database with tables Massimo-Mastino
2/28/2007 4:44:56 PM
Before the CREATE TABLE statements you should put this:

USE Budget
go



"Shiller" <shillerc@gmail.com> ha scritto nel messaggio
news:1172675843.097303.293060@8g2000cwh.googlegroups.com...
[quoted text, click to view]

Re: Stored procedure to create a new database with tables Erland Sommarskog
2/28/2007 10:25:04 PM
Shiller (shillerc@gmail.com) writes:
[quoted text, click to view]

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.


--
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
AddThis Social Bookmark Button