dbTalk Databases Forums  

Stored procedure to create a new database with tables

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss Stored procedure to create a new database with tables in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Shiller
 
Posts: n/a

Default Stored procedure to create a new database with tables - 02-28-2007 , 09:17 AM






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.


Reply With Quote
  #2  
Old   
Massimo-Mastino
 
Posts: n/a

Default Re: Stored procedure to create a new database with tables - 02-28-2007 , 09:44 AM






Before the CREATE TABLE statements you should put this:

USE Budget
go



"Shiller" <shillerc (AT) gmail (DOT) com> ha scritto nel messaggio
news:1172675843.097303.293060 (AT) 8g2000cwh (DOT) googlegroups.com...
Quote:
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.




Reply With Quote
  #3  
Old   
Shiller
 
Posts: n/a

Default Re: Stored procedure to create a new database with tables - 02-28-2007 , 10:07 AM



On Feb 28, 10:44 am, "Massimo-Mastino" <mast... (AT) hotmail (DOT) it> wrote:
Quote:
Before the CREATE TABLE statements you should put this:

USE Budget
go

"Shiller" <shill... (AT) gmail (DOT) com> ha scritto nel messaggionews:1172675843.097303.293060 (AT) 8g2000cwh (DOT) googlegroups.com...



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.- Hide quoted text -

- 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."



Reply With Quote
  #4  
Old   
Greg D. Moore \(Strider\)
 
Posts: n/a

Default Re: Stored procedure to create a new database with tables - 02-28-2007 , 10:22 AM



"Shiller" <shillerc (AT) gmail (DOT) com> wrote

Quote:
On Feb 28, 10:44 am, "Massimo-Mastino" <mast... (AT) hotmail (DOT) it> wrote:
Before the CREATE TABLE statements you should put this:

USE Budget
go

"Shiller" <shill... (AT) gmail (DOT) com> ha scritto nel
messaggionews:1172675843.097303.293060 (AT) 8g2000cwh (DOT) googlegroups.com...



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.- Hide quoted text -

- 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."

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.




Reply With Quote
  #5  
Old   
AlterEgo
 
Posts: n/a

Default Re: Stored procedure to create a new database with tables - 02-28-2007 , 11:10 AM



Shiller,

Use a three part qualifier in your create table statement:

create table MyDB.dbo.MyTable ...

-- Bill

"Greg D. Moore (Strider)" <mooregr_deleteth1s (AT) greenms (DOT) com> wrote

Quote:
"Shiller" <shillerc (AT) gmail (DOT) com> wrote in message
news:1172678827.112925.18770 (AT) k78g2000cwa (DOT) googlegroups.com...
On Feb 28, 10:44 am, "Massimo-Mastino" <mast... (AT) hotmail (DOT) it> wrote:
Before the CREATE TABLE statements you should put this:

USE Budget
go

"Shiller" <shill... (AT) gmail (DOT) com> ha scritto nel
messaggionews:1172675843.097303.293060 (AT) 8g2000cwh (DOT) googlegroups.com...



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.- Hide quoted text -

- 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."


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.




Reply With Quote
  #6  
Old   
Shiller
 
Posts: n/a

Default Re: Stored procedure to create a new database with tables - 02-28-2007 , 11:52 AM



On Feb 28, 12:10 pm, "AlterEgo" <altereg... (AT) dslextreme (DOT) com> wrote:
Quote:
Shiller,

Use a three part qualifier in your create table statement:

create table MyDB.dbo.MyTable ...

-- Bill

"Greg D. Moore (Strider)" <mooregr_deletet... (AT) greenms (DOT) com> wrote in messagenews:K_hFh.7399$tD2.4682 (AT) newsread1 (DOT) news.pas.earthlink.net...



"Shiller" <shill... (AT) gmail (DOT) com> wrote in message
news:1172678827.112925.18770 (AT) k78g2000cwa (DOT) googlegroups.com...
On Feb 28, 10:44 am, "Massimo-Mastino" <mast... (AT) hotmail (DOT) it> wrote:
Before the CREATE TABLE statements you should put this:

USE Budget
go

"Shiller" <shill... (AT) gmail (DOT) com> ha scritto nel
messaggionews:1172675843.097303.293060 (AT) 8g2000cwh (DOT) googlegroups.com...

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.- Hide quoted text -

- 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."

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.- Hide quoted text -

- Show quoted text -
Thank you so much Bill, your technique works.



Reply With Quote
  #7  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: Stored procedure to create a new database with tables - 02-28-2007 , 04:25 PM



Shiller (shillerc (AT) gmail (DOT) com) writes:
Quote:
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.


--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.