Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Query


Query

Author
Message
Austin_123
Austin_123
SSC-Addicted
SSC-Addicted (489 reputation)SSC-Addicted (489 reputation)SSC-Addicted (489 reputation)SSC-Addicted (489 reputation)SSC-Addicted (489 reputation)SSC-Addicted (489 reputation)SSC-Addicted (489 reputation)SSC-Addicted (489 reputation)

Group: General Forum Members
Points: 489 Visits: 454
Hi All,

Below query will drop all access for specified user from the user database.

DECLARE C1 CURSOR FOR
SELECT NAME FROM MASTER.DBO.SYSDATABASES
WHERE DBID > 4
AND DATABASEPROPERTYEX(name, 'Status') = 'ONLINE'
OPEN C1
FETCH NEXT FROM C1
INTO @SR
WHILE(@@FETCH_STATUS <> -1)
BEGIN
SET @CMD = 'USE [' +@SR + ']' + @NL +
'IF EXISTS (SELECT NAME FROM DBO.SYSUSERS WHERE NAME LIKE N''domain\user'')' + @NL +
'EXEC SP_REVOKEDBACCESS N''domain\user'' + @NL +
PRINT 'DROPPING THE ACCOUNT FROM ' + @SR + '....'
EXEC (@CMD)
FETCH NEXT FROM C1
INTO @SR
END
CLOSE C1
DEALLOCATE C1

-- @NL is CHAR(13)
with some databases when users have created with username as below

lf login is : WIN\Austin then with some database it has created
'Austin' only (no domail prefix) and yes login has referred to WIN\Austin.

Above code works ok when both login and user on db are same but it fails when users have been created without domain prefix.

I am thinking to store the output of below command in a variable.
SELECT name from dbo.sysusers where SID = SUSERS_SID(N'WIN\Austin')

I am not sure how to integrate this with main code. In other words If I store the output of above query then how can I pass the variable as parameter for SP_REVOKEDBACCESS @name_in_db or is there any way to execute the query as parameter for sp.

Thanks in advance.

Regards,
Austin
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search