|
sql
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Change only USER tables to a new user
Hello,
I have a database created with several tables. There are two types of table - system and user. I want to change all the user tables ownership from dbo to another user. Is there a script that would allow me to do that - but only affect the user type tables? Thanks, Tmuld Hi
Show quoteHide quote "Tmuldoon" wrote: You don't give the version of SQL Server you are using! SQL 2005 has a ALTER > Hello, > > I have a database created with several tables. There are two types > of table - system and user. > > I want to change all the user tables ownership from dbo to another > user. > > Is there a script that would allow me to do that - but only affect the > user type tables? > > Thanks, > > Tmuld > SCHEMA statement that will allow you to change the schema of a securable. The tables to move could be identified by selecting them from the sys.tables view. If you are using SQL 2000, then I think you will need to re-create the table and use INSERT..SELECT to move the data, you may have to drop the constraints on the old table before creating the new table to avoid name conflicts. This may be easier to do by scripting out the tables and editing the script. Why do you need to change schemas? John Tmuld,
If you are using SQL Server 2000 use: sp_changeobjectowner If you are using SQL Server 2005 use: ALTER TABLE You can create your own script by: SELECT 'EXEC sp_changeobjectowner ''' +TABLE_SCHEMA+ '.'+TABLE_NAME + ''', ''NewOwner''', * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' ORDER BY TABLE_NAME The output of this SQL 2000 select is a script you can run. (Or edit first, then run.) If you use the ALTER TABLE approach then apply this idea to generating that script. Be sure with SQL 2005 that you understand the difference between 'owner' and 'schema' since these are now separated. RLF Show quoteHide quote "Tmuldoon" <tmuld***@spliced.com> wrote in message news:1176822600.477208.87050@b75g2000hsg.googlegroups.com... > Hello, > > I have a database created with several tables. There are two types > of table - system and user. > > I want to change all the user tables ownership from dbo to another > user. > > Is there a script that would allow me to do that - but only affect the > user type tables? > > Thanks, > > Tmuld > Hi
Show quoteHide quote "Russell Fields" wrote: I had forgotten about sp_changeobjectowner!> Tmuld, > > If you are using SQL Server 2000 use: sp_changeobjectowner > If you are using SQL Server 2005 use: ALTER TABLE > > You can create your own script by: > > SELECT 'EXEC sp_changeobjectowner ''' +TABLE_SCHEMA+ > '.'+TABLE_NAME + ''', ''NewOwner''', * > FROM INFORMATION_SCHEMA.TABLES > WHERE TABLE_TYPE = 'BASE TABLE' > ORDER BY TABLE_NAME > > The output of this SQL 2000 select is a script you can run. (Or edit first, > then run.) > > If you use the ALTER TABLE approach then apply this idea to generating that > script. Be sure with SQL 2005 that you understand the difference between > 'owner' and 'schema' since these are now separated. > > RLF > > "Tmuldoon" <tmuld***@spliced.com> wrote in message > news:1176822600.477208.87050@b75g2000hsg.googlegroups.com... > > Hello, > > > > I have a database created with several tables. There are two types > > of table - system and user. > > > > I want to change all the user tables ownership from dbo to another > > user. > > > > Is there a script that would allow me to do that - but only affect the > > user type tables? > > > > Thanks, > > > > Tmuld > > For SQL 2005 you could use SELECT 'ALTER SCHEMA NewSchema TRANSFER [' +TABLE_SCHEMA + '].['+TABLE_NAME + ']' FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' ORDER BY TABLE_NAME With both methods you will need to re-apply permissions to the tables. John
Other interesting topics
Rolling Back from build 3159 to build 3152
can't access server from another PC SQL 2005 Performance One table returns ODBC timeout error Major SQL Server 2k5 x64 configuration problem ssrs Can you spot a security problem here with XQuery!?... Problem with TCP connections to SQL 2000 application deleting a tx log backup Best practise on Database security |
|||||||||||||||||||||||