Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
DB Engine
SQL ServerMSDESQL Server CE
Services
Analysis (Data Mining)Analysis (OLAP)DTSIntegration ServicesNotification ServicesReporting Services
Programming
CLRConnectivitySQLXML
Other Technologies
ClusteringEnglish QueryFull-Text SearchReplicationService Broker
General
Data WarehousingPerformanceSecuritySetupSQL Server ToolsOther SQL Server Topics
DirectoryUser Groups
Related Topics
MS AccessOther DB ProductsMS Server Products.NET DevelopmentVB DevelopmentJava DevelopmentMore Topics ...

SQL Server Forum / General / Other SQL Server Topics / February 2007

Tip: Looking for answers? Try searching our database.

Stored procedure to create a new database with tables

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Shiller - 28 Feb 2007 15:17 GMT
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.
Massimo-Mastino - 28 Feb 2007 15:44 GMT
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.
Erland Sommarskog - 28 Feb 2007 22:25 GMT
> 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

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.