473,749 Members | 2,580 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

stored procedure to create new database

Is there a stored procedure installed by sql server 2000 that I can
call and just pass in the name of a new database and have it create
the database for me? If not, how do I do it in sql? Thanks.
Jul 20 '05 #1
3 24085

"geoff" <ca********@hot mail.com> wrote in message
news:bf******** *************** **@posting.goog le.com...
Is there a stored procedure installed by sql server 2000 that I can
call and just pass in the name of a new database and have it create
the database for me? If not, how do I do it in sql? Thanks.


This is one way:

declare @newdb sysname
set @newdb = 'NewDatabase'
exec('create database ' + @newdb)

See CREATE DATABASE in Books Online for full syntax.

Simon
Jul 20 '05 #2
Thanks Simon, actually, I just remembered BOL after I made the post. I
found the syntax for CREATE DATABASE "DBName". I ran it in query
analyzer to test it and it worked fine. So I figured I'd make a stored
procedure out of it, but it's bombing on that line saying there's an
incorrect syntax. Do you see a problem with my code here? Thanks
again.

CREATE PROCEDURE CreateNewClient Database

@pDBName varchar (128)

AS

if not exists(select dbid from master.sysdatab ases where name =
@pDBName)
CREATE DATABASE @pDBName
else
raiserror("Data base already exists.",3)

if @@error = 0
return 0
else
return -1

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #3

"geoff tyler" <ca********@hot mail.com> wrote in message
news:40******** *************@n ews.frii.net...
Thanks Simon, actually, I just remembered BOL after I made the post. I
found the syntax for CREATE DATABASE "DBName". I ran it in query
analyzer to test it and it worked fine. So I figured I'd make a stored
procedure out of it, but it's bombing on that line saying there's an
incorrect syntax. Do you see a problem with my code here? Thanks
again.

CREATE PROCEDURE CreateNewClient Database

@pDBName varchar (128)

AS

if not exists(select dbid from master.sysdatab ases where name =
@pDBName)
CREATE DATABASE @pDBName
else
raiserror("Data base already exists.",3)

if @@error = 0
return 0
else
return -1

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!


If you check the syntax description for CREATE DATABASE, you'll see that it
doesn't accept variables - that was why my example used EXEC() to build a
string and then execute it. A couple of other points:

* master..sysdata bases - note the additional full stop required (see "Using
Identifiers as Object Names")
* Avoid using double quotes (see SET QUOTED_IDENTIFI ER)
* Your RAISERROR syntax is incorrect (no value for state)
* Checking @@ERROR at that point doesn't do much - see this link for more
details on how to use @@ERROR correctly:

http://www.sommarskog.se/error-handling-II.html

Simon
Jul 20 '05 #4

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

Similar topics

1
8661
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...
1
1461
by: Hennie de Nooijer | last post by:
Hi, Ik created an application with visuals basic.NET. This has a connection string to one database, let's say 'A'. In this database a stored procedure is called which should execute a string (which is passed by the) VB tool. This string is a CREATE VIEW statement en this should be executed in another database let's say 'B'. I tried this in Transact - SQL
1
1927
by: RSH | last post by:
Im trying to create a stored procedure of the following code. I want to set it so that I have to send the first 4 variables (@DB, @BackUpFile,@TestDB,@RestoreFile). I am having trouble when i try to save it...SQL Server wants me to decalre the variables. How would I go about making this a stored procedure correctly? Here is the code: USE master GO
0
1329
by: Marco Vasquez | last post by:
Currently, we are using the next tool in order to execute SQL commands in DB2 databases: About DB2 Administration Tools Environment -------------------------------------------------------------------- DB2 administration tools level: Product identifier SQL08020 Level identifier 03010106 Level DB2 v8.1.7.445 Build level s040812
2
1935
by: nikolacace | last post by:
Hi, I have a stored procedure (the code is below) that I use to retrieve one value from my database. I tested the code in Query Analyzer, and it works (I get the value I was looking for). However, when I call the same code from the stored procedure, I get no value. The code that is executed is the same and the input parameter is the same. Does anybody have an idea? The code:
5
16559
by: Wael | last post by:
Hi, I have the following stored procedure that does some processing and puts the result in a temporary table. I tried several things that procedure to display output that I can access with ADO.Net, but it doesn't work. It doesn't even display the result in the query analyzer unless I add SELECT @ReturnFullName Any help? The stored procedure:
1
4128
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
9
4147
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)...
6
1515
by: chessitguy | last post by:
my stored procedure: create procedure ps_title @Title nchar(15) AS SELECT ,Title FROM books Where Title = @Title this is my code. i'm passing variable to sp and getting no results.
3
6202
by: yinzara | last post by:
I have the following trigger that calls a DB2 stored procedure: DROP TRIGGER GGWU.TRI_A_MULTI_PROP@ CREATE TRIGGER GGWU.TRI_A_MULTI_PROP AFTER INSERT ON GGWU.MULTIPLIER_PROPERTY REFERENCING NEW AS POST FOR EACH ROW MODE DB2SQL BEGIN ATOMIC CALL GGWU.PKG_MULT_PROP_INSERT(POST.C_PROPERTY_CODE,POST.I_MULTIPLIER_ID); END@
0
8996
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
8832
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
9566
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...
1
9333
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9254
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4608
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
4879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3319
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2217
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.