Saturday, February 25, 2012

Osql

Hi all.

My first post here.

I want to distriburte a msde database. When doing so, I also want to make a new login, make a user, make the user the dbowner of the database i installed.

I found the Stored procedures I need to use, and I have tested that it works using OSQL.

What I want to do now is to make this automatic. After installing the database the OSQL commands should be executed and no user interference should be necessary. How can that be done ?

peetthrough a *.bat file?|||through a *.bat file?

Ok, how ?

From what I see I have to do the following :

'**************** sample ********************

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

D:\>osql -S PEETSPC\PEETSQL -U sa
Password:
1> sp_attach_single_file_db "TEST","D:\DB\TEST.MDF"
2> go
New log file 'D:\DB\TEST_log.LDF' was created.
1> exit

D:\>osql -S PEETSPC\PEETSQL -U sa -d TEST
Password:
1> sp_addlogin test, mypass, TEST
2> sp_adduser test, test, db_owner
3> go
1> sp_addlogin test, mypass, TEST
2> go
New login created.
1> sp_adduser test, test, db_owner
2> go
Granted database access to 'test'.
'test' added to role 'db_owner'.
1> exit

D:\>

How can I do this using a bat file ?
I know what a bat file is, but I do not see how I can get a bat file to do the above. Please help me out if you can :-)

peet|||*bump*

anyone ?|||You need two separate files. The first one I'll calll test.sql will contain:sp_attach_single_file_db "TEST","D:\DB\TEST.MDF"
go

USE TEST
GO

sp_addlogin test, mypass, TEST
sp_adduser test, test, db_owner
go

sp_addlogin test, mypass, TEST
go

sp_adduser test, test, db_owner
goThe second file I'll call test.bat:osql -S PEETSPC\PEETSQL -U sa - P myPass=123 -i test.sql -o test.rptWhen you run the batch file, it will create the database, add the logins and users, and put the output into the test.rpt file for your reading pleasure.

-PatP

No comments:

Post a Comment