472,807 Members | 3,161 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,807 software developers and data experts.

[help] scripted database creation

Hi all :)

My apologies if I posted in the wrong groups, but I just jumped
in MS SQL waters, so any guidance will be appreciated.

What I'm trying to do is the following process:

[1] present operator with a web page (script)
[2] once filled with db name and username, script will create
..sql file
[3] osql.exe will be called with, i presume, -i file.sql and
create a database

I have limited SQL knowledge, but I got the information from
Enterprise Manager when I ran 'All Tasks -Generate SQL
Script' on how the .sql file should look like.

I realized what are the commands that would create a fresh DB
(I ran this for newly created DB), and figured my .php script
should create such a file.

It's fairly basic, and I'm almost sure all of you know how
outputed .sql file looks like, but anyway here it is:

Script is called with parameters 'six' as database name and
'magarac' as user name:

---------------------------------------------------------------
IF EXISTS (SELECT name FROM master.dbo.sysdatabases WHERE name
= N'six')
DROP DATABASE [six]
GO

CREATE DATABASE [six] ON (NAME = N'six_Data', FILENAME = N'E:
\Databasepath\six_Data.MDF' , SIZE = 1, MAXSIZE = 20,
FILEGROWTH = 10%) LOG ON (NAME = N'six_Log', FILENAME = N'E:
\Databasepath\six_Log.LDF' , SIZE = 1, MAXSIZE = 20, FILEGROWTH
= 10%)
COLLATE SQL_Latin1_General_CP1_CI_AS
GO

exec sp_dboption N'six', N'autoclose', N'false'
GO

....

use [six]
GO

if not exists (select * from dbo.sysusers where name =
N'guest' and hasdbaccess = 1)
EXEC sp_grantdbaccess N'guest'
GO

if not exists (select * from dbo.sysusers where name =
N'sinisam')
EXEC sp_grantdbaccess N'magarac', N'magarac'
GO

exec sp_addrolemember N'db_owner', N'magarac'
GO
---------------------------------------------------------------

I managed to get an exact replica of .sql file that Enterprise
Manager created. It leads me to believe that this way of
automated database creation is indeed possible.

Really sorry for making you to go through all this text, but
after I get a green light on this from you guys, I'll have a
bit more problematic question.

Is there any reason why this should not be used, or would fail?

Thanks in advance :)
P.S.
Just as a heads-up, next part of my problem is automated
creation of new MS SQL server login to use with new DB.
Jul 13 '07 #1
3 1984
Limunski Magarac (li**************@yahoo.com) writes:
CREATE DATABASE [six] ON (NAME = N'six_Data', FILENAME = N'E:
\Databasepath\six_Data.MDF' , SIZE = 1, MAXSIZE = 20,
FILEGROWTH = 10%) LOG ON (NAME = N'six_Log', FILENAME = N'E:
\Databasepath\six_Log.LDF' , SIZE = 1, MAXSIZE = 20, FILEGROWTH
= 10%)
COLLATE SQL_Latin1_General_CP1_CI_AS
GO
The SIZE and MAXSIZE values, from where did you get these? Out of
thin air? Or is there is a limit of 20 MB in the terms of service?
20 MB is quite small size. Not the least for the log file. And if
20 MB is indeed the size, there is little reason to fiddle with
autogrowth - create it to 20 MB directly.

Also, I would question the choice of collation. Given your name and
that you appear from post from Germany, I think you should pick a
different collation.
exec sp_dboption N'six', N'autoclose', N'false'
GO
Good!
if not exists (select * from dbo.sysusers where name =
N'guest' and hasdbaccess = 1)
EXEC sp_grantdbaccess N'guest'
GO
Eh? Any particular reason you enable guest? Particularly at a web
host, I would not recommend this.
if not exists (select * from dbo.sysusers where name =
N'sinisam')
EXEC sp_grantdbaccess N'magarac', N'magarac'
GO

exec sp_addrolemember N'db_owner', N'magarac'
GO
Hm, wouldn't be better to make the user the owner of the database?


--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.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
Jul 13 '07 #2
-Erland Sommarskog ->

Hi Erland,

Thank you for your reply.
Apologies for not responding sooner, but I had a lot on my mind
during weekend time.

The SIZE and MAXSIZE values, from where did you get these? Out of
thin air? Or is there is a limit of 20 MB in the terms of service?
20 MB is quite small size. Not the least for the log file. And if
20 MB is indeed the size, there is little reason to fiddle with
autogrowth - create it to 20 MB directly.
Actually, I created one database just the way I usually do and
then started 'All Tasks -Generate SQL Script' to see how .sql
file looks for the newly created databases.

20 MB is just a default size. Many times it's not needed to
increase the size, but if it's necessary, I can always do it
manually.

Also, I would question the choice of collation. Given your name and
that you appear from post from Germany, I think you should pick a
different collation.
It is the correct one, but I'm glad you noticed :)

if not exists (select * from dbo.sysusers where name =
N'guest' and hasdbaccess = 1)
EXEC sp_grantdbaccess N'guest'
GO

Eh? Any particular reason you enable guest? Particularly at a web
host, I would not recommend this.
Access is granted via group membership. It has 'public' role
membership. But I'll look into this further, thanks for the
heads-up.

exec sp_addrolemember N'db_owner', N'magarac'
GO

Hm, wouldn't be better to make the user the owner of the database?
Isn't this the exact thing this line does? Sets the user as the
owner?
So in the end, I guess this can be used to script a database
creation?

When the script is called with DBname and username, it should
create the database and a new SQL server login (and set default
database for this login to DBname).

I'm afraid I really do not know how to script a creation of a
new login. I've managed to go this far with scripted creation
of database, and with your help I went even further.

But if you have any advice on adding new SQL logins, link or
any other hint, I'd be more than happy to do more researching.

Thank you for your time,
Six
Jul 16 '07 #3
-Erland Sommarskog ->

I have no words to thank you!

I think I'm gonna stalk you now and see if there's anything you
need help with :)

Seriously, thanks for pointing out stuff I took for granted,
too.

Much appreciated,
Six
Jul 17 '07 #4

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

Similar topics

6
by: Dim St Thomas | last post by:
I am a developer working on a database client program. I am testing this program on a Windows XP machine (1.5 GHz AMD chip, 480 Mb RAM, 60 Gb disk) This machine has Oracle 9.2.0.1.0 and RedBrick...
3
by: Warren Wright | last post by:
I have production SQL Server database that must be moved to a new machine. There is a fairly complex DTS package on the original server that is used to handle the weekly updates to the database. ...
6
by: Bernd Koehler | last post by:
Hi: I am a EE prof currently teaching an IT course. One the assignments students recently completed was designing a small MS Access Database. I have two submissions that are remarkably...
0
by: Christian Nunciato | last post by:
Hi there: I'm a developer, and one of my clients operates a small Web site that with a simple contact form, written in ASP.NET, that posts back to itself and saves data to a database. The site...
6
by: James Radke | last post by:
Hello, I have a multithreaded windows NT service application (vb.net 2003) that I am working on (my first one), which reads a message queue and creates multiple threads to perform the processing...
12
by: mark2kay | last post by:
the assignment was to make list of 7 days starting with curent only that red was supposed to be in red. ive creted code only problem is its automatically makes 1st day red, which would be ok if day...
10
by: shsandeep | last post by:
The ETL application loaded around 3000 rows in 14 seconds in a Development database while it took 2 hours to load in a UAT database. UAT db is partitioned. Dev db is not partitioned. the...
1
by: rcamarda | last post by:
I have SQL Server 2005 x64. I found in the studio I can generate all sorts of scripts, including the creation of indexes. I works, however the script contains the CREATE TABLE statements as well....
0
by: SOI_0152 | last post by:
Hi all! Happy New Year 2008. Il hope it will bring you love and happyness I'm new on this forum. I wrote a stored procedure on mainframe using DB2 7.1.1 and IBM language c. Everything works...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.