473,804 Members | 2,225 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Stored procedure to create a new database with tables

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.

Feb 28 '07 #1
6 13055
Before the CREATE TABLE statements you should put this:

USE Budget
go

"Shiller" <sh******@gmail .comha scritto nel messaggio
news:11******** **************@ 8g2000cwh.googl egroups.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.

Feb 28 '07 #2
On Feb 28, 10:44 am, "Massimo-Mastino" <mast...@hotmai l.itwrote:
Before the CREATE TABLE statements you should put this:

USE Budget
go

"Shiller" <shill...@gmail .comha scritto nel messaggionews:1 1************** ********@8g2000 cwh.googlegroup s.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."

Feb 28 '07 #3
"Shiller" <sh******@gmail .comwrote in message
news:11******** *************@k 78g2000cwa.goog legroups.com...
On Feb 28, 10:44 am, "Massimo-Mastino" <mast...@hotmai l.itwrote:
>Before the CREATE TABLE statements you should put this:

USE Budget
go

"Shiller" <shill...@gmail .comha scritto nel
messaggionews: 11************* *********@8g200 0cwh.googlegrou ps.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.
Feb 28 '07 #4
Shiller,

Use a three part qualifier in your create table statement:

create table MyDB.dbo.MyTabl e ...

-- Bill

"Greg D. Moore (Strider)" <mo************ ****@greenms.co mwrote in message
news:K_******** *********@newsr ead1.news.pas.e arthlink.net...
"Shiller" <sh******@gmail .comwrote in message
news:11******** *************@k 78g2000cwa.goog legroups.com...
>On Feb 28, 10:44 am, "Massimo-Mastino" <mast...@hotmai l.itwrote:
>>Before the CREATE TABLE statements you should put this:

USE Budget
go

"Shiller" <shill...@gmail .comha scritto nel
messaggionews :11************ **********@8g20 00cwh.googlegro ups.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.

Feb 28 '07 #5
On Feb 28, 12:10 pm, "AlterEgo" <altereg...@dsl extreme.comwrot e:
Shiller,

Use a three part qualifier in your create table statement:

create table MyDB.dbo.MyTabl e ...

-- Bill

"Greg D. Moore (Strider)" <mooregr_delete t...@greenms.co mwrote in messagenews:K_* *************** *@newsread1.new s.pas.earthlink .net...
"Shiller" <shill...@gmail .comwrote in message
news:11******** *************@k 78g2000cwa.goog legroups.com...
On Feb 28, 10:44 am, "Massimo-Mastino" <mast...@hotmai l.itwrote:
Before the CREATE TABLE statements you should put this:
>USE Budget
go
>"Shiller" <shill...@gmail .comha scritto nel
messaggionews: 11************* *********@8g200 0cwh.googlegrou ps.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.

Feb 28 '07 #6
Shiller (sh******@gmail .com) writes:
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, es****@sommarsk og.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
Feb 28 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
8663
by: Robin Tucker | last post by:
Is it possible to execute a stored procedure in one database, which then itself executes a stored procedure from another database? We have decide to split our data into a tree structure (DB1) and data blobs (DB2) (we are using MSDE and we have a 2gb limit with each DB so we've done it this way for that reason). I would like to, say, execute a stored procedure in DB1, passing in the data blob and other details, DB1 will create a tree node...
4
13473
by: marc | last post by:
I've been developing a stored procedure that uses a user defined function in the query portion of the procedure. However, since the end product needs to allow for dynamic table names, the UDF will not work. I've been trying to get this to work with converting the UDF to a procedure, but I'm having no luck. Here is the background on what I'm trying to accomplish. I need to perform a sub-identity on a table, I have the normal identity set,...
5
4021
by: Thanos | last post by:
Hello, Im a novice at this so bare with me. I have two tables which will be match merged based on 10 conditions (select statements). For example, if a record exists in table A but not in Table B then that record will be inserted into a separate audit table called Table C lets say! So based on this 10 conditions or select statements that I wrote I would like to create a stored procedure but im not sure how to begin. Any help would be...
2
3337
by: Eli | last post by:
Hi all We currently have a strange problem with calling a Stored Procedure (SQL Database) in our C# Project. The only error I get is "System error" which says a lot :) Background: We have several stored procedures to Insert and update datas in our SQL database. Some stored procedures are smaller (insert datas in only one table) and some of them are quite big (insert datas in several
2
4498
by: Joe Campbell | last post by:
I have a DBA that wrote a stored procedure that does a SELECT from a particluar SQL Server table. Within that stored procedure he links over to grab a column from another database table. I need to display this information in 5 different label controls in an ASP.NET web page. When I test the stored procedure in a GridView all the columns display correctly (I am not manually defining the columns). When I try to use labels I can display all...
1
4134
by: masoumeh | last post by:
please help me I use this code TA_Insert_Language as new DataSetName.Insert_LanguageTableAdapter DT_Insert_Language as new DataSetName.Insert_LanguageDataTable dim MaxRow as new DataRow dim row as integer MaxRow=Select_MaxRow.MAXROW row=MaxRow.Select("","").GetValue 'This is a stored procedure that select max(Row)form the table in database
3
6876
by: comp_databases_ms-sqlserver | last post by:
This post is related to SQL server 2000 and SQL Server 2005 all editions. Many of my stored procedures create temporary tables in the code. I want to find a way to find the query plan for these procs Repro --*********************************** use pubs go CREATE PROCEDURE Test @percentage int
9
4148
by: fniles | last post by:
I am using VB.NET 2003 and SQL2000 database. I have a stored procedure called "INSERT_INTO_MYTABLE" that accepts 1 parameter (varchar(10)) and returns the identity column value from that table. When calling the stored procedure from VB.NET, in the CommandText, can I just say "INSERT_INTO_MYTABLE '12345'" instead of calling it with "INSERT_INTO_MYTABLE" then do the following : OleDbCommand2.Parameters.Add("@Account", SqlDbType.VarChar, 10)...
5
2819
by: Phil Barber | last post by:
I have a stored procedure that contains several temp tables, I am attempting to assign a tableadapter to the Sp. when it goes to create the persistent fields (in the wizard) I get Select command error Invalid object #DrY, which happens to be the name of the first declared temp table in the SP. how do properly connect a Sp to a tableAdapter that uses Temp tables Thanks (using: VS 2005, C#, ASP.net) Phil Barber
1
3834
by: ajay kumar chada | last post by:
Can anybody help me in finding error in the following SP please, (actullay i need to drop the tables which had been created six months ago CREATE PROCEDURE TGNSX00.TAB_DROP_PROC(OUT OUT_SQLCODE integer) LANGUAGE SQL MODIFIES SQL DATA DYNAMIC RESULT SETS 1 P1: BEGIN Declare v_stmt varchar(100); Declare TBSC varchar(100);
0
9715
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9595
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10603
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10353
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9176
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7643
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5536
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5675
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3836
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.