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 |
|
CanadaDBA
583 Posts |
Posted - 2005-04-20 : 14:35:26
|
I have a script that restores a database and grants rights to the users. I was using sp_dropuser, sp_grantdbaccess, and sp_addrolemember for this reason. USE MyDBGOexec sp_dropuser 'Userid'exec sp_grantdbaccess 'Userid','Userid'exec sp_addrolemember 'db_owner','Userid' In order to automate this RESTORE, I created a SP and put my script in it but I can not use USE command in a SP and therefore cannot use the above sp_...s. What can I do?Canada DBA |
|
|
jason
Posting Yak Master
164 Posts |
Posted - 2005-04-20 : 14:45:37
|
| Call 'USE' outside the sproc? |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2005-04-20 : 16:04:12
|
| You should be calling sp_ with the three part naming convention so that you don't require USE:SomeDBName.dbo.sp_grantdbaccessTara |
 |
|
|
CanadaDBA
583 Posts |
Posted - 2005-04-21 : 08:59:10
|
Thanks Tara. I used this way and it is working. But still there is a problem in somewhere else! In the SP, after the DB is restored and users are defined, I need to grant execute rights to a userid. Here is my code: declare @objname varchar(100) declare namelist insensitive cursor forselect name from [MyDB].[dbo].sysobjects where xtype in('p','fn') AND category=0 open namelistfetch next from namelist into @objnamewhile @@fetch_status=0begin exec('grant execute on [dbo].[' + @objname + '] to [Userid]') fetch next from namelist into @objnameend close namelistdeallocate namelistIn exec('grant execute on [dbo].['... I get an error because the object does not exist in the current database. I tried to use it as exec('grant execute on [MyDB].[dbo].[' + @objname + '] to [Userid]') but it didn't work. quote: Originally posted by tduggan You should be calling sp_ with the three part naming convention so that you don't require USE:SomeDBName.dbo.sp_grantdbaccessTara
Canada DBA |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2005-04-21 : 12:26:13
|
| I would put the grants in a stored procedure. Then call the stored procedure with DBName.dbo.SPName. Or you could use sp_executesql and qualify it with the database name instead of using EXEC.Tara |
 |
|
|
CanadaDBA
583 Posts |
Posted - 2005-04-21 : 13:22:00
|
| The sp_executesql approach doesn't work but I think I have to stick with your 1st idea. I need to split my SP into 2 parts: Restore and Grants. Thanks,Canada DBA |
 |
|
|
CanadaDBA
583 Posts |
Posted - 2005-04-22 : 09:13:42
|
I put the code into isp_MyDB_Grant_Rights and ran the following: EXEC master.dbo.isp_MyDB_Restore_Last_Night_BackupUSE MyDBGOEXEC master.dbo.isp_MyDB_Grant_Rights The problem is that although I have USEd MyDB but exec('grant execute on [dbo].[' + @objname + '] to [Userid]')cannot "see" the userid. How can I grant Execute right to the Userid?Thanks,p.s. The original problem is that I want to restore everynight's production backup on my Dev server on the next day. Userid needs to have execute right on the database's PSs and UDFs.quote: Originally posted by tduggan I would put the grants in a stored procedure. Then call the stored procedure with DBName.dbo.SPName. Or you could use sp_executesql and qualify it with the database name instead of using EXEC.Tara
Canada DBA |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2005-04-22 : 12:42:26
|
| I think you are going to need to put isp_MyDB_Grant_Rights into MyDB. You shouldn't be putting anything into master anyway. If you want objects outside of your user database, you should be creating a new database for this. We call ours Admin.Tara |
 |
|
|
CanadaDBA
583 Posts |
Posted - 2005-04-22 : 14:50:34
|
Actually, I am following your way and have created the Admin DB and put these SPs into it. But in my approach, MyDB is being restored and I have no access to the SP at that time. ...Well, when I think, the DB is being restored and then the Grant SP is executed so, I can use your suggest as well. By the way, I changed the EXEC 'GRANT...' into EXEC('USE MyDB ; GRANT execute on [dbo].[' + @objname + '] to Userid]') and it worked.quote: Originally posted by tduggan I think you are going to need to put isp_MyDB_Grant_Rights into MyDB. You shouldn't be putting anything into master anyway. If you want objects outside of your user database, you should be creating a new database for this. We call ours Admin.Tara
Canada DBA |
 |
|
|
|
|
|
|
|