| 
 | 
								
									
										|  |  
										|  |  
										| | This is very handy stored procedure which I use all time to change object owner. Many times you create object as a different user other than "dbo" and things start breaking. I use this sp to change owner off all objects to "dbo". | 
 |  Click here to copy the following block |  | 
 IF OBJECT_ID('ChangeAllObjOwner') IS NOT NULL DROP PROC ChangeAllObjOwner
 GO
 
 CREATE PROCEDURE ChangeAllObjOwner (
 @oldowner sysname,
 @newowner sysname
 )
 AS
 DECLARE @objname sysname
 SET NOCOUNT ON
 
 
 IF USER_ID(@oldowner) IS NULL
 BEGIN
 RAISERROR ('The @oldowner passed does not exist in the database', 16, 1)
 RETURN
 END
 
 IF USER_ID(@newowner) IS NULL
 BEGIN
 RAISERROR ('The @newowner passed does not exist in the database', 16, 1)
 RETURN
 END
 
 DECLARE owner_cursor CURSOR FOR
 SELECT name FROM sysobjects WHERE uid = USER_ID(@oldowner)
 
 OPEN owner_cursor
 FETCH NEXT FROM owner_cursor INTO @objname
 WHILE (@@fetch_status <> -1)
 BEGIN
 SET @objname = @oldowner + '.' + @objname
 EXEC sp_changeobjectowner @objname, @newowner
 FETCH NEXT FROM owner_cursor INTO @objname
 END
 
 CLOSE owner_cursor
 DEALLOCATE owner_cursor
 GO
 | 
 |  
										|  |  
										|  |  
 
	
		| Submitted By :
					Nayan Patel 
					 (Member Since : 5/26/2004 12:23:06 PM) |  
		|  |  
		| 
				 | Job Description :   
			He is the moderator of this site and  currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting. |  
		| View all (893) submissions by this author 
			(Birth Date : 7/14/1981 ) |  |  |