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 |
|
victord
Yak Posting Veteran
64 Posts |
Posted - 2003-04-16 : 05:27:35
|
| Hello Folks,I know we can use EXEC sp_changeobjectowner 'authors', 'Corporate\GeorgeW' to change a single object in a database.. But how do i change the owner of tables over 500 in a database.Thanks in Advance,Victord |
|
|
macka
Posting Yak Master
162 Posts |
Posted - 2003-04-16 : 05:51:41
|
| victord,DECLARE @name AS VARCHAR(255)DECLARE curDB CURSOR FOR--modify this query to suit your needsSELECT nameFROM sysobjectsWHERE xtype = 'U'OPEN curDBFETCH NEXT FROM curDB into @nameWHILE @@FETCH_STATUS = 0BEGIN EXEC sp_changeobjectowner @name, 'Corporate\GeorgeW' FETCH NEXT FROM curDB into @nameENDCLOSE curDBDEALLOCATE curDBCheers,macka.--There are only 10 types of people in the world - Those who understand binary, and those who don't. |
 |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2003-04-16 : 06:31:33
|
| or (no reason not to use an explicit cursor for this but ...)declare @s varchar(128)select @s = ''while @s < (select max(name) from sysobjects where xtype = 'U')beginselect @s = min(name) from sysobjects where xtype = 'U' and name > @nameEXEC sp_changeobjectowner @name, 'Corporate\GeorgeW' end==========================================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. |
 |
|
|
victord
Yak Posting Veteran
64 Posts |
Posted - 2003-04-16 : 07:04:03
|
| Macka,Thanks for your reply.But the problem i am having now is that after changing the owner of the tables/objects it drops all existing permissions for the objects. How do i reapply the permissions.Cheers,victord |
 |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2003-04-16 : 07:35:07
|
| If they are all the same then add a grant statement to the loopselect @sql = 'grant all to usrrole on ' + @nameexec (@sql)==========================================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. |
 |
|
|
victord
Yak Posting Veteran
64 Posts |
Posted - 2003-04-16 : 08:49:31
|
| Thanks Nr,I tried the first procedure you gave me but came up with erroas follows :Server: Msg 137, Level 15, State 2, Line 5Must declare the variable '@name'.Server: Msg 137, Level 15, State 1, Line 6Must declare the variable '@name'.There seems to be something missing in the procedure.Macka,Tried your procedure as well but came up with errors as follows:Server: Msg 15001, Level 16, State 1, Procedure sp_changeobjectowner, Line 38Object 'EMSLS' does not exist or is not a valid object for this operation.Server: Msg 15001, Level 16, State 1, Procedure sp_changeobjectowner, Line 38Object 'EMSPD' does not exist or is not a valid object for this operation.This looks like a permission error to me.Please help. |
 |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2003-04-16 : 09:48:19
|
| declare @name varchar(128) select @name = '' while @name < (select max(name) from sysobjects where xtype = 'U') begin select @name = min(name) from sysobjects where xtype = 'U' and name > @name EXEC sp_changeobjectowner @name , 'Corporate\GeorgeW'select @sql = 'grant all to usrrole on Corporate\GeorgeW.' + @name exec (@sql) end Suspect the tables are not all owned by the user you are running this under.Include uid in the select to get objects owned by your user or include the current owner in the change statement.Mighet be better to use the information_schema views.==========================================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. |
 |
|
|
|
|
|
|
|