<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Prolific Notion &#187; SQL</title>
	<atom:link href="http://www.prolificnotion.co.uk/tag/sql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.prolificnotion.co.uk</link>
	<description>Welcome to the miscellaneous mutterings of Simon Dingley, Freelance Web Developer</description>
	<lastBuildDate>Sat, 24 Jul 2010 14:23:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>SQL Server Stored Procedure to Change Object Owner</title>
		<link>http://www.prolificnotion.co.uk/sql-server-stored-procedure-to-change-object-owner/</link>
		<comments>http://www.prolificnotion.co.uk/sql-server-stored-procedure-to-change-object-owner/#comments</comments>
		<pubDate>Thu, 15 Jan 2009 20:20:46 +0000</pubDate>
		<dc:creator>Simon Dingley</dc:creator>
				<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Server]]></category>

		<guid isPermaLink="false">http://blog.prolificnotion.co.uk/?p=60</guid>
		<description><![CDATA[This is more of a note for myself but it&#8217;s handy little snippet for future reference to change the owner of objects in a MS SQL Server database. This is particularly handy when objects have been created by web applications and not set the object owner to dbo. To view th source code for the [...]


Related posts:<ol><li><a href='http://www.prolificnotion.co.uk/invalid-object-name-umbracouser/' rel='bookmark' title='Permanent Link: Invalid object name &#8216;umbracoUser&#8217;'>Invalid object name &#8216;umbracoUser&#8217;</a> <small>Yet another problem on an Umbraco v4.0 site I am...</small></li>
</ol>]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.prolificnotion.co.uk%2Fsql-server-stored-procedure-to-change-object-owner%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.prolificnotion.co.uk%2Fsql-server-stored-procedure-to-change-object-owner%2F&amp;style=normal&amp;service=bit.ly&amp;service_api=R_e35365ff035ed93fc3e67e6f868b7be3" height="61" width="50" /><br />
			</a>
		</div>
<p>This is more of a note for myself but it&#8217;s handy little snippet for future reference to change the owner of objects in a MS SQL Server database. This is particularly handy when objects have been created by web applications and not set the object owner to dbo.</p>
<p>To view th source code for the stored procedure <span id="more-60"></span></p>
<pre class="brush: sql;">if exists (select * from sysobjects where id = object_id(N'[dbo].[chObjOwner]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)

drop procedure [dbo].[chObjOwner]

GO

 

SET QUOTED_IDENTIFIER  OFF    SET ANSI_NULLS  ON 

GO

 

CREATE proc chObjOwner( @usrName varchar(20), @newUsrName varchar(50))

as

-- @usrName is the current user

-- @newUsrName is the new user

 

set nocount on

declare @uid int                   -- UID of the user

declare @objName varchar(50)       -- Object name owned by user

declare @currObjName varchar(50)   -- Checks for existing object owned by new user 

declare @outStr varchar(256)       -- SQL command with 'sp_changeobjectowner'

set @uid = user_id(@usrName)

 

declare chObjOwnerCur cursor static

for

select name from sysobjects where uid = @uid

 

open chObjOwnerCur

if @@cursor_rows = 0

begin

  print 'Error: No objects owned by ' + @usrName

  close chObjOwnerCur

  deallocate chObjOwnerCur

  return 1

end

 

fetch next from chObjOwnerCur into @objName

 

while @@fetch_status = 0

begin

  set @currObjName = @newUsrName + &quot;.&quot; + @objName

  if (object_id(@currObjName) &gt; 0)

    print 'WARNING *** ' + @currObjName + ' already exists ***'

  set @outStr = &quot;sp_changeobjectowner '&quot; + @usrName + &quot;.&quot; + @objName + &quot;','&quot; + @newUsrName + &quot;'&quot;

  print @outStr

  print 'go'

  fetch next from chObjOwnerCur into @objName

end

 

close chObjOwnerCur

deallocate chObjOwnerCur

set nocount off

return 0

 

 

GO

SET QUOTED_IDENTIFIER  OFF    SET ANSI_NULLS  ON 

GO
</pre>


<p>Related posts:<ol><li><a href='http://www.prolificnotion.co.uk/invalid-object-name-umbracouser/' rel='bookmark' title='Permanent Link: Invalid object name &#8216;umbracoUser&#8217;'>Invalid object name &#8216;umbracoUser&#8217;</a> <small>Yet another problem on an Umbraco v4.0 site I am...</small></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.prolificnotion.co.uk/sql-server-stored-procedure-to-change-object-owner/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
