Using Sp_grantdbaccess

Nov 16, 2007



i have a stored procedure for creating SQL server users within a database but i have been given a change request where the created SQLServer user will need to be granteddbaccess to another db also

is there a way to do this??

at the mo my sp reads like this:-





Code Block

CREATE PROCEDURE ys_InsertUser2
@int_Identity int OUTPUT,
@str_username varchar(25),
@str_role varchar(20),
@str_password varchar(15),
@str_Database varchar(20)
AS

SET NOCOUNT ON
DECLARE @USER varchar(25)
SELECT @USER=username from tblUser where username=@str_username
BEGIN
if not exists (select * from master..syslogins where name = @str_username)
BEGIN
declare @logindb nvarchar(132), @loginlang nvarchar(132)

select @logindb = @str_Database, @loginlang = N'British'

if @logindb is null or not exists (select * from master..sysdatabases where name = @logindb)
select @logindb = N'master'

if @loginlang is null or (not exists (select * from master..syslanguages where name = @loginlang) and @loginlang <> N'us_english')
select @loginlang = @@language

exec sp_addlogin @str_username, null, @logindb, @loginlang
END
if not exists (select * from sysusers where name = @str_username and uid < 16382)
EXEC sp_grantdbaccess @str_username, @str_username
exec sp_addrolemember @str_Role, @str_username
EXEC sp_password NULL, @str_password, @str_username
END

SELECT @int_Identity = 0 --just a bluff inorder to use formclasses
IF @@Error = 0
BEGIN
RETURN 0
END
ELSE
BEGIN
RETURN 99
END

SET NOCOUNT OFF





but i need to expand the sp_grantdbaccess and sp_addrolemember to include access to another DB and not the local one which the stored proc is running in.

is this possible?

Cheers,
Craig

View 3 Replies


ADVERTISEMENT

Can Sp_grantdbaccess Attach User To Default Schema

Jun 15, 2007

Hi
For MSSQL 2005 server, when we create new user using stored procedure sp_grantdbaccess, it creates schema with name given to user.

While when we create new user with query " Create login identfied by'password'. It attach user to default schema at the server.


My question is can we have a default schema assigned to the user, while user is created with sp_grantdbaccess?

Is there any way to do so?

Please reply as early as possible.

View 3 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved