Sunday, March 11, 2012

Create Proc

Hello
I have one probably stupid qestion
I want to create procedure that will create user then create database with the same name as user name and the give that user db_owner permision to that data base i created procedure that creat a user and creat a database but i couldn't do last part of it
becous USE statment can't be use in a procedur
Do you have some idea of how to create something like this ?
e.g.
create proc usp
@.login sysname,
@.pwd sysname=''
as
set nocount on
declare @.sql nvarchar(1000)
set @.sql = '--create database
if db_id('+quotename(@.login,char(39)+char(39))+') is null
create database '+quotename(@.login)
print(@.sql)
exec(@.sql)
set @.sql = '--create login
if suser_sid('+quotename(@.login,char(39)+char(39))+') is null
exec sp_addlogin
'+quotename(@.login,char(39)+char(39))+','+quotenam e(@.pwd,char(39)+char(39))+
','+quotename(@.login,char(39)+char(39))
print(@.sql)
exec(@.sql)
set @.sql = '--grantdbaccess
exec '+quotename(@.login)+'..sp_grantdbaccess
'+quotename(@.login,char(39)+char(39))
print(@.sql)
exec(@.sql)
set @.sql = '--add db_owner
exec '+quotename(@.login)+'..sp_addrolemember
''db_owner'','+quotename(@.login,char(39)+char(39))
print(@.sql)
exec(@.sql)
go
"Nikon" <anonymous@.discussions.microsoft.com> wrote in message
news:1B54E87B-2788-4711-B0F2-31D8AEEA085B@.microsoft.com...
> Hello
> I have one probably stupid qestion
> I want to create procedure that will create user then create database with
the same name as user name and the give that user db_owner permision to that
data base i created procedure that creat a user and creat a database but i
couldn't do last part of it becous USE statment can't be use in a procedur
> Do you have some idea of how to create something like this ?
>
|||THX YOU HELPED ME VERY MUCH I now just must analyz it and understend it =)

No comments:

Post a Comment