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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Default Schema = DBO

Author  Topic 

nguyenl
Posting Yak Master

128 Posts

Posted - 2009-05-29 : 12:29:16
Hi,

Please help. I am trying to find a way to change all my users default schema to "DBO". Currently I can do it manually, but I have over 100 users.

Thanks

dinakar
Master Smack Fu Yak Hacker

2507 Posts

Posted - 2009-05-29 : 12:31:51
try this:


DECLARE @currUser varchar(100),
@SQL nvarchar(200)
DECLARE Users CURSOR
READ_ONLY
FOR select [NAME] from sys.database_principals
where
[name] not in('dbo','guest','INFORMATION_SCHEMA','sys') AND
[type] in('S','U') AND
[name] = [default_schema_name]
OPEN Users
FETCH NEXT FROM Users INTO @currUser
WHILE (@@fetch_status <> -1)
BEGIN
SET @SQL = 'ALTER USER [' + @currUser + '] WITH DEFAULT_SCHEMA = dbo'
EXEC (@SQL)
FETCH NEXT FROM Users INTO @currUser
END
CLOSE Users
DEALLOCATE Users
GO
SELECT name as username, default_schema_name FROM sys.database_principals
WHERE type in ('S','U') and name not in('dbo','guest','INFORMATION_SCHEMA','sys')




Dinakar Nethi
************************
Life is short. Enjoy it.
************************
http://weblogs.sqlteam.com/dinakar/
Go to Top of Page
   

- Advertisement -