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 |
andrija11
Starting Member
2 Posts |
Posted - 2007-01-17 : 04:49:26
|
Hi all,during my development I have to restore database more then few times, so I'm using script to do it... but, I always have to delete user, then add its access to database again from 'Login' node, because of the restore before. Does anyone have some quick script to delete user from database, and add it again with roles 'public' and 'db_owner'? thx |
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2007-01-17 : 08:48:01
|
seesp_dropusersp_addusersp_addrolemember==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
andrija11
Starting Member
2 Posts |
Posted - 2007-01-17 : 10:38:25
|
THX!whole script looks like this now:use master goDECLARE @dbid int, @spid int, @execstr varchar(15) --Getting the database_id for the specified database SET @dbid = DB_ID('MyDatabase') --Get the lowest spid SET @spid = (SELECT MIN(spid) FROM master..sysprocesses WHERE dbid = @dbid) WHILE @spid IS NOT NULL BEGIN IF @spid <> @@SPID --To avoid the KILL attempt on own connection BEGIN SET @execstr = 'KILL ' + LTRIM(STR(@spid)) EXEC(@execstr) --Killing the connection END --Get the spid higher than the last spid SET @spid = (SELECT MIN(spid) FROM master..sysprocesses WHERE dbid = @dbid AND spid > @spid) END RESTORE DATABASE [MyDatabase] FROM DISK = N'c:\Program Files\Microsoft SQL Server\MSSQL\BACKUP\MyDatabase.bak' WITH FILE = 1, NOUNLOAD, STATS = 10PRINT 'Database restored at : ' + CAST(GETDATE() AS VARCHAR(20))GOprint 'restore user'gouse MyDatabasegosp_dropuser 'mydbuser'gosp_adduser 'mydbuser'gosp_addrolemember 'db_owner', 'mydbuser' |
 |
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2007-01-17 : 15:56:05
|
Also have a look at sp_change_users_login==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
|
|