0

I have a script that drops the user if it exists and then recreates it. I have this repeated like 30 times. Shown below:

IF EXISTS (SELECT 1 FROM sys.sysusers AS s WHERE s.name = N'johndoe')
BEGIN
    PRINT 'Dropping user johndoe...' 
    DROP USER johndoe;
END

CREATE USER [johndoe] FOR LOGIN [uat\johndoe];

I was wondering if it would be possible to write a stored procedure that accepts user name and domain name and then drops and recreates the user? So that I could call like:

EXEC usp_DropAndRecreateUsers @username='johndoe', @domainName='uat'

However, after writing a stored procedure, I realized it wouldn't be as easy as I thought. Which probably is the reason this wasn't already done. But still, is this even possible to do in SQL Server (2008 and above)?

What I did was (which doesn't work):

CREATE PROCEDURE dbo.usp_DropAndRecreateUsers
    (@username varchar(100),
     @domainName varchar(100))
AS 
    DECLARE @loginName varchar(200) = '['+@domainName+'\'+@username+']';

    IF EXISTS (SELECT 1 FROM sys.sysusers AS s WHERE s.name = @username)
    BEGIN
        DROP USER @username;
    END

    CREATE USER @username FOR LOGIN @loginName;

Update: The error is that when I use a parameter in Drop/Create User @username, it is considered as invalid sql statement. The error message reads: Incorrect syntax near '@username'.Expecting ID, or QUOTED_ID.

5
  • 1
    Could you be more vague than "which doesn't work"?
    – HABO
    May 16, 2017 at 3:13
  • @HABO "which performs other than expected". Do I win? May 16, 2017 at 3:36
  • He has a point though, you haven't described the problem/error you are running into, which makes it pretty hard to help you solve it. May 16, 2017 at 3:38
  • Curious why are you creating a procedure for adding an user. IMO, it may not be a good idea to handle database users from a business application. Unless you have a strong use case.
    – Nayak
    May 16, 2017 at 5:56
  • @Nayak - The only reason I am wanting a stored proc here is to avoid code duplication. This sp will not be called by any application. This will be called by a script that runs at the end of our database refresh process. After the refresh, users needs to be tied to domain specific login, which we handle this way.
    – haku
    May 16, 2017 at 14:08

1 Answer 1

1

Rather than running from variable, create query and execute it

CREATE PROCEDURE dbo.USP_DROPANDRECREATEUSERS
    (@username varchar(100),
     @domainName varchar(100))
AS 
    BEGIN
    DECLARE @loginName varchar(200) = '['+@domainName+'\'+@username+']';
    DECLARE @query VARCHAR(MAX);
    IF EXISTS (SELECT 1 FROM sys.sysusers AS s WHERE s.name = @username)
    BEGIN
        SET @query = 'DROP USER ' + @username;
        EXEC (@query);
    END

    SET @query = 'CREATE USER ' + @username + ' FOR LOGIN ' + @loginName;
    EXEC (@query);
    END

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.