<?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>grokgarble.com &#187; SQL</title>
	<atom:link href="http://jeffmurr.com/blog/?feed=rss2&#038;tag=sql" rel="self" type="application/rss+xml" />
	<link>http://jeffmurr.com/blog</link>
	<description>Database, Software, and System Engineering</description>
	<lastBuildDate>Tue, 26 Sep 2017 19:07:40 +0000</lastBuildDate>
	<language>en-US</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=3.9.30</generator>
	<item>
		<title>Grant Execute Permission on All Stored Procedures in a SQL Database</title>
		<link>http://jeffmurr.com/blog/?p=8</link>
		<comments>http://jeffmurr.com/blog/?p=8#comments</comments>
		<pubDate>Sat, 26 Jan 2013 21:38:50 +0000</pubDate>
		<dc:creator><![CDATA[Jeff Murr]]></dc:creator>
				<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://jeffmurr.com/blog/?p=8</guid>
		<description><![CDATA[First blog post.  I&#8217;ll make it short, simple, and [hopefully] sweet. I like it when bloggers have the solution easy to find at the top and then explain in a little more detail below.  I&#8217;ll try following that as much as possible. Recently, I was asked to blanket all user stored procedures.  The practicality of this [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>First blog post.  I&#8217;ll make it short, simple, and [hopefully] sweet.</p>
<p>I like it when bloggers have the solution easy to find at the top and then explain in a little more detail below.  I&#8217;ll try following that as much as possible.</p>
<p>Recently, I was asked to blanket all user stored procedures.  The practicality of this may be limited, but a useful trick and I thought I&#8217;d share along with breaking it down.</p>
<p>SQL 2000</p>
<pre class="brush:sql">USE [DataBaseName]
DECLARE Proc_Cursor CURSOR
FOR SELECT NAME FROM sysobjects WHERE objectproperty( id, N'IsMSShipped' ) = 0
AND objectproperty( id, N'IsProcedure' ) = 1
OPEN Proc_Cursor
DECLARE @Name VARCHAR(500)
DECLARE @Query VARCHAR(500)
FETCH NEXT FROM Proc_Cursor INTO @Name;
WHILE @@FETCH_STATUS = 0
BEGIN
  SET @Query = 'GRANT EXEC ON ' + @Name + ' TO [CustomRole]'
  EXEC(@Query)
  FETCH NEXT FROM Proc_Cursor INTO @Name;
END
CLOSE Proc_Cursor;
DEALLOCATE Proc_Cursor;</pre>
<p><code><br />
</code></p>
<p>SQL 2005/SQL 2008/2008 R2</p>
<pre class="brush:sql">USE [DataBaseName]
DECLARE Proc_Cursor CURSOR
FOR SELECT NAME FROM sys.objects WHERE type = 'P' AND is_ms_shipped = '0'
OPEN Proc_Cursor
DECLARE @Name VARCHAR(500)
DECLARE @Query VARCHAR(500)
FETCH NEXT FROM Proc_Cursor INTO @Name;
WHILE @@FETCH_STATUS = 0
BEGIN
  SET @Query = 'GRANT EXEC ON ' + @Name + ' TO [CustomRole]'
  EXEC(@Query)
  FETCH NEXT FROM Proc_Cursor INTO @Name;
END
CLOSE Proc_Cursor;
DEALLOCATE Proc_Cursor</pre>
<p>I&#8217;ll break the above methods down for in the 2008 version to simplify reading.</p>
<p>First, we create a cursor of all objects that are stored procedures that are user defined.</p>
<blockquote>
<pre class="brush:sql">DECLARE Proc_Cursor CURSOR
FOR SELECT NAME FROM sys.objects WHERE type = 'P' AND is_ms_shipped = '0'</pre>
</blockquote>
<p>Next, open the cursor and work each stroed proc captured in the above select statement with a WHILE loop, creating dynamic SQL.  The dynamic SQL is needed since you can&#8217;t execute a GRANT command with a variable out right:</p>
<blockquote>
<pre class="brush:sql">OPEN Proc_Cursor
DECLARE @Name VARCHAR(500)
DECLARE @Query VARCHAR(500)
FETCH NEXT FROM Proc_Cursor INTO @Name;
WHILE @@FETCH_STATUS = 0
BEGIN
  SET @Query = 'GRANT EXEC ON ' + @Name + ' TO [CustomRole]'</pre>
</blockquote>
<p>Execute our new Query each time we find a new stored proc in our cursor then fetch the next:</p>
<blockquote>
<pre class="brush:sql">  EXEC(@Query)
  FETCH NEXT FROM Proc_Cursor INTO @Name;
END</pre>
</blockquote>
<p>When we&#8217;re done, make sure to close your cursor and deallocate its resources:</p>
<blockquote>
<pre class="brush:sql">CLOSE Proc_Cursor;
DEALLOCATE Proc_Cursor;</pre>
</blockquote>
<p>Happy granting&#8230;</p>
<div data-counters='1' data-style='square' data-size='regular' data-url='http://jeffmurr.com/blog/?p=8' data-title='Grant Execute Permission on All Stored Procedures in a SQL Database' class='linksalpha_container linksalpha_app_3'><a href='//www.linksalpha.com/share?network='facebook' class='linksalpha_icon_facebook'></a><a href='//www.linksalpha.com/share?network='twitter' class='linksalpha_icon_twitter'></a><a href='//www.linksalpha.com/share?network='googleplus' class='linksalpha_icon_googleplus'></a><a href='//www.linksalpha.com/share?network='mail' class='linksalpha_icon_mail'></a></div><div data-position='' data-url='http://jeffmurr.com/blog/?p=8' data-title='Grant Execute Permission on All Stored Procedures in a SQL Database' class='linksalpha_container linksalpha_app_7'><a href='//www.linksalpha.com/share?network='facebook' class='linksalpha_icon_facebook'></a><a href='//www.linksalpha.com/share?network='twitter' class='linksalpha_icon_twitter'></a><a href='//www.linksalpha.com/share?network='googleplus' class='linksalpha_icon_googleplus'></a><a href='//www.linksalpha.com/share?network='mail' class='linksalpha_icon_mail'></a></div>
<!-- NgfbSharing::get_buttons content filter skipped: buttons not allowed in rss feeds -->
]]></content:encoded>
			<wfw:commentRss>http://jeffmurr.com/blog/?feed=rss2&#038;p=8</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
