Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
sumwanlah
Starting Member
43 Posts |
Posted - 2002-03-26 : 04:26:41
|
Hi!I've written a short piece with many errors... I'm looking for anyone out there who can help me correct the syntax on this baby...I'm trying to look through the entire server looking out for databases where a user already has access.You see, I've got a user who is away on long-term leave, and I need to find and replace her access with another user.I'm trying to write a script that runs through each database to find if her user login exists.If she does, then based on the results list, I will manually remove her user login and replace it with another...HELP!!! Hahahahahha!!!! Here's the script:------------------------ declare @dbName sysnameDECLARE test_cursor CURSOR FORSELECT name FROM sysdatabases ORDER BY nameopen test_cursorfetch next from test_cursorinto @dbNamewhile @@FETCH_STATUS = 0BEGINexecute('use ' +@dbname+'') PRINT 'Database Now in Use: ' + @dbName begin execute('if exists (select * from '+@dbName+'.dbo.sysusers where name = "sumwanlah" and uid < 16382)') print 'she is here:'+@dbName end FETCH NEXT FROM test_cursor INTO @dbNameENDclose test_cursordeallocate test_cursor |
|
|
dsdeming
479 Posts |
Posted - 2002-03-26 : 08:19:46
|
| I think the problem is in here:begin execute('if exists (select * from '+@dbName+'.dbo.sysusers where name = "sumwanlah" and uid < 16382)') print 'she is here:'+@dbName endFirst, I don't think you'll ever get the print statement to execute reliably because the exists statement is executed dynamically but the print statement is not in the same dynamic batch. Second, because of the previous 'execute( 'use ' + @dbname )' statement, you don't need dynamic sql here anyway. The database has already been changed. You should be able to change the above code to something like:begin if exists (select * from dbo.sysusers where name = 'sumwanlah' and uid < 16382) print 'she is here:'+@dbName end |
 |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2002-03-26 : 08:23:53
|
| Don't have sql server here but something like this.I have included code to drop the user (and removed the unnecessary cursor) - don't know whether you would want that.declare @username varchar(128)set @username = 'hisname'declare @dbname varchar(128) ,@uid int ,@sql nvarchar(1000) ,@rc intselect @uid = suid from master..syslogins where name = @usernameselect @dbname = ''while exists (select * from master..sysdatabases where name > @dbname)beginselect @dbname = min(name) from master..sysdatabases where name > @dbnameselect @rc = 0select @sql = 'select @rc = 1 from ' + @dbname' + ..sysusers where uid = ' + convert(varchar(10),@uid)exec sp_executesql @sql, N'@rc int output, @rc outputif @rc = 1begin select 'user in' + @dbname select @sql = 'exec ' + @dbname + '..sp_executesql N''drop user ' + @username + '''' exec (@sql)endend==========================================Cursors are useful if you don't know sql.Beer is not cold and it isn't fizzy.Edited by - nr on 03/26/2002 08:25:30 |
 |
|
|
|
|
|
|
|