<?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>nlsimmons.com &#187; Stored Procedures</title>
	<atom:link href="http://nlsimmons.com/?cat=4&#038;feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://nlsimmons.com</link>
	<description>Professional Blog &#38; Online Resume</description>
	<lastBuildDate>Wed, 10 Jun 2015 15:02:01 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=4.2.2</generator>
	<item>
		<title>Altering Stored Procedure Families Programatically</title>
		<link>http://nlsimmons.com/?p=212</link>
		<comments>http://nlsimmons.com/?p=212#comments</comments>
		<pubDate>Thu, 29 Mar 2012 18:41:12 +0000</pubDate>
		<dc:creator><![CDATA[]]></dc:creator>
				<category><![CDATA[Stored Procedures]]></category>
		<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[Automation]]></category>
		<category><![CDATA[Script]]></category>
		<category><![CDATA[Stored Procedure]]></category>

		<guid isPermaLink="false">http://nlsimmons.com/SQLScraps/?p=212</guid>
		<description><![CDATA[Alter every stored procedure in a database that fits a given criteria Recently I had to search for, and update,&#8230;]]></description>
				<content:encoded><![CDATA[            <script type="text/javascript" src="http://nlsimmons.com/wp-content/plugins/wordpress-code-snippet/scripts/shBrushSql.js"></script>
<h2>Alter every stored procedure in a database that fits a given criteria</h2>
<p>Recently I had to search for, and update, hard-coding server references for every stored procedure in a database.  This amounted to altering over 60 stored procedures.  Instead of going through each stored procedure, one-by-one, I used the following code to accomplish the task:</p>
<p><pre class="brush: sql">BEGIN TRANSACTION

CREATE TABLE #ProcsToChange (
	ProcName NVARCHAR(MAX) NULL
)

CREATE TABLE #tblHolding (
	ID INT IDENTITY(1,1) NOT NULL,
	proctext NVARCHAR(MAX) NULL
)

INSERT #ProcsToChange (ProcName)
SELECT DISTINCT OBJECT_NAME(sc.id)
FROM syscomments sc
INNER JOIN sysobjects so 
ON sc.id = so.id
WHERE sc.text LIKE '%[OldServer]%'
ORDER BY OBJECT_NAME(sc.id); 

DECLARE @newProcText NVARCHAR(MAX)
DECLARE @proc NVARCHAR(MAX)

DECLARE c CURSOR FOR
SELECT ProcName FROM #ProcsToChange;

OPEN c;
FETCH NEXT FROM c INTO @proc 

WHILE @@FETCH_STATUS = 0
BEGIN

	INSERT #tblHolding (proctext)
	EXEC sp_helptext @proc
	
	PRINT @proc
	SELECT @newProcText = NULL;
	SELECT @newProcText = COALESCE(@newProcText,'') + proctext FROM #tblHolding ORDER BY ID ASC
	SELECT @newProcText = REPLACE(@newProcText, 'CREATE PROCEDURE', 'ALTER PROCEDURE');
	SELECT @newProcText = REPLACE(@newProcText, '[OldServer]', '[NewServer]');

	--PRINT @newProcText;
	EXEC sp_executesql @newProcText;

	TRUNCATE TABLE #tblHolding
	
	FETCH NEXT FROM c INTO @proc
	
END
CLOSE c;
DEALLOCATE c;

DROP TABLE #ProcsToChange
DROP TABLE #tblHolding

--ROLLBACK
COMMIT
</pre></p>
<p>In order to grab only the stored procedures fitting your criteria, simply manipulate the WHERE clause that is used to filter data being inserted into #ProcsToChange.  </p>
<p>This code should be thoroughly tested in a test environment before using in production.  This code is provided as-is, with no warranty whatsoever.  </p>
<p>
<script type="text/javascript"><!--
google_ad_client = "ca-pub-6201556538459768";
/* SQLScraps - short */
google_ad_slot = "3929371169";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script><br />
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
]]></content:encoded>
			<wfw:commentRss>http://nlsimmons.com/?feed=rss2&#038;p=212</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Scripting Stored Procedure Permissions</title>
		<link>http://nlsimmons.com/?p=104</link>
		<comments>http://nlsimmons.com/?p=104#comments</comments>
		<pubDate>Wed, 06 Oct 2010 09:36:52 +0000</pubDate>
		<dc:creator><![CDATA[]]></dc:creator>
				<category><![CDATA[Permissions]]></category>
		<category><![CDATA[Stored Procedures]]></category>

		<guid isPermaLink="false">http://nlsimmons.com/SQLScraps/?p=7</guid>
		<description><![CDATA[Overview When scripting stored procedure code for deployment to several servers, there are a few things to keep in mind. &#8230;]]></description>
				<content:encoded><![CDATA[            <script type="text/javascript" src="http://nlsimmons.com/wp-content/plugins/wordpress-code-snippet/scripts/shBrushSql.js"></script>
<h4>Overview</h4>
<p>When scripting stored procedure code for deployment to several servers, there are a few things to keep in mind.  One of those things is the permissions associated with the procedure you&#8217;re deploying.  When deploying a brand new stored procedure, the process of assigning permissions is fairly straightforward.  If you need to deploy an update to an existing stored procedure, things get a bit more complicated.  If you simply issue a DROP PROCEDURE call followed by the CREATE PROCEDURE code, you will lose the permissions that are associated with the stored procedure.  Issuing an ALTER PROCEDURE command will preserve the permissions but will cause you trouble if there are places where the procedure doesn&#8217;t yet exist.  To solve this issue, you should script out the permissions on a procedure to a temp table before dropping the procedure.  If there is a chance that the batch will fail, use a persisted table or a global temp table instead.  Once the proc is recreated, run through the permissions and apply them to the proc again.  This way, you&#8217;ve preserved the permissions on the proc.</p>
<p>Now, there are some pitfalls to this approach.  If the proc didn&#8217;t already exist when you run the deploy script, there will obviously be no permissions to script out to a temp table.  So, you might run into the problem where the same proc on two different databases has two sets of permissions.  This might be an issue, but considering the proc didn&#8217;t exist in the first place, it&#8217;s doubtful that it would be an issue.  Nevertheless it is something to keep in mind.</p>
<p>The way to get around this pitfall is to include desired the permissions in the SQL script used to create the procedure.  This way, the newly created proc will have the permissions it had before, in addition to any included in the deployment script.</p>
<h4>The Code</h4>
<p>Here is some sample SQL for scripting the permissions into a temp table:</p>
<p><pre class="brush: sql">IF EXISTS (SELECT * FROM sys.objects WHERE name = 'SampleProc')
BEGIN
	IF OBJECT_ID('tempdb..#tmp_PermissionsWork') IS NOT NULL DROP TABLE #tmp_PermissionsWork
	CREATE TABLE #tmp_PermissionsWork (
		  ID INT IDENTITY(1,1) PRIMARY KEY CLUSTERED NOT NULL 
		, Permission NVARCHAR(MAX) NULL
	)
	
	INSERT #tmp_PermissionsWork (Permission)
	SELECT state_desc + ' ' + permission_name + ' ON ' + OBJECT_NAME(major_id) + ' TO ' + QUOTENAME(USER_NAME(grantee_principal_id)) + ';' FROM sys.database_permissions 
	WHERE major_id = OBJECT_ID('SampleProc')

	DROP PROCEDURE SampleProc
END
GO

CREATE PROCEDURE SampleProc
AS
...
GO

DECLARE @cmd NVARCHAR(MAX)

WHILE EXISTS(SELECT 1 FROM #tmp_PermissionsWork)
BEGIN
	SELECT TOP 1 @cmd = Permission
	FROM #tmp_PermissionsWork
	
	EXEC sp_executesql @cmd;
	
	DELETE FROM #tmp_PermissionsWork 
	WHERE Permission = @cmd
END
GO</pre></p>
<pre>
IF EXISTS (SELECT * FROM sys.objects WHERE name = 'SampleProc')
BEGIN
	IF OBJECT_ID('tempdb..#tmp_PermissionsWork') IS NOT NULL DROP TABLE #tmp_PermissionsWork
	CREATE TABLE #tmp_PermissionsWork (
		  ID INT IDENTITY(1,1) PRIMARY KEY CLUSTERED NOT NULL 
		, Permission NVARCHAR(MAX) NULL
	)
	
	INSERT #tmp_PermissionsWork (Permission)
	SELECT state_desc + ' ' + permission_name + ' ON ' + OBJECT_NAME(major_id) + ' TO ' + QUOTENAME(USER_NAME(grantee_principal_id)) + ';' FROM sys.database_permissions 
	WHERE major_id = OBJECT_ID('SampleProc')

	DROP PROCEDURE SampleProc
END
GO

CREATE PROCEDURE SampleProc
AS
...
GO

DECLARE @cmd NVARCHAR(MAX)

WHILE EXISTS(SELECT 1 FROM #tmp_PermissionsWork)
BEGIN
	SELECT TOP 1 @cmd = Permission
	FROM #tmp_PermissionsWork
	
	EXEC sp_executesql @cmd;
	
	DELETE FROM #tmp_PermissionsWork 
	WHERE Permission = @cmd
END
GO
</pre>
<p>To use this code, swap out &#8216;SampleProc&#8217; with your stored procedure and put your procedure code in place of lines 17 &#8211; 20.</p>
<p><font face=arial size=1>*This code is provided &#8220;AS-IS&#8221; with no warranty of any kind.</font></p>
]]></content:encoded>
			<wfw:commentRss>http://nlsimmons.com/?feed=rss2&#038;p=104</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
