<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://sqlblog.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Piotr Rodak</title><link>http://sqlblog.com/blogs/piotr_rodak/default.aspx</link><description>&lt;h2 id="postToolbar" class="CommonSubTitle"&gt;if datepart(dw, getdate()) in (6, 7)&lt;br /&gt;
	&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;b&gt;use pubs&lt;/b&gt;;&lt;/h2&gt;&lt;br /&gt;&lt;br /&gt;</description><dc:language>en</dc:language><generator>CommunityServer 2.1 SP2 (Build: 61129.1)</generator><item><title>Hadoop growing pains</title><link>http://sqlblog.com/blogs/piotr_rodak/archive/2012/06/21/hadoop-growing-pains.aspx</link><pubDate>Thu, 21 Jun 2012 20:45:56 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:44001</guid><dc:creator>Piotr Rodak</dc:creator><slash:comments>4</slash:comments><comments>http://sqlblog.com/blogs/piotr_rodak/comments/44001.aspx</comments><wfw:commentRss>http://sqlblog.com/blogs/piotr_rodak/commentrss.aspx?PostID=44001</wfw:commentRss><description>&lt;p&gt;This post is not going to be about SQL Server. I have been reading recently more and more about “Big Data” – very catchy term that describes untamed increase of the data that mankind is producing each day and the struggle to capture the meaning of these data. Ten years ago, and perhaps even three years ago this need was not so recognized. Increasing number of smartphones and discernable trend of mainstream Internet traffic moving to the smartphone generated one means that there is bigger and bigger stream of information that has to be stored, transformed, analysed and perhaps monetized. The nature of this traffic makes if very difficult to wrap it into boundaries of relational database engines. The amount of data makes it near to impossible to process them in relational databases within reasonable time. This is where ‘cloud’ technologies come to play.&lt;/p&gt;  &lt;p&gt;I just read a good article about the &lt;a href="http://ovum.com/2012/06/21/tooling-is-starting-to-tame-hadoop/" target="_blank"&gt;growing pains of Hadoop&lt;/a&gt;, which became one of the leading players on distributed processing arena within last year or two. Toby Baer concludes in it that lack of enterprise ready toolsets hinders Hadoop’s apprehension in the enterprise world. While this is true, something else drew my attention. According to the article &lt;u&gt;there are already about half of a dozen of commercially supported distributions of Hadoop&lt;/u&gt;. For me, who has not been involved into intricacies of open-source world, this is quite interesting observation. On one hand, it is good that there is competition as it is beneficial in the end to the customer. On the other hand, the customer is faced with difficulty of choosing the right distribution. In future, when Hadoop distributions fork even more, this choice will be even harder. The distributions will have overlapping sets of features, yet will be quite incompatible with each other. I suppose it will take a few years until leaders emerge and the market will begin to resemble what we see in Linux world. There are myriads of distributions, but only few are acknowledged by the industry as enterprise standard. Others are honed by bearded individuals with too much time to spend.&lt;/p&gt;  &lt;p&gt;In any way, the third fact I can’t help but notice about the proliferation of distributions of Hadoop is that IT professionals will have jobs.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;   &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:1c504cf7-cd4a-41b6-a76e-896b7e8a80ea" class="wlWriterEditableSmartContent"&gt;BuzzNet Tags: &lt;a href="http://www.buzznet.com/tags/Hadoop" rel="tag"&gt;Hadoop&lt;/a&gt;,&lt;a href="http://www.buzznet.com/tags/Big+Data" rel="tag"&gt;Big Data&lt;/a&gt;,&lt;a href="http://www.buzznet.com/tags/Enterprise+IT" rel="tag"&gt;Enterprise IT&lt;/a&gt;&lt;/div&gt;&lt;/p&gt;&lt;img src="http://sqlblog.com/aggbug.aspx?PostID=44001" width="1" height="1"&gt;</description><category domain="http://sqlblog.com/blogs/piotr_rodak/archive/tags/Big+Data/default.aspx">Big Data</category><category domain="http://sqlblog.com/blogs/piotr_rodak/archive/tags/Cloud/default.aspx">Cloud</category><category domain="http://sqlblog.com/blogs/piotr_rodak/archive/tags/Hadoop/default.aspx">Hadoop</category><category domain="http://sqlblog.com/blogs/piotr_rodak/archive/tags/nosql/default.aspx">nosql</category></item><item><title>Running SSIS packages from C#</title><link>http://sqlblog.com/blogs/piotr_rodak/archive/2012/04/09/running-ssis-packages-from-c.aspx</link><pubDate>Mon, 09 Apr 2012 20:52:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:42749</guid><dc:creator>Piotr Rodak</dc:creator><slash:comments>4</slash:comments><comments>http://sqlblog.com/blogs/piotr_rodak/comments/42749.aspx</comments><wfw:commentRss>http://sqlblog.com/blogs/piotr_rodak/commentrss.aspx?PostID=42749</wfw:commentRss><description>&lt;p&gt;Most of the developers and DBAs know about two ways of deploying packages: You can deploy them to database server and run them using SQL Server Agent job or you can deploy the packages to file system and run them using dtexec.exe utility. Both approaches have their pros and cons. However I would like to show you that there is a third way (sort of) that is often overlooked, and it can give you capabilities the ‘traditional’ approaches can’t.&lt;/p&gt;  &lt;p&gt;I have been working for a few years with applications that run packages from host applications that are implemented in .NET. As you know, SSIS provides &lt;a target="_blank" href="http://msdn.microsoft.com/en-us/library/ms136025.aspx"&gt;programming model&lt;/a&gt; that you can use to implement more flexible solutions. SSIS applications are usually thought to be batch oriented, with fairly rigid architecture and processing model, with fixed timeframes when the packages are executed to process data. It doesn’t to be the case, you don’t have to limit yourself to batch oriented architecture. I have very good experiences with service oriented architectures processing large amounts of data. These applications are more complex than what I would like to show here, but the principle stays the same: you can execute packages as a service, on ad-hoc basis. You can also implement and schedule various signals, HTTP calls, file drops, time schedules, Tibco messages and other to run the packages. You can implement event handler that will trigger execution of SSIS when a certain event occurs in &lt;a target="_blank" href="http://www.microsoft.com/sqlserver/en/us/solutions-technologies/business-intelligence/complex-event-processing.aspx"&gt;StreamInsight&lt;/a&gt; stream.&lt;/p&gt;  &lt;p&gt;This post is just a small example of how you can use the API and other features to create a service that can run SSIS packages on demand.&lt;/p&gt;  &lt;p&gt;I thought it might be a good idea to implement a &lt;a target="_blank" href="http://en.wikipedia.org/wiki/Representational_state_transfer"&gt;restful service&lt;/a&gt; that would listen to requests and execute appropriate actions. As it turns out, it is trivial in C#. The application is implemented as console application for the ease of debugging and running. In reality, you might want to implement the application as Windows service. To begin, you have to reference namespace &lt;a target="_blank" href="http://msdn.microsoft.com/en-us/library/system.servicemodel.web.aspx"&gt;System.ServiceModel.Web&lt;/a&gt; and then add a few lines of code:&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:91282449-2cdd-43d7-ba11-f7bb27562907" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#ddd;max-height:400px;overflow:auto;"&gt; &lt;ol style="background:#000000;margin:0 0 0 2.5em;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#678cb1;"&gt;Uri&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt; baseAddress &lt;/span&gt;&lt;span style="color:#e8e2b7;"&gt;=&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt; &lt;/span&gt;&lt;span style="color:#93c763;"&gt;new&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt; &lt;/span&gt;&lt;span style="color:#678cb1;"&gt;Uri&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt;(&lt;/span&gt;&lt;span style="color:#ec7600;"&gt;"http://localhost:8011/"&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt;);&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#0c0c0c;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#f1f2f3;"&gt;&lt;/span&gt;&lt;span style="color:#678cb1;"&gt;WebServiceHost&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt; svcHost &lt;/span&gt;&lt;span style="color:#e8e2b7;"&gt;=&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt; &lt;/span&gt;&lt;span style="color:#93c763;"&gt;new&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt; &lt;/span&gt;&lt;span style="color:#678cb1;"&gt;WebServiceHost&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt;(&lt;/span&gt;&lt;span style="color:#93c763;"&gt;typeof&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt;(&lt;/span&gt;&lt;span style="color:#678cb1;"&gt;PackRunner&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt;), baseAddress);&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#0c0c0c;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#f1f2f3;"&gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#f1f2f3;"&gt;&lt;/span&gt;&lt;span style="color:#93c763;"&gt;try&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#0c0c0c;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#f1f2f3;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#f1f2f3;"&gt;&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt;svcHost&lt;/span&gt;&lt;span style="color:#e8e2b7;"&gt;.&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt;Open();&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#0c0c0c;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#f1f2f3;"&gt;&lt;/span&gt;&lt;span style="color:#678cb1;"&gt;Console&lt;/span&gt;&lt;span style="color:#e8e2b7;"&gt;.&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt;WriteLine(&lt;/span&gt;&lt;span style="color:#ec7600;"&gt;"Service is running"&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt;);&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#0c0c0c;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#f1f2f3;"&gt;&lt;/span&gt;&lt;span style="color:#678cb1;"&gt;Console&lt;/span&gt;&lt;span style="color:#e8e2b7;"&gt;.&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt;WriteLine(&lt;/span&gt;&lt;span style="color:#ec7600;"&gt;"Press enter to stop the service."&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt;);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#f1f2f3;"&gt;&lt;/span&gt;&lt;span style="color:#678cb1;"&gt;Console&lt;/span&gt;&lt;span style="color:#e8e2b7;"&gt;.&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt;ReadLine();&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#0c0c0c;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#f1f2f3;"&gt;svcHost&lt;/span&gt;&lt;span style="color:#e8e2b7;"&gt;.&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt;Close();&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#0c0c0c;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#f1f2f3;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#f1f2f3;"&gt;&lt;/span&gt;&lt;span style="color:#93c763;"&gt;catch&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt; (&lt;/span&gt;&lt;span style="color:#678cb1;"&gt;CommunicationException&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt; cex)&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#0c0c0c;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#f1f2f3;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#f1f2f3;"&gt;&lt;/span&gt;&lt;span style="color:#678cb1;"&gt;Console&lt;/span&gt;&lt;span style="color:#e8e2b7;"&gt;.&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt;WriteLine(&lt;/span&gt;&lt;span style="color:#ec7600;"&gt;"An exception occurred: {0}"&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt;, cex&lt;/span&gt;&lt;span style="color:#e8e2b7;"&gt;.&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt;Message);&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#0c0c0c;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#f1f2f3;"&gt;svcHost&lt;/span&gt;&lt;span style="color:#e8e2b7;"&gt;.&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt;Abort();&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#f1f2f3;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;The interesting lines are 3, 7 and 13. In line 3 you create a WebServiceHost object. In line 7 you start listening on the defined URL and then in line 13 you shut down the service.&lt;/p&gt;  &lt;p&gt;As you have noticed, the WebServiceHost constructor is accepting type of an object (here: &lt;b&gt;PackRunner&lt;/b&gt;) that will be instantiated as singleton and subsequently used to process the requests. This is the class where you put your logic, but to tell WebServiceHost how to use it, the class must implement an interface which declares methods to be used by the host. The interface itself must be ornamented with attribute &lt;a target="_blank" href="http://msdn.microsoft.com/en-us/library/system.servicemodel.servicecontractattribute.aspx"&gt;ServiceContract&lt;/a&gt;.&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:2a1df341-963e-4cc3-a758-d605c900dbaa" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#ddd;max-height:400px;overflow:auto;"&gt; &lt;ol style="background:#000000;margin:0 0 0 2.5em;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#f1f2f3;"&gt;[&lt;/span&gt;&lt;span style="color:#678cb1;"&gt;ServiceContract&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#0c0c0c;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#f1f2f3;"&gt;&lt;/span&gt;&lt;span style="color:#93c763;"&gt;public&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt; &lt;/span&gt;&lt;span style="color:#93c763;"&gt;interface&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt; &lt;/span&gt;&lt;span style="color:#8c8cb4;"&gt;IPackRunner&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#f1f2f3;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#0c0c0c;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#f1f2f3;"&gt;&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt;[&lt;/span&gt;&lt;span style="color:#678cb1;"&gt;OperationContract&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#f1f2f3;"&gt;[&lt;/span&gt;&lt;span style="color:#678cb1;"&gt;WebGet&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt;(UriTemplate &lt;/span&gt;&lt;span style="color:#e8e2b7;"&gt;=&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt; &lt;/span&gt;&lt;span style="color:#ec7600;"&gt;"runpack?package={name}"&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt;)]&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#0c0c0c;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#f1f2f3;"&gt;&lt;/span&gt;&lt;span style="color:#93c763;"&gt;string&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt; RunPackage1(&lt;/span&gt;&lt;span style="color:#93c763;"&gt;string&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt; name);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#0c0c0c;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#f1f2f3;"&gt;[&lt;/span&gt;&lt;span style="color:#678cb1;"&gt;OperationContract&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#f1f2f3;"&gt;[&lt;/span&gt;&lt;span style="color:#678cb1;"&gt;WebGet&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt;(UriTemplate &lt;/span&gt;&lt;span style="color:#e8e2b7;"&gt;=&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt; &lt;/span&gt;&lt;span style="color:#ec7600;"&gt;"runpackwithparams?package={name}&amp;amp;rows={rows}"&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt;)]&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#0c0c0c;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#f1f2f3;"&gt;&lt;/span&gt;&lt;span style="color:#93c763;"&gt;string&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt; RunPackage2(&lt;/span&gt;&lt;span style="color:#93c763;"&gt;string&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt; name, &lt;/span&gt;&lt;span style="color:#93c763;"&gt;int&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt; rows);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#f1f2f3;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;Each method that is going to be used by WebServiceHost has to have attribute &lt;a target="_blank" href="http://msdn.microsoft.com/en-us/library/system.servicemodel.operationcontractattribute.aspx"&gt;OperationContract&lt;/a&gt;, as well as &lt;a target="_blank" href="http://msdn.microsoft.com/en-us/library/system.servicemodel.web.webgetattribute.aspx"&gt;WebGet&lt;/a&gt; or &lt;a target="_blank" href="http://msdn.microsoft.com/en-us/library/system.servicemodel.web.webinvokeattribute.aspx"&gt;WebInvoke&lt;/a&gt; attribute. The detailed discussion of the available options is outside of scope of this post. I also recommend using more descriptive names to methods &lt;img style="border-bottom-style:none;border-left-style:none;border-top-style:none;border-right-style:none;" class="wlEmoticon wlEmoticon-smile" alt="Smile" src="http://sqlblog.com/blogs/piotr_rodak/wlEmoticon-smile_48ABAEFB.png"&gt;.&lt;/p&gt;  &lt;p&gt;Then, you have to provide the implementation of the interface:&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:18fd6c19-65d6-464c-9d4f-cf767f0edd95" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#ddd;max-height:400px;overflow:auto;"&gt; &lt;ol style="background:#000000;margin:0 0 0 2em;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#93c763;"&gt;public&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt; &lt;/span&gt;&lt;span style="color:#93c763;"&gt;class&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt; &lt;/span&gt;&lt;span style="color:#678cb1;"&gt;PackRunner&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt; : &lt;/span&gt;&lt;span style="color:#8c8cb4;"&gt;IPackRunner&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#0c0c0c;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#f1f2f3;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#f1f2f3;"&gt;&lt;/span&gt;&lt;span style="color:#e8e2b7;"&gt;...&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;There are two methods defined in this class. I think that since the full code is attached to the post, I will show only the more interesting method, the RunPackage2.&lt;/p&gt;  &lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:4cea7e45-0c65-4bf4-9a6d-2280b5e0448d" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#ddd;max-height:500px;overflow:auto;"&gt; &lt;ol style="background:#000000;margin:0 0 0 2.5em;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#808080;"&gt;///&lt;/span&gt;&lt;span style="color:#99a38a;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#0c0c0c;"&gt;&lt;span style="color:#f1f2f3;"&gt;&lt;/span&gt;&lt;span style="color:#808080;"&gt;///&lt;/span&gt;&lt;span style="color:#99a38a;"&gt; Runs package and sets some of its variables.&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#f1f2f3;"&gt;&lt;/span&gt;&lt;span style="color:#808080;"&gt;///&lt;/span&gt;&lt;span style="color:#99a38a;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#0c0c0c;"&gt;&lt;span style="color:#f1f2f3;"&gt;&lt;/span&gt;&lt;span style="color:#808080;"&gt;///&lt;/span&gt;&lt;span style="color:#99a38a;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;&amp;lt;param name="name"&amp;gt;&lt;/span&gt;&lt;span style="color:#99a38a;"&gt;Name of the package&lt;/span&gt;&lt;span style="color:#808080;"&gt;&amp;lt;/param&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#f1f2f3;"&gt;&lt;/span&gt;&lt;span style="color:#808080;"&gt;///&lt;/span&gt;&lt;span style="color:#99a38a;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;&amp;lt;param name="rows"&amp;gt;&lt;/span&gt;&lt;span style="color:#99a38a;"&gt;Number of rows to export&lt;/span&gt;&lt;span style="color:#808080;"&gt;&amp;lt;/param&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#0c0c0c;"&gt;&lt;span style="color:#f1f2f3;"&gt;&lt;/span&gt;&lt;span style="color:#808080;"&gt;///&lt;/span&gt;&lt;span style="color:#99a38a;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;&amp;lt;returns&amp;gt;&amp;lt;/returns&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#f1f2f3;"&gt;&lt;/span&gt;&lt;span style="color:#93c763;"&gt;public&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt; &lt;/span&gt;&lt;span style="color:#93c763;"&gt;string&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt; RunPackage2(&lt;/span&gt;&lt;span style="color:#93c763;"&gt;string&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt; name, &lt;/span&gt;&lt;span style="color:#93c763;"&gt;int&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt; rows)&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#0c0c0c;"&gt;&lt;span style="color:#f1f2f3;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#f1f2f3;"&gt;&lt;/span&gt;&lt;span style="color:#93c763;"&gt;try&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#0c0c0c;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#f1f2f3;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#f1f2f3;"&gt;&lt;/span&gt;&lt;span style="color:#93c763;"&gt;string&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt; pkgLocation &lt;/span&gt;&lt;span style="color:#e8e2b7;"&gt;=&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt; &lt;/span&gt;&lt;span style="color:#678cb1;"&gt;ConfigurationManager&lt;/span&gt;&lt;span style="color:#e8e2b7;"&gt;.&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt;AppSettings[&lt;/span&gt;&lt;span style="color:#ec7600;"&gt;"PackagePath"&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt;];&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#0c0c0c;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#f1f2f3;"&gt;pkgLocation &lt;/span&gt;&lt;span style="color:#e8e2b7;"&gt;=&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt; &lt;/span&gt;&lt;span style="color:#678cb1;"&gt;Path&lt;/span&gt;&lt;span style="color:#e8e2b7;"&gt;.&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt;Combine(pkgLocation, name&lt;/span&gt;&lt;span style="color:#e8e2b7;"&gt;.&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt;Replace(&lt;/span&gt;&lt;span style="color:#ec7600;"&gt;"\""&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt;, &lt;/span&gt;&lt;span style="color:#ec7600;"&gt;""&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt;));&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#0c0c0c;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#f1f2f3;"&gt;&lt;/span&gt;&lt;span style="color:#678cb1;"&gt;Console&lt;/span&gt;&lt;span style="color:#e8e2b7;"&gt;.&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt;WriteLine();&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#0c0c0c;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#f1f2f3;"&gt;&lt;/span&gt;&lt;span style="color:#678cb1;"&gt;Console&lt;/span&gt;&lt;span style="color:#e8e2b7;"&gt;.&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt;WriteLine(&lt;/span&gt;&lt;span style="color:#ec7600;"&gt;"Calling package {0} with parameter {1}."&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt;, name, rows);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#f1f2f3;"&gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#0c0c0c;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#f1f2f3;"&gt;&lt;/span&gt;&lt;span style="color:#678cb1;"&gt;Application&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt; app &lt;/span&gt;&lt;span style="color:#e8e2b7;"&gt;=&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt; &lt;/span&gt;&lt;span style="color:#93c763;"&gt;new&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt; &lt;/span&gt;&lt;span style="color:#678cb1;"&gt;Application&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt;();&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#f1f2f3;"&gt;&lt;/span&gt;&lt;span style="color:#678cb1;"&gt;Package&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt; pkg &lt;/span&gt;&lt;span style="color:#e8e2b7;"&gt;=&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt; app&lt;/span&gt;&lt;span style="color:#e8e2b7;"&gt;.&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt;LoadPackage(pkgLocation, &lt;/span&gt;&lt;span style="color:#93c763;"&gt;null&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt;);&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#0c0c0c;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#f1f2f3;"&gt;pkg&lt;/span&gt;&lt;span style="color:#e8e2b7;"&gt;.&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt;Variables[&lt;/span&gt;&lt;span style="color:#ec7600;"&gt;"User::ExportRows"&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt;]&lt;/span&gt;&lt;span style="color:#e8e2b7;"&gt;.&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt;Value &lt;/span&gt;&lt;span style="color:#e8e2b7;"&gt;=&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt; rows;&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#0c0c0c;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#f1f2f3;"&gt;&lt;/span&gt;&lt;span style="color:#678cb1;"&gt;DTSExecResult&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt; pkgResults &lt;/span&gt;&lt;span style="color:#e8e2b7;"&gt;=&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt; pkg&lt;/span&gt;&lt;span style="color:#e8e2b7;"&gt;.&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt;Execute();&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#f1f2f3;"&gt;&lt;/span&gt;&lt;span style="color:#678cb1;"&gt;Console&lt;/span&gt;&lt;span style="color:#e8e2b7;"&gt;.&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt;WriteLine();&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#0c0c0c;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#f1f2f3;"&gt;&lt;/span&gt;&lt;span style="color:#678cb1;"&gt;Console&lt;/span&gt;&lt;span style="color:#e8e2b7;"&gt;.&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt;WriteLine(pkgResults&lt;/span&gt;&lt;span style="color:#e8e2b7;"&gt;.&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt;ToString()); &lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#f1f2f3;"&gt;&lt;/span&gt;&lt;span style="color:#93c763;"&gt;if&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt; (pkgResults &lt;/span&gt;&lt;span style="color:#e8e2b7;"&gt;==&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt; &lt;/span&gt;&lt;span style="color:#678cb1;"&gt;DTSExecResult&lt;/span&gt;&lt;span style="color:#e8e2b7;"&gt;.&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt;Failure)&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#0c0c0c;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#f1f2f3;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#f1f2f3;"&gt;&lt;/span&gt;&lt;span style="color:#678cb1;"&gt;Console&lt;/span&gt;&lt;span style="color:#e8e2b7;"&gt;.&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt;WriteLine();&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#0c0c0c;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#f1f2f3;"&gt;&lt;/span&gt;&lt;span style="color:#678cb1;"&gt;Console&lt;/span&gt;&lt;span style="color:#e8e2b7;"&gt;.&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt;WriteLine(&lt;/span&gt;&lt;span style="color:#ec7600;"&gt;"Errors occured during execution of the package:"&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt;);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#f1f2f3;"&gt;&lt;/span&gt;&lt;span style="color:#93c763;"&gt;foreach&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt; (&lt;/span&gt;&lt;span style="color:#678cb1;"&gt;DtsError&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt; er &lt;/span&gt;&lt;span style="color:#93c763;"&gt;in&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt; pkg&lt;/span&gt;&lt;span style="color:#e8e2b7;"&gt;.&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt;Errors)&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#0c0c0c;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#f1f2f3;"&gt;&lt;/span&gt;&lt;span style="color:#678cb1;"&gt;Console&lt;/span&gt;&lt;span style="color:#e8e2b7;"&gt;.&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt;WriteLine(&lt;/span&gt;&lt;span style="color:#ec7600;"&gt;"{0}: {1}"&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt;, er&lt;/span&gt;&lt;span style="color:#e8e2b7;"&gt;.&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt;ErrorCode, er&lt;/span&gt;&lt;span style="color:#e8e2b7;"&gt;.&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt;Description);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#f1f2f3;"&gt;&lt;/span&gt;&lt;span style="color:#678cb1;"&gt;Console&lt;/span&gt;&lt;span style="color:#e8e2b7;"&gt;.&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt;WriteLine();&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#0c0c0c;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#f1f2f3;"&gt;&lt;/span&gt;&lt;span style="color:#93c763;"&gt;return&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt; &lt;/span&gt;&lt;span style="color:#ec7600;"&gt;"Errors occured during execution. Contact your support."&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt;;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#f1f2f3;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#0c0c0c;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#f1f2f3;"&gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#f1f2f3;"&gt;&lt;/span&gt;&lt;span style="color:#678cb1;"&gt;Console&lt;/span&gt;&lt;span style="color:#e8e2b7;"&gt;.&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt;WriteLine();&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#0c0c0c;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#f1f2f3;"&gt;&lt;/span&gt;&lt;span style="color:#678cb1;"&gt;Console&lt;/span&gt;&lt;span style="color:#e8e2b7;"&gt;.&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt;WriteLine();&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#f1f2f3;"&gt;&lt;/span&gt;&lt;span style="color:#93c763;"&gt;return&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt; &lt;/span&gt;&lt;span style="color:#ec7600;"&gt;"OK"&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt;;&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#0c0c0c;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#f1f2f3;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#f1f2f3;"&gt;&lt;/span&gt;&lt;span style="color:#93c763;"&gt;catch&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt; (&lt;/span&gt;&lt;span style="color:#678cb1;"&gt;Exception&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt; ex)&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#0c0c0c;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#f1f2f3;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#f1f2f3;"&gt;&lt;/span&gt;&lt;span style="color:#678cb1;"&gt;Console&lt;/span&gt;&lt;span style="color:#e8e2b7;"&gt;.&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt;WriteLine(ex);&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#0c0c0c;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#f1f2f3;"&gt;&lt;/span&gt;&lt;span style="color:#93c763;"&gt;return&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt; ex&lt;/span&gt;&lt;span style="color:#e8e2b7;"&gt;.&lt;/span&gt;&lt;span style="color:#f1f2f3;"&gt;ToString();&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#f1f2f3;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#0c0c0c;"&gt;&lt;span style="color:#f1f2f3;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt;  &lt;p&gt;The method accepts package name and number of rows to export. The packages are deployed to the file system. The path to the packages is configured in the application configuration file. This way, you can implement multiple services on the same machine, provided you also configure the URL for each instance appropriately.&lt;/p&gt;  &lt;p&gt;To run a package, you have to reference &lt;a target="_blank" href="http://msdn.microsoft.com/en-us/library/microsoft.sqlserver.dts.runtime.aspx"&gt;Microsoft.SqlServer.Dts.Runtime&lt;/a&gt; namespace. This namespace is implemented in Microsoft.SQLServer.ManagedDTS.dll which in my case was installed in the folder “&lt;i&gt;C:\Program Files (x86)\Microsoft SQL Server\100\SDK\Assemblies&lt;/i&gt;”. Once you have done it, you can create an instance of &lt;b&gt;Microsoft.SqlServer.Dts.Runtime.Application&lt;/b&gt; as in line 18 in the above snippet. It may be a good idea to create the Application object in the constructor of the PackRunner class, to avoid necessity of recreating it each time the service is invoked. Then, in line 19 you see that an instance of &lt;a target="_blank" href="http://msdn.microsoft.com/en-us/library/microsoft.sqlserver.dts.runtime.package.aspx"&gt;Microsoft.SqlServer.Dts.Runtime.Package&lt;/a&gt; is created. The method LoadPackage in its simplest form just takes package file name as the first parameter.&lt;/p&gt;  &lt;p&gt;Before you run the package, you can set its variables to certain values. This is a great way of configuring your packages without all the hassle with dtsConfig files. In the above code sample, variable “User:ExportRows” is set to value of the parameter “rows” of the method.&lt;/p&gt;  &lt;p&gt;Eventually, you execute the package. The method doesn’t throw exceptions, you have to test the result of execution yourself. If the execution wasn’t successful, you can examine collection of errors exposed by the package. These are the familiar errors you often see during development and debugging of the package. I you run the package from the code, you have opportunity to persist them or log them using your favourite logging framework.&lt;/p&gt;  &lt;p&gt;The package itself is very simple; it connects to my AdventureWorks database and saves number of rows specified in variable “User::ExportRows” to a file. &lt;/p&gt;  &lt;p&gt;You should know that before you run the package, you can change its connection strings, logging, events and many more.&lt;/p&gt;  &lt;p&gt;I attach solution with the test service, as well as a project with two test packages.&lt;/p&gt;  &lt;p&gt;To test the service, you have to run it and wait for the message saying that the host is started.&lt;/p&gt;  &lt;p&gt;&lt;img src="https://public.blu.livefilestore.com/y1p_ASeGz2Iak4y-SoF0boyKTRh6Db-LimMGHydobIcmgWBIQIo9s1Xj9mi3KIET8jYwIT-TsrP7dyQ_LbrLE9R-g/ScreenShot031.jpg?psid=1"&gt;&lt;/p&gt;  &lt;p&gt;Then, just type (or copy and paste) the below command to your browser. &lt;/p&gt;  &lt;p&gt;&lt;a href="http://localhost:8011/runpackwithparams?package=%22ExportEmployees.dtsx%22&amp;amp;rows=12"&gt;http://localhost:8011/runpackwithparams?package=%22ExportEmployees.dtsx%22&amp;amp;rows=12&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;When everything works fine, and you modified the package to point to your AdventureWorks database, you should see "&lt;b&gt;OK&lt;/b&gt;” wrapped in xml:&lt;/p&gt;  &lt;p&gt;&lt;img src="https://public.blu.livefilestore.com/y1puYYFBYpedlyt0B5NlwzHSjSAGhmMBAQlTbVOh3nacTRmp4v5gFd8miV5mUmo5oN_IQ-oAbrP78FDGZPZHlS8gQ/ScreenShot035.jpg?psid=1"&gt;&lt;/p&gt;  &lt;p&gt;I stopped the database service to simulate invalid connection string situation. The output of the request is different now:&lt;/p&gt;  &lt;p&gt;&lt;img src="https://public.blu.livefilestore.com/y1p2iD9EoMeLvh2i647iz_pCjYMhgxzPWo0dDn6FQvgwZJX5LhJUU8gnwv3wgamw3zuESDmD1IJ65k5YpFT5Wemjg/ScreenShot036.jpg?psid=1"&gt;&lt;/p&gt;  &lt;p&gt;And the service console window shows more information:&lt;/p&gt;  &lt;p&gt;&lt;img src="https://public.blu.livefilestore.com/y1p2iD9EoMeLvh7DDGUQXpZAhiZUzy2p1KmAVSuxgeBnEkkZD5YV6Bb1BYKla6vfte1GGITFM4IntvIjJZOORh0xQ/ScreenShot037.jpg?psid=1"&gt;&lt;/p&gt;  &lt;p&gt;As you see, implementing service oriented ETL framework is not a very difficult task. You have ability to configure the packages before you run them, you can implement logging that is consistent with the rest of your system. In application I have worked with we also have resource monitoring and execution control. We don’t allow to run more than certain number of packages to run simultaneously. This ensures we don’t strain the server and we use memory and CPUs efficiently.&lt;/p&gt;  &lt;p&gt;The attached zip file contains two projects. One is the package runner. &lt;b&gt;It has to be executed with administrative privileges as it registers HTTP namespace&lt;/b&gt;. The other project contains two simple packages. &lt;/p&gt;  &lt;p&gt;This is really a cool thing, you should check it out!&lt;/p&gt;&lt;img src="http://sqlblog.com/aggbug.aspx?PostID=42749" width="1" height="1"&gt;</description><enclosure url="http://sqlblog.com/blogs/piotr_rodak/attachment/42749.ashx" length="95117" type="application/x-zip-compressed" /><category domain="http://sqlblog.com/blogs/piotr_rodak/archive/tags/.NET/default.aspx">.NET</category><category domain="http://sqlblog.com/blogs/piotr_rodak/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://sqlblog.com/blogs/piotr_rodak/archive/tags/good+practices/default.aspx">good practices</category><category domain="http://sqlblog.com/blogs/piotr_rodak/archive/tags/SOA/default.aspx">SOA</category><category domain="http://sqlblog.com/blogs/piotr_rodak/archive/tags/SQL+Server+2008+R2/default.aspx">SQL Server 2008 R2</category><category domain="http://sqlblog.com/blogs/piotr_rodak/archive/tags/SSIS/default.aspx">SSIS</category><category domain="http://sqlblog.com/blogs/piotr_rodak/archive/tags/tips+and+tricks/default.aspx">tips and tricks</category></item><item><title>Server level permissions for developers–why you should read books</title><link>http://sqlblog.com/blogs/piotr_rodak/archive/2012/01/02/server-level-permissions-for-developers-why-you-should-read-books.aspx</link><pubDate>Mon, 02 Jan 2012 03:05:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:40715</guid><dc:creator>Piotr Rodak</dc:creator><slash:comments>1</slash:comments><comments>http://sqlblog.com/blogs/piotr_rodak/comments/40715.aspx</comments><wfw:commentRss>http://sqlblog.com/blogs/piotr_rodak/commentrss.aspx?PostID=40715</wfw:commentRss><description>&lt;p&gt;It is quite difficult recently for me to find some time to write a new post, so I don’t seem to be leading the rankings of the most frequent bloggers. I rarely recently have opportunity to lay my hands on the code too, so natural sources of ‘inspiration’ are less often. Hopefully this will change in 2012 and I will have more opportunities to write something useful.&lt;/p&gt;  &lt;p&gt;In a big enterprise the roles of server users are usually more distinguished than in smaller companies. You have DBAs, developers and business users. Each of these roles has generally different goals and different rights when it comes to connecting to the database servers. Large organizations tend to implement service oriented approach to processes, which means that various teams are responsible for executing various tasks ordered by other teams. In the case of developers and DBAs, the latter are responsible for all maintenance and management tasks on servers which are ‘owned’ by developers. Such organisation of work often results in sealing off developers from the maintenance related activities, such as creating databases or snapshots, running traces, viewing server state and troubleshooting blocks and deadlocks. This approach unfortunately, while imposes more order to the server utilization and prevents accidental failures, tends to slow down the development process, sometimes preventing any work from being done by significant number of developers for time anything from few minutes to few hours. This is a waste of time and money of course. In my case, development team wanted to have right to kill user sessions on development servers. You know, sometimes there is a process that takes a lot of time and locks, or there is an open transaction keeping schema locks all over the place and and the owner of it is gone for the day without committing it.&lt;/p&gt;  &lt;p&gt;Problem is, that to be able to kill a session, you have to be a sysadmin, processadmin, or have ALTER ANY CONNECTION right granted to you at server level. Obviously, in certain environments these permissions are not the ones DBAs want to give to &lt;strike&gt;random people&lt;/strike&gt; developers. If you GRANT ALTER ANY CONNECTION to a login, this login can kill some important sessions, not only the runaway ones. It may break audit, scheduled maintenance, backups, other teams ETL processes – just imagine your mailbox – it ain’t gonna look pretty. It would be much better to be able to grant login right to kill only spids belonging to certain users or working on certain databases. How can you eat the cake and have the cake?&lt;/p&gt;  &lt;p&gt;I remembered that I had read about this idea in one of the books, namely &lt;a target="_blank" href="http://www.amazon.com/exec/obidos/ASIN/159059729X/ref=nosim/apre-20"&gt;Expert SQL Server 2005 Development&lt;/a&gt; by &lt;a target="_blank" href="http://sqlblog.com/blogs/adam_machanic/"&gt;Adam Machanic&lt;/a&gt;, &lt;a target="_blank" href="http://sqlblog.com/blogs/lara_rubbelke/"&gt;Lara Rubbelke&lt;/a&gt; and &lt;a target="_blank" href="http://sqlblog.com/blogs/hugo_kornelis/"&gt;Hugo Kornelis&lt;/a&gt;. This is an excellent book, you should really read it if you haven’t yet done so. There is a chapter in the book, discussing the security features introduced in SQL Server 2005. These features allow architects to design more versatile and complex applications without jeopardizing security requirements. As you know, the security model in SQL Server 2005 has been completely revamped, for example the notion of the &lt;b&gt;&lt;a target="_blank" href="http://msdn.microsoft.com/en-us/library/ms190387%28v=SQL.90%29.aspx"&gt;schema&lt;/a&gt;&lt;/b&gt; was separated from the ownership and database user. There are more features, one of most important probably is the ability to sign stored procedures with certificates and to create logins and users from the certificates. The discussion about security architecture and certificates is huge and going beyond scope of this post. One of the starting points you can check out is the MSDN article "&lt;a target="_blank" href="http://msdn.microsoft.com/en-us/library/bb283235.aspx"&gt;Securing SQL Server&lt;/a&gt;”.&lt;/p&gt;  &lt;p&gt;In short, if you were to grant server wide elevated rights to certain groups of users, how would you do it? Here’s a little shopping list:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Create a certificate &lt;/li&gt;    &lt;li&gt;Create a login &lt;b&gt;from the certificate&lt;/b&gt; &lt;/li&gt;    &lt;li&gt;Grant ALTER ANY CONNECTION to the login &lt;/li&gt;    &lt;li&gt;If you want to have the procedure in database other than master, you must copy the certificate to this database. You do it with backup-restore technique.&lt;/li&gt;    &lt;li&gt;Create a user for the login &lt;/li&gt;    &lt;li&gt;Create stored procedure that will issue KILL command &lt;/li&gt;    &lt;li&gt;Sign the procedure with the certificate &lt;/li&gt;    &lt;li&gt;Grant EXECUTE on the procedure to developers. &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;Here are some highlights of the solution. I attach the testing code at the end of the post as usual.&lt;/p&gt;  &lt;p&gt;Sample code for creating certificate and the login is as follows.&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:f93dc5b1-ded6-4fc1-9dd3-f16da8d75066" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#ddd;max-height:400px;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2.5em;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#008000;"&gt;---create certificate that the procedure will be signed with&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;create&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;certificate&lt;/span&gt; kill_session_certificate&lt;/li&gt; &lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#0000ff;"&gt;encryption&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;by&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;password&lt;/span&gt; &lt;span style="color:#808080;"&gt;=&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;'1410SomeReallyStrongPassword2011!'&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#0000ff;"&gt;with&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;subject&lt;/span&gt; &lt;span style="color:#808080;"&gt;=&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;'Enable KILL through procedure'&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;go&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#008000;"&gt;---create login that will be granted right to kill sessions&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;create&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;login&lt;/span&gt; kill_login &lt;span style="color:#0000ff;"&gt;from&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;certificate&lt;/span&gt; kill_session_certificate&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;go&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#008000;"&gt;--licence to kill&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;grant&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;ALTER&lt;/span&gt; &lt;span style="color:#808080;"&gt;ANY&lt;/span&gt; CONNECTION &lt;span style="color:#0000ff;"&gt;to&lt;/span&gt; kill_login&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;This is the stored procedure developers can call to kill sessions.&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:410bd5b7-2e04-4537-b97b-1b3c519ad81b" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#ddd;max-height:400px;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2.5em;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;create&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;procedure&lt;/span&gt; killsession&lt;span style="color:#808080;"&gt;(&lt;/span&gt;@sessionid &lt;span style="color:#0000ff;"&gt;int&lt;/span&gt;&lt;span style="color:#808080;"&gt;)&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;as&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;begin&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#008000;"&gt;/* Piotr Rodak: procedure calls KILL command. Must be signed by certificate to work.*/&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#0000ff;"&gt;print&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;'killsession: executing as '&lt;/span&gt; &lt;span style="color:#808080;"&gt;+&lt;/span&gt; &lt;span style="color:#ff00ff;"&gt;suser_name&lt;/span&gt;&lt;span style="color:#808080;"&gt;()&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#0000ff;"&gt;declare&lt;/span&gt; @command &lt;span style="color:#0000ff;"&gt;varchar&lt;/span&gt;&lt;span style="color:#808080;"&gt;(&lt;/span&gt;300&lt;span style="color:#808080;"&gt;)&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#0000ff;"&gt;set&lt;/span&gt; @command &lt;span style="color:#808080;"&gt;=&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;'kill '&lt;/span&gt; &lt;span style="color:#808080;"&gt;+&lt;/span&gt; &lt;span style="color:#ff00ff;"&gt;convert&lt;/span&gt;&lt;span style="color:#808080;"&gt;(&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;varchar&lt;/span&gt;&lt;span style="color:#808080;"&gt;,&lt;/span&gt; @sessionid&lt;span style="color:#808080;"&gt;)&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#0000ff;"&gt;print&lt;/span&gt; @command&lt;/li&gt; &lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#008000;"&gt;---you can filter only sessions running on certain databases here.. &lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#0000ff;"&gt;exec&lt;/span&gt;&lt;span style="color:#808080;"&gt;(&lt;/span&gt;@command&lt;span style="color:#808080;"&gt;)&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#008000;"&gt;---you can add logging here.. &lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#0000ff;"&gt;print&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;'killed session '&lt;/span&gt; &lt;span style="color:#808080;"&gt;+&lt;/span&gt; &lt;span style="color:#ff00ff;"&gt;convert&lt;/span&gt;&lt;span style="color:#808080;"&gt;(&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;varchar&lt;/span&gt;&lt;span style="color:#808080;"&gt;,&lt;/span&gt; @sessionid&lt;span style="color:#808080;"&gt;)&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;end&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;You can create the procedure in any database, but if you choose a database different than master you have to copy the certificate from master to the target database.&lt;/p&gt;  &lt;p&gt;And here’s code to sign the procedure:&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:56e9ab18-9eb8-4807-8ff2-563406307332" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#ddd;max-height:500px;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2em;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#008000;"&gt;---sign the procedure with the certificate&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;add&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;signature&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;to&lt;/span&gt; killsession &lt;span style="color:#0000ff;"&gt;by&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;certificate&lt;/span&gt; kill_session_certificate&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;with&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;password&lt;/span&gt; &lt;span style="color:#808080;"&gt;=&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;'1410SomeReallyStrongPassword2011!'&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;Since you are executing the command wrapped into a stored procedure, you have potential to implement additional audit, like grab execution plan or some other performance indicators of the server at the moment when the command is issued. This can be extremely helpful for troubleshooting reasons. I think this is pretty cool.&lt;/p&gt;            &lt;p&gt;Here’s the testing &lt;a target="_blank" href="https://docs.google.com/open?id=0B5ayjYKh-6TxNjYxYWM3YzItMmM2ZC00ZTA2LTg3ZWEtNmU4MzYzNTg0NmRh"&gt;script&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;Enjoy.&lt;/p&gt;&lt;img src="http://sqlblog.com/aggbug.aspx?PostID=40715" width="1" height="1"&gt;</description><category domain="http://sqlblog.com/blogs/piotr_rodak/archive/tags/good+practices/default.aspx">good practices</category><category domain="http://sqlblog.com/blogs/piotr_rodak/archive/tags/security/default.aspx">security</category><category domain="http://sqlblog.com/blogs/piotr_rodak/archive/tags/SQL+Server+2008/default.aspx">SQL Server 2008</category><category domain="http://sqlblog.com/blogs/piotr_rodak/archive/tags/SQL+Server+2008+R2/default.aspx">SQL Server 2008 R2</category><category domain="http://sqlblog.com/blogs/piotr_rodak/archive/tags/tips+and+tricks/default.aspx">tips and tricks</category><category domain="http://sqlblog.com/blogs/piotr_rodak/archive/tags/T-SQL/default.aspx">T-SQL</category></item><item><title>OUTPUT clause and windowing functions</title><link>http://sqlblog.com/blogs/piotr_rodak/archive/2011/07/31/output-clause-and-windowing-functions.aspx</link><pubDate>Sun, 31 Jul 2011 20:55:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:37412</guid><dc:creator>Piotr Rodak</dc:creator><slash:comments>8</slash:comments><comments>http://sqlblog.com/blogs/piotr_rodak/comments/37412.aspx</comments><wfw:commentRss>http://sqlblog.com/blogs/piotr_rodak/commentrss.aspx?PostID=37412</wfw:commentRss><description>&lt;p&gt;A few days ago I was asked a question how to remove duplicates from a table. As usually in such cases, the duplicate values were related only to several columns, not all of them. So classical approach, to copy distinct data into temp table, truncate the table with duplicates and then copy the data back wouldn’t work. In such cases I find one of the windowing functions, row_number() to be the easiest to use. You just define criteria of numbering of rows and then you remove all rows are &lt;b&gt;not first&lt;/b&gt; in their partition. Let’s have a look how it works.&lt;/p&gt;  &lt;p&gt;First, let’s create and populate sample table:&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:2f67f1dc-cbef-4da3-874d-71e3fdf24d4b" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#ddd;max-height:500px;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2.5em;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;if&lt;/span&gt; &lt;span style="color:#ff00ff;"&gt;object_id&lt;/span&gt;&lt;span style="color:#808080;"&gt;(&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;'tempdb..#t'&lt;/span&gt;&lt;span style="color:#808080;"&gt;)&lt;/span&gt; &lt;span style="color:#808080;"&gt;is&lt;/span&gt; &lt;span style="color:#808080;"&gt;not&lt;/span&gt; &lt;span style="color:#808080;"&gt;null&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#0000ff;"&gt;drop&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;table&lt;/span&gt; #t&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;create&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;table&lt;/span&gt; #t&lt;span style="color:#808080;"&gt;(&lt;/span&gt;Id &lt;span style="color:#0000ff;"&gt;char&lt;/span&gt;&lt;span style="color:#808080;"&gt;(&lt;/span&gt;3&lt;span style="color:#808080;"&gt;),&lt;/span&gt; randomData &lt;span style="color:#0000ff;"&gt;uniqueidentifier&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;default&lt;/span&gt;&lt;span style="color:#808080;"&gt;(&lt;/span&gt;&lt;span style="color:#ff00ff;"&gt;newid&lt;/span&gt;&lt;span style="color:#808080;"&gt;()))&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;insert&lt;/span&gt; #t&lt;span style="color:#808080;"&gt;(&lt;/span&gt;Id&lt;span style="color:#808080;"&gt;)&lt;/span&gt; &lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;values&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#808080;"&gt;(&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;'123'&lt;/span&gt;&lt;span style="color:#808080;"&gt;),&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;(&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;'124'&lt;/span&gt;&lt;span style="color:#808080;"&gt;),&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#808080;"&gt;(&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;'133'&lt;/span&gt;&lt;span style="color:#808080;"&gt;),&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;(&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;'133'&lt;/span&gt;&lt;span style="color:#808080;"&gt;),&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;(&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;'133'&lt;/span&gt;&lt;span style="color:#808080;"&gt;),&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#808080;"&gt;(&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;'141'&lt;/span&gt;&lt;span style="color:#808080;"&gt;),&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;(&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;'141'&lt;/span&gt;&lt;span style="color:#808080;"&gt;),&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#808080;"&gt;(&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;'121'&lt;/span&gt;&lt;span style="color:#808080;"&gt;),&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;(&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;'121'&lt;/span&gt;&lt;span style="color:#808080;"&gt;),&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#808080;"&gt;(&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;'145'&lt;/span&gt;&lt;span style="color:#808080;"&gt;),&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;(&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;'145'&lt;/span&gt;&lt;span style="color:#808080;"&gt;),&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#808080;"&gt;(&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;'152'&lt;/span&gt;&lt;span style="color:#808080;"&gt;)&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;select&lt;/span&gt; &lt;span style="color:#808080;"&gt;*&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;from&lt;/span&gt; #t&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;When you run the query, you should see result similar to the following illustration:&lt;/p&gt;  &lt;p&gt;&lt;img src="https://blufiles.storage.live.com/y1pkh0KvDICqyCFedz7gczEQRDf7ojUtHJQW-A5OdMS-LAXaVgX97gmcR_Khw0eg6eZidjedokWxKE/ScreenShot011.jpg?psid=1" height="373" width="508"&gt;&lt;/p&gt;  &lt;p&gt;Deleting duplicates is pretty straightforward. You define partitioned row numbering and then you delete all rows that have row number bigger than 1:&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:af71ae57-84a9-45f9-8bae-5b9be429541e" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#ddd;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2em;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#808080;"&gt;;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;with&lt;/span&gt; dupes &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; &lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#808080;"&gt;(&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#0000ff;"&gt;select&lt;/span&gt; &lt;span style="color:#808080;"&gt;*,&lt;/span&gt; &lt;span style="color:#ff00ff;"&gt;row_number&lt;/span&gt;&lt;span style="color:#808080;"&gt;()&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;over&lt;/span&gt;&lt;span style="color:#808080;"&gt;(&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;partition&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;by&lt;/span&gt; Id &lt;span style="color:#0000ff;"&gt;order&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;by&lt;/span&gt; Id&lt;span style="color:#808080;"&gt;)&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; rn&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#0000ff;"&gt;from&lt;/span&gt; #t&lt;/li&gt; &lt;li&gt;&lt;span style="color:#808080;"&gt;)&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;delete&lt;/span&gt; dupes&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;where&lt;/span&gt; rn &lt;span style="color:#808080;"&gt;&amp;gt;&lt;/span&gt; 1&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;select&lt;/span&gt; &lt;span style="color:#808080;"&gt;*&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;from&lt;/span&gt; #t&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;    &lt;p&gt;As you see, the duplicates were removed.&lt;/p&gt;  &lt;p&gt;&lt;img src="https://blufiles.storage.live.com/y1pkh0KvDICqyBYYSqzCp1DfiGTKrV9_zK1MfMEm4uMpRuGJaPYfmX73bEgz7YMTzPmiK36r-_wxe4/ScreenShot012.jpg?psid=1" height="247" width="471"&gt;&lt;/p&gt;  &lt;p&gt;The execution plan for the above statement is pretty straightforward:&lt;/p&gt;  &lt;p&gt;&lt;img src="https://blufiles.storage.live.com/y1pL9tMjPVX5yM_uC4gMszdezTLey0oRr6WvjUdva6BhEf7AMKFtIP3jfYHMJ_aKQRXLHsH18f_2_4/Deleteplan1.jpg?psid=1" height="113" width="1017"&gt;&lt;/p&gt;  &lt;p&gt;The most interesting operator in this plan is the Sequence Project operator. It adds the calculated row number to the output, based on the criteria that you pass to the row_number() OVER clause. Here we partitioned the input by column Id which causes the row number to be restarted from 1 whenever new Id is encountered in the input. The row number is returned as result of internal function &lt;b&gt;row_number&lt;/b&gt;, which you can see in the following picture:&lt;/p&gt;  &lt;p&gt;&lt;img src="https://blufiles.storage.live.com/y1p56Zr99vHyQDjwBoBGaFiKx_q46OKAuhT6QsW3O3w4UocnK0he-CDy_k27q6_oGDqnLDKBYL_vhM/ScreenShot014.jpg?psid=1" height="182" width="565"&gt;&lt;/p&gt;  &lt;p&gt;On the left to the Sequence Project operator you can see Filter operator. This operator filters the input based on the certain predicate. You can see in&amp;nbsp; the picture below, that Filter operator allows to pass though only rows that have Expr1004 &amp;gt; 1.&lt;/p&gt;  &lt;p&gt;&lt;img src="https://blufiles.storage.live.com/y1pA682Ra77WVcobDGDPIbgTQ1rZD5L4HmVf7AIE7R4-aGmQI7vZVijfX6-IkkFqxaCsl6wRLCumus/ScreenShot013.jpg?psid=1" height="451" width="435"&gt;&lt;/p&gt;  &lt;p&gt;You can check that in this particular case only five rows were let through and subsequently deleted. Two with Id 133, one 141, one 121 and one 145.&lt;/p&gt;  &lt;p&gt;&lt;img src="https://blufiles.storage.live.com/y1pcFdwy4t1ecIWfUoq_fwzY3PfoRY1pJ7do4hYQJCTr7rrLh1c3E6ck0ULFnDa9x1ZuoKCdQ4_4OA/ScreenShot015.jpg?psid=1" height="169" width="294"&gt;&lt;/p&gt;  &lt;p&gt;What if you want to look at the rows that are deleted? Let’s repopulate the table and run the delete statement, this time with the &lt;a href="http://msdn.microsoft.com/en-us/library/ms177564%28v=SQL.105%29.aspx"&gt;OUTPUT&lt;/a&gt; clause:&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:6a78ce14-907a-4e35-9882-cf02c50511e0" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#ddd;max-height:500px;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2.5em;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;truncate&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;table&lt;/span&gt; #t&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;insert&lt;/span&gt; #t&lt;span style="color:#808080;"&gt;(&lt;/span&gt;Id&lt;span style="color:#808080;"&gt;)&lt;/span&gt; &lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;values&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#808080;"&gt;(&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;'123'&lt;/span&gt;&lt;span style="color:#808080;"&gt;),&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;(&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;'124'&lt;/span&gt;&lt;span style="color:#808080;"&gt;),&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#808080;"&gt;(&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;'133'&lt;/span&gt;&lt;span style="color:#808080;"&gt;),&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;(&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;'133'&lt;/span&gt;&lt;span style="color:#808080;"&gt;),&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;(&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;'133'&lt;/span&gt;&lt;span style="color:#808080;"&gt;),&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#808080;"&gt;(&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;'141'&lt;/span&gt;&lt;span style="color:#808080;"&gt;),&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;(&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;'141'&lt;/span&gt;&lt;span style="color:#808080;"&gt;),&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#808080;"&gt;(&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;'121'&lt;/span&gt;&lt;span style="color:#808080;"&gt;),&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;(&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;'121'&lt;/span&gt;&lt;span style="color:#808080;"&gt;),&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#808080;"&gt;(&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;'145'&lt;/span&gt;&lt;span style="color:#808080;"&gt;),&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;(&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;'145'&lt;/span&gt;&lt;span style="color:#808080;"&gt;),&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#808080;"&gt;(&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;'152'&lt;/span&gt;&lt;span style="color:#808080;"&gt;)&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#808080;"&gt;;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;with&lt;/span&gt; dupes &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; &lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#808080;"&gt;(&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#0000ff;"&gt;select&lt;/span&gt; &lt;span style="color:#808080;"&gt;*,&lt;/span&gt; &lt;span style="color:#ff00ff;"&gt;row_number&lt;/span&gt;&lt;span style="color:#808080;"&gt;()&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;over&lt;/span&gt;&lt;span style="color:#808080;"&gt;(&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;partition&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;by&lt;/span&gt; Id &lt;span style="color:#0000ff;"&gt;order&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;by&lt;/span&gt; Id&lt;span style="color:#808080;"&gt;)&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; rn&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#0000ff;"&gt;from&lt;/span&gt; #t&lt;/li&gt; &lt;li&gt;&lt;span style="color:#808080;"&gt;)&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;delete&lt;/span&gt; dupes&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;output&lt;/span&gt; deleted&lt;span style="color:#808080;"&gt;.*&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;where&lt;/span&gt; rn &lt;span style="color:#808080;"&gt;&amp;gt;&lt;/span&gt; 1&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;The OUTPUT clause allows you to return rows from pseudo-tables &lt;i&gt;inserted&lt;/i&gt; and &lt;i&gt;deleted&lt;/i&gt;, the same that you have access to in triggers. You can return these rows to the client or to a temporary table or table variable for further processing, or audit. Let’s have a look at the execution plan.&lt;/p&gt;  &lt;p&gt;&lt;img src="https://blufiles.storage.live.com/y1p9E5oiHJWJ6TpUpLRiumhQ4NmVdFhODUkItdrnZoe4F_p5MYplIRklWY9Na6QuJHP9yMjZZf6KL4/Deleteplan2.jpg?psid=1" height="79" width="1004"&gt;&lt;/p&gt;  &lt;p&gt;What’s this?! The Sort, Segment and Sequence Project operators where added &lt;b&gt;second time&lt;/b&gt; to the plan! When you look at their details you will see that they have exactly the same predicates and conditions as the set on the right-hand side of the Filter operator. &lt;b&gt;The difference is that they are applied to the data stream returned by the output clause&lt;/b&gt;. You can verify this looking at the results returned by the statement:&lt;/p&gt;  &lt;p&gt;&lt;img src="https://blufiles.storage.live.com/y1p70stiSP7weX2HUXcQ9YJ2WTG6ITFRTbF0MXCyybp61ZRpJeR-OHFhf4KqS58rhs9AhEn7V7-HVc/ScreenShot016.jpg?psid=1" height="176" width="507"&gt;&lt;/p&gt;  &lt;p&gt;Have you noticed something? the &lt;b&gt;rn&lt;/b&gt; column contains values 1 and 2, while it should contain only values 2 and 3, because this is what the Filter operator allowed to flow through. This is the evidence that the new operators work on the output data stream. You can see that they work on 5 rows, the ones that were deleted:&lt;/p&gt;  &lt;p&gt;&lt;img src="https://blufiles.storage.live.com/y1ptxdbW28wSRjYGuIdlJIFGbcxdKHalb9wAPhbdEG8VZT3ggJ-HGPsLCFTXNeSMtIK-MPrlZeW5Fw/ScreenShot017.jpg?psid=1" height="189" width="649"&gt;&lt;/p&gt;  &lt;p&gt;It is obvious that if you use output clause on queries that otherwise perform very well, you may run into problems. When you have many rows, the Sort operator can become a bottleneck when it is executed twice. The example also shows that the row number identifier of the row is not reliable within boundaries of single query. This may become an issue for you, if you want to identify rows based on the output from an update or delete statement.&lt;/p&gt;  &lt;p&gt;From purely performance point of view, what can you do to avoid these additional operators? The answer is simple – don’t return the row_number() column in the OUTPUT clause.&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:353b40d0-e044-4ff1-9093-83b6c60dcb6e" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#ddd;max-height:500px;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2.5em;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;truncate&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;table&lt;/span&gt; #t&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;insert&lt;/span&gt; #t&lt;span style="color:#808080;"&gt;(&lt;/span&gt;Id&lt;span style="color:#808080;"&gt;)&lt;/span&gt; &lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;values&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#808080;"&gt;(&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;'123'&lt;/span&gt;&lt;span style="color:#808080;"&gt;),&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;(&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;'124'&lt;/span&gt;&lt;span style="color:#808080;"&gt;),&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#808080;"&gt;(&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;'133'&lt;/span&gt;&lt;span style="color:#808080;"&gt;),&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;(&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;'133'&lt;/span&gt;&lt;span style="color:#808080;"&gt;),&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;(&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;'133'&lt;/span&gt;&lt;span style="color:#808080;"&gt;),&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#808080;"&gt;(&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;'141'&lt;/span&gt;&lt;span style="color:#808080;"&gt;),&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;(&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;'141'&lt;/span&gt;&lt;span style="color:#808080;"&gt;),&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#808080;"&gt;(&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;'121'&lt;/span&gt;&lt;span style="color:#808080;"&gt;),&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;(&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;'121'&lt;/span&gt;&lt;span style="color:#808080;"&gt;),&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#808080;"&gt;(&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;'145'&lt;/span&gt;&lt;span style="color:#808080;"&gt;),&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;(&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;'145'&lt;/span&gt;&lt;span style="color:#808080;"&gt;),&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#808080;"&gt;(&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;'152'&lt;/span&gt;&lt;span style="color:#808080;"&gt;)&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#808080;"&gt;;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;with&lt;/span&gt; dupes &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; &lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#808080;"&gt;(&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#0000ff;"&gt;select&lt;/span&gt; &lt;span style="color:#808080;"&gt;*,&lt;/span&gt; &lt;span style="color:#ff00ff;"&gt;row_number&lt;/span&gt;&lt;span style="color:#808080;"&gt;()&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;over&lt;/span&gt;&lt;span style="color:#808080;"&gt;(&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;partition&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;by&lt;/span&gt; Id &lt;span style="color:#0000ff;"&gt;order&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;by&lt;/span&gt; Id&lt;span style="color:#808080;"&gt;)&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; rn&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#0000ff;"&gt;from&lt;/span&gt; #t&lt;/li&gt; &lt;li&gt;&lt;span style="color:#808080;"&gt;)&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;delete&lt;/span&gt; dupes&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;output&lt;/span&gt; deleted&lt;span style="color:#808080;"&gt;.&lt;/span&gt;Id&lt;span style="color:#808080;"&gt;,&lt;/span&gt; deleted&lt;span style="color:#808080;"&gt;.&lt;/span&gt;randomData&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;where&lt;/span&gt; rn &lt;span style="color:#808080;"&gt;&amp;gt;&lt;/span&gt; 1&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;As you see in the snippet above, I explicitly return columns from the &lt;i&gt;deleted&lt;/i&gt; table and I don’t return the &lt;b&gt;rn&lt;/b&gt; column. The optimizer realized that the column is not needed and removed the additional operators from the plan, so looks exactly the same as the first plan we saw in this post.&lt;/p&gt;  &lt;p&gt;&lt;img src="https://blufiles.storage.live.com/y1pRls7C8Yu96mPO67Hew3oiDzHp4LcmAXUysQExS4Lrza_83G_ieyQ630GkZ5YMFtCjzXwkScO3hs/Deleteplan3.jpg?psid=1" height="102" width="918"&gt;&lt;/p&gt;  &lt;p&gt;Well, if someone asked me what I think about this behavior, I would say it is a bug in the SQL Server engine. The windowing functions shouldn’t be applied to the results of the OUTPUT clause because they affect performance and potentially affect the logic of the application. &lt;/p&gt;  &lt;p&gt;This is the link to the &lt;a href="https://q5doxa.blu.livefilestore.com/y1pZuZi8Mvj5-xHwUPO1rGu6OPy7jbflS-HIHuNxez27jg4A8kcu6ctkXi3PUoBeQZmDsXeCVSB3IEIb5m4Js3jtkgIf_MKX05A/OUTPUT-testing_duplicates.sql?download&amp;amp;psid=1"&gt;test script&lt;/a&gt;, so you can run your own investigations.&lt;/p&gt;&lt;img src="http://sqlblog.com/aggbug.aspx?PostID=37412" width="1" height="1"&gt;</description><enclosure url="http://sqlblog.com/blogs/piotr_rodak/attachment/37412.ashx" length="541" type="application/zip" /><category domain="http://sqlblog.com/blogs/piotr_rodak/archive/tags/performance/default.aspx">performance</category><category domain="http://sqlblog.com/blogs/piotr_rodak/archive/tags/SQL+Server+2008+R2/default.aspx">SQL Server 2008 R2</category><category domain="http://sqlblog.com/blogs/piotr_rodak/archive/tags/tips+and+tricks/default.aspx">tips and tricks</category><category domain="http://sqlblog.com/blogs/piotr_rodak/archive/tags/T-SQL/default.aspx">T-SQL</category></item><item><title>Security – how to assign rights to database objects</title><link>http://sqlblog.com/blogs/piotr_rodak/archive/2011/04/10/security-how-to-assign-rights-to-database-objects.aspx</link><pubDate>Sun, 10 Apr 2011 09:23:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:34777</guid><dc:creator>Piotr Rodak</dc:creator><slash:comments>1</slash:comments><comments>http://sqlblog.com/blogs/piotr_rodak/comments/34777.aspx</comments><wfw:commentRss>http://sqlblog.com/blogs/piotr_rodak/commentrss.aspx?PostID=34777</wfw:commentRss><description>&lt;p&gt;A few days ago my team mate asked me to check out why one of the logins doesn’t have right to execute to a few stored procedures. Allegedly it had the right before. After a few minutes of digging in the source control it turned out that these procedures were scripted with explicit EXECUTE rights to them. However, some later updates to the code did not contain the rights scripted. Developers rarely pay too much attention to the security design of the code they write. It is not common that the code is implemented with least possible access level in mind. A classic example of this is TRUNCATE TABLE. While this is very handy command and can be very useful in certain scenarios, developers don’t realize that the user account executing it has to have some elevated rights. &lt;/p&gt;  &lt;p&gt;I have seen many databases which implement explicit rights to objects. This is to some degree a legacy issue, as in SQL Server 2000 and before security model was completely different and more difficult to manage. The common practice at the time was to script rights to objects, and to make things simple everyone who had right to connect to database could do everything in it. Even recently I saw a table with explicit rights like this:&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:24860838-d6c2-428e-87c3-6d211242970e" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#ddd;max-height:400px;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2.5em;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;create&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;table&lt;/span&gt; dbo&lt;span style="color:#808080;"&gt;.&lt;/span&gt;sometable&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#808080;"&gt;(&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    column1 &lt;span style="color:#0000ff;"&gt;int&lt;/span&gt;&lt;span style="color:#808080;"&gt;,&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    columnt2 &lt;span style="color:#0000ff;"&gt;char&lt;/span&gt;&lt;span style="color:#808080;"&gt;(&lt;/span&gt;1&lt;span style="color:#808080;"&gt;),&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    column3 &lt;span style="color:#0000ff;"&gt;varchar&lt;/span&gt;&lt;span style="color:#808080;"&gt;(&lt;/span&gt;255&lt;span style="color:#808080;"&gt;)&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#808080;"&gt;)&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;go&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;grant&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;select&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;on&lt;/span&gt; dbo&lt;span style="color:#808080;"&gt;.&lt;/span&gt;sometable &lt;span style="color:#0000ff;"&gt;to&lt;/span&gt; [public]&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;grant&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;delete&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;on&lt;/span&gt; dbo&lt;span style="color:#808080;"&gt;.&lt;/span&gt;sometable &lt;span style="color:#0000ff;"&gt;to&lt;/span&gt; [public]&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;grant&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;insert&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;on&lt;/span&gt; dbo&lt;span style="color:#808080;"&gt;.&lt;/span&gt;sometable &lt;span style="color:#0000ff;"&gt;to&lt;/span&gt; [public]&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;grant&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;update&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;on&lt;/span&gt; dbo&lt;span style="color:#808080;"&gt;.&lt;/span&gt;sometable &lt;span style="color:#0000ff;"&gt;to&lt;/span&gt; [public]&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;go&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;&lt;font size="1"&gt;&lt;strong&gt;This is definitely a bad practice.&lt;/strong&gt;&lt;/font&gt;&amp;#160; &lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Let’s examine slightly different scenario, the one that I mentioned at the beginning of this post. Let’s analyse what options you have when you need to give user rights to execute a stored procedure but do don’t want to give rights to public role:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Assign explicit rights for the user to the procedure. &lt;/li&gt;    &lt;li&gt;Assign explicit rights to a role and assign user to that role &lt;/li&gt;    &lt;li&gt;Create the procedure within a separate schema, assign user to specific role and assign rights for the schema to the role. &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;The above three options have their pros and cons. The first option is in most cases an aftermath of legacy design and while it is most granular, it is also hardest to manage and monitor.&lt;/p&gt;  &lt;p&gt;Second option is also very granular, the only difference is after all that the rights are assigned to a database role. Main advantage of this solution is that you can assign more than one user to the database role and they will be automatically granted required access to the objects.&lt;/p&gt;  &lt;p&gt;The third option requires some consideration regarding design of the database. You may need to change some code calling the procedures. You may still want to create a database role and assign rights for the schema to the role. This gives you best flexibility.&lt;/p&gt;  &lt;p&gt;Let’s examine some of the effects each of these options brings in when implemented. I created script file that examines six scenarios you may come across in your database. This is definitely not a complete list, bear in mind.&lt;/p&gt;  &lt;p&gt;First, let’s create test database and login that we will use to test the solutions. I will also create a sample table and stored procedure in the database.&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:f94218d3-9808-4161-be7b-f62611ef092a" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#ddd;max-height:400px;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2.5em;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;use&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;master&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;go&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;create&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;login&lt;/span&gt; RemoteLogin &lt;span style="color:#0000ff;"&gt;with&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;password&lt;/span&gt;&lt;span style="color:#808080;"&gt;=&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;&amp;#39;Str()ngPwd&amp;#39;&lt;/span&gt;&lt;span style="color:#808080;"&gt;,&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;check_policy&lt;/span&gt;&lt;span style="color:#808080;"&gt;=&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;off&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;go&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;create&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;database&lt;/span&gt; testExecRights&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;go&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;use&lt;/span&gt; testExecRights&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;go&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;create&lt;/span&gt; &lt;span style="color:#ff00ff;"&gt;user&lt;/span&gt; [RemoteUser] &lt;span style="color:#0000ff;"&gt;from&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;login&lt;/span&gt; [RemoteLogin]&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;go&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;create&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;table&lt;/span&gt; dbo&lt;span style="color:#808080;"&gt;.&lt;/span&gt;tTest&lt;span style="color:#808080;"&gt;(&lt;/span&gt;a &lt;span style="color:#0000ff;"&gt;int&lt;/span&gt;&lt;span style="color:#808080;"&gt;,&lt;/span&gt; b &lt;span style="color:#0000ff;"&gt;int&lt;/span&gt;&lt;span style="color:#808080;"&gt;)&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;go&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;create&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;procedure&lt;/span&gt; dbo&lt;span style="color:#808080;"&gt;.&lt;/span&gt;pTestInsert&lt;span style="color:#808080;"&gt;(&lt;/span&gt;@a &lt;span style="color:#0000ff;"&gt;int&lt;/span&gt;&lt;span style="color:#808080;"&gt;,&lt;/span&gt; @b &lt;span style="color:#0000ff;"&gt;int&lt;/span&gt;&lt;span style="color:#808080;"&gt;)&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;as&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;begin&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="color:#0000ff;"&gt;insert&lt;/span&gt; dbo&lt;span style="color:#808080;"&gt;.&lt;/span&gt;tTest&lt;span style="color:#808080;"&gt;(&lt;/span&gt;a&lt;span style="color:#808080;"&gt;,&lt;/span&gt; b&lt;span style="color:#808080;"&gt;)&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;values&lt;/span&gt;&lt;span style="color:#808080;"&gt;(&lt;/span&gt;@a&lt;span style="color:#808080;"&gt;,&lt;/span&gt; @b&lt;span style="color:#808080;"&gt;)&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;end&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;go&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;The first scenario shows what happens when you create an user in the database, but you don’t assign any specific rights for it.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:c51af934-ccfe-4e92-8163-e69c009f58cd" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#ddd;max-height:400px;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2.5em;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#008000;"&gt;---Scenario 1: user has public right to the database&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;execute&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;login&lt;/span&gt;&lt;span style="color:#808080;"&gt;=&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;&amp;#39;RemoteLogin&amp;#39;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;select&lt;/span&gt; &lt;span style="color:#ff00ff;"&gt;suser_name&lt;/span&gt;&lt;span style="color:#808080;"&gt;()&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;go&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="color:#0000ff;"&gt;begin&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;try&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#008000;"&gt;---should fail&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        &lt;span style="color:#0000ff;"&gt;exec&lt;/span&gt; dbo&lt;span style="color:#808080;"&gt;.&lt;/span&gt;pTestInsert&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;1&lt;span style="color:#808080;"&gt;,&lt;/span&gt; 1&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt;&lt;span style="color:#808080;"&gt;;&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="color:#0000ff;"&gt;end&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;try&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff;"&gt;begin&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;catch&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        &lt;span style="color:#0000ff;"&gt;declare&lt;/span&gt; @errormsg &lt;span style="color:#0000ff;"&gt;nvarchar&lt;/span&gt;&lt;span style="color:#808080;"&gt;(&lt;/span&gt;2000&lt;span style="color:#808080;"&gt;)&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff;"&gt;set&lt;/span&gt; @errormsg &lt;span style="color:#808080;"&gt;=&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;N&amp;#39;Scenario 1: Exception occured in &amp;#39;&lt;/span&gt; &lt;span style="color:#808080;"&gt;+&lt;/span&gt; &lt;span style="color:#ff00ff;"&gt;error_procedure&lt;/span&gt;&lt;span style="color:#808080;"&gt;()&lt;/span&gt; &lt;span style="color:#808080;"&gt;+&lt;/span&gt; &lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;            &lt;span style="color:#ff0000;"&gt;N&amp;#39;: &amp;#39;&lt;/span&gt; &lt;span style="color:#808080;"&gt;+&lt;/span&gt; &lt;span style="color:#ff00ff;"&gt;error_message&lt;/span&gt;&lt;span style="color:#808080;"&gt;()&lt;/span&gt; &lt;span style="color:#808080;"&gt;+&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;N&amp;#39; (&amp;#39;&lt;/span&gt; &lt;span style="color:#808080;"&gt;+&lt;/span&gt; &lt;span style="color:#ff00ff;"&gt;convert&lt;/span&gt;&lt;span style="color:#808080;"&gt;(&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;nvarchar&lt;/span&gt;&lt;span style="color:#808080;"&gt;,&lt;/span&gt; &lt;span style="color:#ff00ff;"&gt;error_number&lt;/span&gt;&lt;span style="color:#808080;"&gt;())&lt;/span&gt; &lt;span style="color:#808080;"&gt;+&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;N&amp;#39;)&amp;#39;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff;"&gt;raiserror&lt;/span&gt;&lt;span style="color:#808080;"&gt;(&lt;/span&gt;@errormsg&lt;span style="color:#808080;"&gt;,&lt;/span&gt; 16&lt;span style="color:#808080;"&gt;,&lt;/span&gt; 1&lt;span style="color:#808080;"&gt;)&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="color:#0000ff;"&gt;end&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;catch&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;This code throws an exception, as expected:&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:f4187380-52b0-4f01-9ed4-c7af4382eed7" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background-color:#ffffff;max-height:400px;overflow:auto;padding:2px 5px;"&gt;Msg 50000, Level 16, State 1, Line 10&lt;br&gt; Scenario 1: Exception occured in pTestInsert: The EXECUTE permission was denied on the object &amp;#39;pTestInsert&amp;#39;, database &amp;#39;testExecRights&amp;#39;, schema &amp;#39;dbo&amp;#39;. (229)&lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;Let’s have a look what we’ll get when we assign the right to execute the stored procedure explicitly:&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:c98a1426-26b6-49f7-9c76-0d3975f059cf" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#ddd;max-height:400px;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2.5em;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#008000;"&gt;---Scenario 2: user has explicit right to execute the procedure&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#008000;"&gt;---this is executed at sa/dbo level&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;grant&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;execute&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;on&lt;/span&gt; dbo&lt;span style="color:#808080;"&gt;.&lt;/span&gt;pTestInsert &lt;span style="color:#0000ff;"&gt;to&lt;/span&gt; [RemoteUser]&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;go&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;execute&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;login&lt;/span&gt;&lt;span style="color:#808080;"&gt;=&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;&amp;#39;RemoteLogin&amp;#39;&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;select&lt;/span&gt; &lt;span style="color:#ff00ff;"&gt;suser_name&lt;/span&gt;&lt;span style="color:#808080;"&gt;()&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;go&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="color:#0000ff;"&gt;begin&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;try&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#008000;"&gt;---should succeed&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        &lt;span style="color:#0000ff;"&gt;exec&lt;/span&gt; dbo&lt;span style="color:#808080;"&gt;.&lt;/span&gt;pTestInsert&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;1&lt;span style="color:#808080;"&gt;,&lt;/span&gt; 1&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt;&lt;span style="color:#808080;"&gt;;&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="color:#0000ff;"&gt;end&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;try&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff;"&gt;begin&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;catch&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        &lt;span style="color:#0000ff;"&gt;declare&lt;/span&gt; @errormsg &lt;span style="color:#0000ff;"&gt;nvarchar&lt;/span&gt;&lt;span style="color:#808080;"&gt;(&lt;/span&gt;2000&lt;span style="color:#808080;"&gt;)&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff;"&gt;set&lt;/span&gt; @errormsg &lt;span style="color:#808080;"&gt;=&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;N&amp;#39;Scenario 2: Exception occured in &amp;#39;&lt;/span&gt; &lt;span style="color:#808080;"&gt;+&lt;/span&gt; &lt;span style="color:#ff00ff;"&gt;error_procedure&lt;/span&gt;&lt;span style="color:#808080;"&gt;()&lt;/span&gt; &lt;span style="color:#808080;"&gt;+&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;N&amp;#39;: &amp;#39;&lt;/span&gt; &lt;span style="color:#808080;"&gt;+&lt;/span&gt; &lt;span style="color:#ff00ff;"&gt;error_message&lt;/span&gt;&lt;span style="color:#808080;"&gt;()&lt;/span&gt; &lt;span style="color:#808080;"&gt;+&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;N&amp;#39; (&amp;#39;&lt;/span&gt; &lt;span style="color:#808080;"&gt;+&lt;/span&gt; &lt;span style="color:#ff00ff;"&gt;convert&lt;/span&gt;&lt;span style="color:#808080;"&gt;(&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;nvarchar&lt;/span&gt;&lt;span style="color:#808080;"&gt;,&lt;/span&gt; &lt;span style="color:#ff00ff;"&gt;error_number&lt;/span&gt;&lt;span style="color:#808080;"&gt;())&lt;/span&gt; &lt;span style="color:#808080;"&gt;+&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;N&amp;#39;)&amp;#39;&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        &lt;span style="color:#0000ff;"&gt;raiserror&lt;/span&gt;&lt;span style="color:#808080;"&gt;(&lt;/span&gt;@errormsg&lt;span style="color:#808080;"&gt;,&lt;/span&gt; 16&lt;span style="color:#808080;"&gt;,&lt;/span&gt; 1&lt;span style="color:#808080;"&gt;)&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff;"&gt;end&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;catch&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;go&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;revert&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;select&lt;/span&gt; &lt;span style="color:#ff00ff;"&gt;suser_name&lt;/span&gt;&lt;span style="color:#808080;"&gt;()&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#008000;"&gt;--revoke granted right&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;revoke&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;execute&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;on&lt;/span&gt; dbo&lt;span style="color:#808080;"&gt;.&lt;/span&gt;pTestInsert &lt;span style="color:#0000ff;"&gt;to&lt;/span&gt; [RemoteUser]&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;go&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;In line 3 in the above snippet you see the command to grant execute right on the stored procedure to the RemoteUser. Please note that the user still has no other rights – he can’t insert rows to the tTest table directly, he even can’t select from the table. Since the above snippet is part of the bigger script, at the end of it, in line 22 you can see command 'REVOKE’ to – revoke the right granted in the line 3. So, how the above code works? Yes, it works OK. It doesn’t throw exception because the permissions are right.&lt;/p&gt;  &lt;p&gt;As I mentioned before, there were procedures which the remote login mysteriously lost ability to call. In my case, the issue was caused by a release script, which did not re-establish appropriate rights to them. There are two ways you can modify a procedure during release. One is obviously ALTER PROCEDURE, which does not change the security configuration of the procedure, the other is a sequence of DROP and CREATE statements. Scenarios 3 and 4 are shown in the below scripts.&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:761217c3-1ddb-47dd-b8c8-dcaa989e15d5" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#ddd;max-height:400px;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2.5em;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#008000;"&gt;---Scenario 3: user has explicit right to execute the procedure but the stored procedure is ALTERED&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#008000;"&gt;--- in subsequent release&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;go&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#008000;"&gt;---this is executed at sa/dbo level&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;grant&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;execute&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;on&lt;/span&gt; dbo&lt;span style="color:#808080;"&gt;.&lt;/span&gt;pTestInsert &lt;span style="color:#0000ff;"&gt;to&lt;/span&gt; [RemoteUser]&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;go&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;go&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#008000;"&gt;---now ALTER the procedure&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;alter&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;procedure&lt;/span&gt; dbo&lt;span style="color:#808080;"&gt;.&lt;/span&gt;pTestInsert&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;(&lt;/span&gt;@a &lt;span style="color:#0000ff;"&gt;int&lt;/span&gt;&lt;span style="color:#808080;"&gt;,&lt;/span&gt; @b &lt;span style="color:#0000ff;"&gt;int&lt;/span&gt;&lt;span style="color:#808080;"&gt;)&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; &lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;begin&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="color:#0000ff;"&gt;insert&lt;/span&gt; dbo&lt;span style="color:#808080;"&gt;.&lt;/span&gt;tTest&lt;span style="color:#808080;"&gt;(&lt;/span&gt;a&lt;span style="color:#808080;"&gt;,&lt;/span&gt; b&lt;span style="color:#808080;"&gt;)&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;values &lt;/span&gt;&lt;span style="color:#808080;"&gt;(&lt;/span&gt;@a&lt;span style="color:#808080;"&gt;,&lt;/span&gt; @a &lt;span style="color:#808080;"&gt;*&lt;/span&gt; @b&lt;span style="color:#808080;"&gt;)&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;end&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;go&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;execute&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;login&lt;/span&gt;&lt;span style="color:#808080;"&gt;=&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;&amp;#39;RemoteLogin&amp;#39;&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;select&lt;/span&gt; &lt;span style="color:#ff00ff;"&gt;suser_name&lt;/span&gt;&lt;span style="color:#808080;"&gt;()&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;go&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="color:#0000ff;"&gt;begin&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;try&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#008000;"&gt;---should succeed&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        &lt;span style="color:#0000ff;"&gt;exec&lt;/span&gt; dbo&lt;span style="color:#808080;"&gt;.&lt;/span&gt;pTestInsert&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;1&lt;span style="color:#808080;"&gt;,&lt;/span&gt; 1&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt;&lt;span style="color:#808080;"&gt;;&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="color:#0000ff;"&gt;end&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;try&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff;"&gt;begin&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;catch&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        &lt;span style="color:#0000ff;"&gt;declare&lt;/span&gt; @errormsg &lt;span style="color:#0000ff;"&gt;nvarchar&lt;/span&gt;&lt;span style="color:#808080;"&gt;(&lt;/span&gt;2000&lt;span style="color:#808080;"&gt;)&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff;"&gt;set&lt;/span&gt; @errormsg &lt;span style="color:#808080;"&gt;=&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;N&amp;#39;Scenario 3: Exception occured in &amp;#39;&lt;/span&gt; &lt;span style="color:#808080;"&gt;+&lt;/span&gt; &lt;span style="color:#ff00ff;"&gt;error_procedure&lt;/span&gt;&lt;span style="color:#808080;"&gt;()&lt;/span&gt; &lt;span style="color:#808080;"&gt;+&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;N&amp;#39;: &amp;#39;&lt;/span&gt; &lt;span style="color:#808080;"&gt;+&lt;/span&gt; &lt;span style="color:#ff00ff;"&gt;error_message&lt;/span&gt;&lt;span style="color:#808080;"&gt;()&lt;/span&gt; &lt;span style="color:#808080;"&gt;+&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;N&amp;#39; (&amp;#39;&lt;/span&gt; &lt;span style="color:#808080;"&gt;+&lt;/span&gt; &lt;span style="color:#ff00ff;"&gt;convert&lt;/span&gt;&lt;span style="color:#808080;"&gt;(&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;nvarchar&lt;/span&gt;&lt;span style="color:#808080;"&gt;,&lt;/span&gt; &lt;span style="color:#ff00ff;"&gt;error_number&lt;/span&gt;&lt;span style="color:#808080;"&gt;())&lt;/span&gt; &lt;span style="color:#808080;"&gt;+&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;N&amp;#39;)&amp;#39;&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        &lt;span style="color:#0000ff;"&gt;raiserror&lt;/span&gt;&lt;span style="color:#808080;"&gt;(&lt;/span&gt;@errormsg&lt;span style="color:#808080;"&gt;,&lt;/span&gt; 16&lt;span style="color:#808080;"&gt;,&lt;/span&gt; 1&lt;span style="color:#808080;"&gt;)&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff;"&gt;end&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;catch&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;go&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;revert&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;select&lt;/span&gt; &lt;span style="color:#ff00ff;"&gt;suser_name&lt;/span&gt;&lt;span style="color:#808080;"&gt;()&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;go&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#008000;"&gt;--revoke granted right&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;revoke&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;execute&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;on&lt;/span&gt; dbo&lt;span style="color:#808080;"&gt;.&lt;/span&gt;pTestInsert &lt;span style="color:#0000ff;"&gt;to&lt;/span&gt; [RemoteUser]&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;go&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:182d9d6c-288a-4eef-a3b1-4a346bde04f3" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#ddd;max-height:500px;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2.5em;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#008000;"&gt;---Scenario 4: user has explicit right to execute the procedure but the stored procedure is &lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#008000;"&gt;---DROPPED an CREATED in subsequent release&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;go&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#008000;"&gt;---this is executed at sa/dbo level&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;grant&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;execute&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;on&lt;/span&gt; dbo&lt;span style="color:#808080;"&gt;.&lt;/span&gt;pTestInsert &lt;span style="color:#0000ff;"&gt;to&lt;/span&gt; [RemoteUser]&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;go&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;go&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#008000;"&gt;---now DROP and CREATE the procedure&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;drop&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;procedure&lt;/span&gt; dbo&lt;span style="color:#808080;"&gt;.&lt;/span&gt;pTestInsert &lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;go&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;create&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;procedure&lt;/span&gt; dbo&lt;span style="color:#808080;"&gt;.&lt;/span&gt;pTestInsert&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;(&lt;/span&gt;@a &lt;span style="color:#0000ff;"&gt;int&lt;/span&gt;&lt;span style="color:#808080;"&gt;,&lt;/span&gt; @b &lt;span style="color:#0000ff;"&gt;int&lt;/span&gt;&lt;span style="color:#808080;"&gt;)&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; &lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;begin&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="color:#0000ff;"&gt;insert&lt;/span&gt; dbo&lt;span style="color:#808080;"&gt;.&lt;/span&gt;tTest&lt;span style="color:#808080;"&gt;(&lt;/span&gt;a&lt;span style="color:#808080;"&gt;,&lt;/span&gt; b&lt;span style="color:#808080;"&gt;)&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;values &lt;/span&gt;&lt;span style="color:#808080;"&gt;(&lt;/span&gt;@a&lt;span style="color:#808080;"&gt;,&lt;/span&gt; @a &lt;span style="color:#808080;"&gt;*&lt;/span&gt; @b&lt;span style="color:#808080;"&gt;)&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;end&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;go&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;execute&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;login&lt;/span&gt;&lt;span style="color:#808080;"&gt;=&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;&amp;#39;RemoteLogin&amp;#39;&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;select&lt;/span&gt; &lt;span style="color:#ff00ff;"&gt;suser_name&lt;/span&gt;&lt;span style="color:#808080;"&gt;()&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;go&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="color:#0000ff;"&gt;begin&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;try&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#008000;"&gt;---should fail&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        &lt;span style="color:#0000ff;"&gt;exec&lt;/span&gt; dbo&lt;span style="color:#808080;"&gt;.&lt;/span&gt;pTestInsert&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;1&lt;span style="color:#808080;"&gt;,&lt;/span&gt; 1&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt;&lt;span style="color:#808080;"&gt;;&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="color:#0000ff;"&gt;end&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;try&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff;"&gt;begin&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;catch&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        &lt;span style="color:#0000ff;"&gt;declare&lt;/span&gt; @errormsg &lt;span style="color:#0000ff;"&gt;nvarchar&lt;/span&gt;&lt;span style="color:#808080;"&gt;(&lt;/span&gt;2000&lt;span style="color:#808080;"&gt;)&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff;"&gt;set&lt;/span&gt; @errormsg &lt;span style="color:#808080;"&gt;=&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;N&amp;#39;Scenario 4: Exception occured in &amp;#39;&lt;/span&gt; &lt;span style="color:#808080;"&gt;+&lt;/span&gt; &lt;span style="color:#ff00ff;"&gt;error_procedure&lt;/span&gt;&lt;span style="color:#808080;"&gt;()&lt;/span&gt; &lt;span style="color:#808080;"&gt;+&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;N&amp;#39;: &amp;#39;&lt;/span&gt; &lt;span style="color:#808080;"&gt;+&lt;/span&gt; &lt;span style="color:#ff00ff;"&gt;error_message&lt;/span&gt;&lt;span style="color:#808080;"&gt;()&lt;/span&gt; &lt;span style="color:#808080;"&gt;+&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;N&amp;#39; (&amp;#39;&lt;/span&gt; &lt;span style="color:#808080;"&gt;+&lt;/span&gt; &lt;span style="color:#ff00ff;"&gt;convert&lt;/span&gt;&lt;span style="color:#808080;"&gt;(&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;nvarchar&lt;/span&gt;&lt;span style="color:#808080;"&gt;,&lt;/span&gt; &lt;span style="color:#ff00ff;"&gt;error_number&lt;/span&gt;&lt;span style="color:#808080;"&gt;())&lt;/span&gt; &lt;span style="color:#808080;"&gt;+&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;N&amp;#39;)&amp;#39;&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        &lt;span style="color:#0000ff;"&gt;raiserror&lt;/span&gt;&lt;span style="color:#808080;"&gt;(&lt;/span&gt;@errormsg&lt;span style="color:#808080;"&gt;,&lt;/span&gt; 16&lt;span style="color:#808080;"&gt;,&lt;/span&gt; 1&lt;span style="color:#808080;"&gt;)&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff;"&gt;end&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;catch&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;go&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;revert&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;select&lt;/span&gt; &lt;span style="color:#ff00ff;"&gt;suser_name&lt;/span&gt;&lt;span style="color:#808080;"&gt;()&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;Scenario 4 throws an exception as expected:&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:9d1b244c-fc66-46d3-8ddb-664d68c2a5bd" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background-color:#ffffff;max-height:400px;overflow:auto;padding:2px 5px;"&gt;Msg 50000, Level 16, State 1, Line 9&lt;br&gt; Scenario 4: Exception occured in pTestInsert: The EXECUTE permission was denied on the object &amp;#39;pTestInsert&amp;#39;, database &amp;#39;testExecRights&amp;#39;, schema &amp;#39;dbo&amp;#39;. (229)&lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;Obviously there is an issue here. The release process must take care of the proper assignment of the rights. The problem is if the script consists of smaller parts prepared by developers, it may be virtually impossible to verify if the rights are scripted correctly in the release script. One can script all explicit permissions to objects before the release and then reconcile them with the permissions existing after the release. This also can be tricky, as sometimes the rights &lt;em&gt;have&lt;/em&gt; to change, sometimes new objects don’t have properly scripted permissions etc. What if you have may users that need to perform certain operations on tables and views? In general, it looks like the idea of assigning rights one by one for all users and objects is bad in most scenarios. Imagine the amount of failure points if you have 3000+ objects in database and you change, say, a few hundred of them within the release. What can you do about it?&lt;/p&gt;  &lt;p&gt;First thing that you should do is to break explicit relationship of users and objects. You can do this by creating a database role which will serve as single point of security assignments. By having several roles in the database, you can tune the security fairly easily without having to rescript permissions for all users each time you need to make a change. Scenario 5 shows such database role example:&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:1da0b810-1d0a-4db9-bb82-6d1758fb4031" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#ddd;max-height:400px;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2.5em;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#008000;"&gt;---Scenario 5: belongs to DATABASE ROLE with explicit right to execute the procedure &lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#008000;"&gt;---and the stored procedure is DROPPED an CREATED in subsequent release&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;go&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#008000;"&gt;---this is executed at sa/dbo level&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;create&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;role&lt;/span&gt; [pTestExecRightRole]&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;go&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;grant&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;execute&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;on&lt;/span&gt; dbo&lt;span style="color:#808080;"&gt;.&lt;/span&gt;pTestInsert &lt;span style="color:#0000ff;"&gt;to&lt;/span&gt; [pTestExecRightRole]&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;go&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;exec&lt;/span&gt; &lt;span style="color:#800000;"&gt;sp_addrolemember&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;&amp;#39;pTestExecRightRole&amp;#39;&lt;/span&gt;&lt;span style="color:#808080;"&gt;,&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;&amp;#39;RemoteUser&amp;#39;&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;go&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#008000;"&gt;---now DROP and CREATE the procedure&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;drop&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;procedure&lt;/span&gt; dbo&lt;span style="color:#808080;"&gt;.&lt;/span&gt;pTestInsert &lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;go&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;create&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;procedure&lt;/span&gt; dbo&lt;span style="color:#808080;"&gt;.&lt;/span&gt;pTestInsert&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;(&lt;/span&gt;@a &lt;span style="color:#0000ff;"&gt;int&lt;/span&gt;&lt;span style="color:#808080;"&gt;,&lt;/span&gt; @b &lt;span style="color:#0000ff;"&gt;int&lt;/span&gt;&lt;span style="color:#808080;"&gt;)&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; &lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;begin&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff;"&gt;insert&lt;/span&gt; dbo&lt;span style="color:#808080;"&gt;.&lt;/span&gt;tTest&lt;span style="color:#808080;"&gt;(&lt;/span&gt;a&lt;span style="color:#808080;"&gt;,&lt;/span&gt; b&lt;span style="color:#808080;"&gt;)&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;values &lt;/span&gt;&lt;span style="color:#808080;"&gt;(&lt;/span&gt;@a&lt;span style="color:#808080;"&gt;,&lt;/span&gt; @a &lt;span style="color:#808080;"&gt;*&lt;/span&gt; @b&lt;span style="color:#808080;"&gt;)&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;end&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;go&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;execute&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;login&lt;/span&gt;&lt;span style="color:#808080;"&gt;=&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;&amp;#39;RemoteLogin&amp;#39;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;select&lt;/span&gt; &lt;span style="color:#ff00ff;"&gt;suser_name&lt;/span&gt;&lt;span style="color:#808080;"&gt;()&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;go&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff;"&gt;begin&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;try&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        &lt;span style="color:#008000;"&gt;---should succeed&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff;"&gt;exec&lt;/span&gt; dbo&lt;span style="color:#808080;"&gt;.&lt;/span&gt;pTestInsert&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;1&lt;span style="color:#808080;"&gt;,&lt;/span&gt; 1&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt;&lt;span style="color:#808080;"&gt;;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff;"&gt;end&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;try&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="color:#0000ff;"&gt;begin&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;catch&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff;"&gt;declare&lt;/span&gt; @errormsg &lt;span style="color:#0000ff;"&gt;nvarchar&lt;/span&gt;&lt;span style="color:#808080;"&gt;(&lt;/span&gt;2000&lt;span style="color:#808080;"&gt;)&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        &lt;span style="color:#0000ff;"&gt;set&lt;/span&gt; @errormsg &lt;span style="color:#808080;"&gt;=&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;N&amp;#39;Scenario 5: Exception occured in &amp;#39;&lt;/span&gt; &lt;span style="color:#808080;"&gt;+&lt;/span&gt; &lt;span style="color:#ff00ff;"&gt;error_procedure&lt;/span&gt;&lt;span style="color:#808080;"&gt;()&lt;/span&gt; &lt;span style="color:#808080;"&gt;+&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;N&amp;#39;: &amp;#39;&lt;/span&gt; &lt;span style="color:#808080;"&gt;+&lt;/span&gt; &lt;span style="color:#ff00ff;"&gt;error_message&lt;/span&gt;&lt;span style="color:#808080;"&gt;()&lt;/span&gt; &lt;span style="color:#808080;"&gt;+&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;N&amp;#39; (&amp;#39;&lt;/span&gt; &lt;span style="color:#808080;"&gt;+&lt;/span&gt; &lt;span style="color:#ff00ff;"&gt;convert&lt;/span&gt;&lt;span style="color:#808080;"&gt;(&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;nvarchar&lt;/span&gt;&lt;span style="color:#808080;"&gt;,&lt;/span&gt; &lt;span style="color:#ff00ff;"&gt;error_number&lt;/span&gt;&lt;span style="color:#808080;"&gt;())&lt;/span&gt; &lt;span style="color:#808080;"&gt;+&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;N&amp;#39;)&amp;#39;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff;"&gt;raiserror&lt;/span&gt;&lt;span style="color:#808080;"&gt;(&lt;/span&gt;@errormsg&lt;span style="color:#808080;"&gt;,&lt;/span&gt; 16&lt;span style="color:#808080;"&gt;,&lt;/span&gt; 1&lt;span style="color:#808080;"&gt;)&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="color:#0000ff;"&gt;end&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;catch&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;go&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;revert&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;select&lt;/span&gt; &lt;span style="color:#ff00ff;"&gt;suser_name&lt;/span&gt;&lt;span style="color:#808080;"&gt;()&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;go&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;exec&lt;/span&gt; &lt;span style="color:#800000;"&gt;sp_droprolemember&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;&amp;#39;pTestExecRightRole&amp;#39;&lt;/span&gt;&lt;span style="color:#808080;"&gt;,&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;&amp;#39;RemoteUser&amp;#39;&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;go&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;drop&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;role&lt;/span&gt; [pTestExecRightRole]&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;go&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;The issue with the above is that still, when an object is dropped and recreated, the rights assigned to it are lost. The script above throws the same exception again:&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:9b8f05fe-87c0-4311-8305-41f94dfa9312" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background-color:#ffffff;max-height:400px;overflow:auto;padding:2px 5px;"&gt;Msg 50000, Level 16, State 1, Line 9&lt;br&gt; Scenario 5: Exception occured in pTestInsert: The EXECUTE permission was denied on the object &amp;#39;pTestInsert&amp;#39;, database &amp;#39;testExecRights&amp;#39;, schema &amp;#39;dbo&amp;#39;. (229)&lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;To solve this you have to group objects in a similar way you do it with users. While database role is used as a container for usSers, &lt;strong&gt;schema&lt;/strong&gt;&amp;#160; serves the same purpose for database objects. Scenario 6 shows this approach.&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:86620007-3dec-40fe-b5e3-bb33e2a7b97d" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#ddd;max-height:400px;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2.5em;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#008000;"&gt;---Scenario 6: user belongs to DATABASE ROLE with explicit right to execute on the SCHEMA&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#008000;"&gt;---and the stored procedure is DROPPED an CREATED in subsequent release&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;go&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#008000;"&gt;---this is executed at sa/dbo level&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;create&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;role&lt;/span&gt; [pTestExecRightRole]&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;go&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;create&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;schema&lt;/span&gt; RemoteExec &lt;span style="color:#0000ff;"&gt;authorization&lt;/span&gt; dbo&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;go&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;grant&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;execute&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;on&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;schema&lt;/span&gt;&lt;span style="color:#808080;"&gt;::&lt;/span&gt;RemoteExec &lt;span style="color:#0000ff;"&gt;to&lt;/span&gt; [pTestExecRightRole]&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;go&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;exec&lt;/span&gt; &lt;span style="color:#800000;"&gt;sp_addrolemember&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;&amp;#39;pTestExecRightRole&amp;#39;&lt;/span&gt;&lt;span style="color:#808080;"&gt;,&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;&amp;#39;RemoteUser&amp;#39;&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;go&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#008000;"&gt;---now DROP and CREATE the procedure&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;drop&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;procedure&lt;/span&gt; dbo&lt;span style="color:#808080;"&gt;.&lt;/span&gt;pTestInsert &lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;go&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;create&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;procedure&lt;/span&gt; RemoteExec&lt;span style="color:#808080;"&gt;.&lt;/span&gt;pTestInsert&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;(&lt;/span&gt;@a &lt;span style="color:#0000ff;"&gt;int&lt;/span&gt;&lt;span style="color:#808080;"&gt;,&lt;/span&gt; @b &lt;span style="color:#0000ff;"&gt;int&lt;/span&gt;&lt;span style="color:#808080;"&gt;)&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; &lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;begin&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff;"&gt;insert&lt;/span&gt; dbo&lt;span style="color:#808080;"&gt;.&lt;/span&gt;tTest&lt;span style="color:#808080;"&gt;(&lt;/span&gt;a&lt;span style="color:#808080;"&gt;,&lt;/span&gt; b&lt;span style="color:#808080;"&gt;)&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;values &lt;/span&gt;&lt;span style="color:#808080;"&gt;(&lt;/span&gt;@a&lt;span style="color:#808080;"&gt;,&lt;/span&gt; @a &lt;span style="color:#808080;"&gt;*&lt;/span&gt; @b&lt;span style="color:#808080;"&gt;)&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;end&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;go&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;execute&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;login&lt;/span&gt;&lt;span style="color:#808080;"&gt;=&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;&amp;#39;RemoteLogin&amp;#39;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;select&lt;/span&gt; &lt;span style="color:#ff00ff;"&gt;suser_name&lt;/span&gt;&lt;span style="color:#808080;"&gt;()&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;go&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff;"&gt;begin&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;try&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        &lt;span style="color:#008000;"&gt;---should succeed&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff;"&gt;exec&lt;/span&gt; RemoteExec&lt;span style="color:#808080;"&gt;.&lt;/span&gt;pTestInsert&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;1&lt;span style="color:#808080;"&gt;,&lt;/span&gt; 1&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt;&lt;span style="color:#808080;"&gt;;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff;"&gt;end&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;try&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="color:#0000ff;"&gt;begin&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;catch&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff;"&gt;declare&lt;/span&gt; @errormsg &lt;span style="color:#0000ff;"&gt;nvarchar&lt;/span&gt;&lt;span style="color:#808080;"&gt;(&lt;/span&gt;2000&lt;span style="color:#808080;"&gt;)&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        &lt;span style="color:#0000ff;"&gt;set&lt;/span&gt; @errormsg &lt;span style="color:#808080;"&gt;=&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;N&amp;#39;Scenario 6: Exception occured in &amp;#39;&lt;/span&gt; &lt;span style="color:#808080;"&gt;+&lt;/span&gt; &lt;span style="color:#ff00ff;"&gt;error_procedure&lt;/span&gt;&lt;span style="color:#808080;"&gt;()&lt;/span&gt; &lt;span style="color:#808080;"&gt;+&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;N&amp;#39;: &amp;#39;&lt;/span&gt; &lt;span style="color:#808080;"&gt;+&lt;/span&gt; &lt;span style="color:#ff00ff;"&gt;error_message&lt;/span&gt;&lt;span style="color:#808080;"&gt;()&lt;/span&gt; &lt;span style="color:#808080;"&gt;+&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;N&amp;#39; (&amp;#39;&lt;/span&gt; &lt;span style="color:#808080;"&gt;+&lt;/span&gt; &lt;span style="color:#ff00ff;"&gt;convert&lt;/span&gt;&lt;span style="color:#808080;"&gt;(&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;nvarchar&lt;/span&gt;&lt;span style="color:#808080;"&gt;,&lt;/span&gt; &lt;span style="color:#ff00ff;"&gt;error_number&lt;/span&gt;&lt;span style="color:#808080;"&gt;())&lt;/span&gt; &lt;span style="color:#808080;"&gt;+&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;N&amp;#39;)&amp;#39;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff;"&gt;raiserror&lt;/span&gt;&lt;span style="color:#808080;"&gt;(&lt;/span&gt;@errormsg&lt;span style="color:#808080;"&gt;,&lt;/span&gt; 16&lt;span style="color:#808080;"&gt;,&lt;/span&gt; 1&lt;span style="color:#808080;"&gt;)&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="color:#0000ff;"&gt;end&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;catch&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;go&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;revert&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;select&lt;/span&gt; &lt;span style="color:#ff00ff;"&gt;suser_name&lt;/span&gt;&lt;span style="color:#808080;"&gt;()&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;go&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;As you see, I created schema RemoteExec in line 7. I granted explicit right to execute on &lt;strong&gt;all&lt;/strong&gt; objects belonging to the schema to the database role pTestExecRightRole. Subsequently I simulate release process by dropping existing procedure dbo.pTestInsert and creating it in the schema RemoteExec. As you see, I don’t apply any explicit rights to the stored procedure. The fact that it belongs to the RemoteExec schema is sufficient for it to be callable by the user belonging to pTestExecRightRole. You can easily add and remove procedures from the RemoteExec schema and they will automatically inherit any rights you have chosen to assign to them, without any manual coding. More importantly, the rights are not tied to the objects, so you will not loose required functionality after making changes to the objects.&lt;/p&gt;  &lt;p&gt;As I said before, this approach has its consequences. You may need to modify some of the code that calls the stored procedures to explicitly reference schema, in some cases you need to modify the code of the stored procedures. If you work with older code the most likely schema it references is an &lt;strong&gt;implicit&lt;/strong&gt; dbo – object names are used without any schema part. This may be OK and resolve at runtime, but in some cases it won’t especially if you move the procedure to a different schema. It is a good practice to explicitly reference schemas in your code to avoid any confusion and problems.&lt;/p&gt;  &lt;p&gt;You can download the test script with all scenarios and cleanup code from &lt;a href="http://6mxuuq.bay.livefilestore.com/y1pKQ8Haga-1tL-GUvgv9zWz-XeQsEKCT39JH_qqy_A48Mjc9gKQj4zyoBR0gBxvqBMWHxSgJoiZu-9Oe9NC6ZDk4SecA_-34Wa/TestingExecRights.sql?download&amp;amp;psid=1"&gt;this location&lt;/a&gt;. Enjoy.&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:f0a749f7-dd54-4f76-9db0-8f1bf2b14254" class="wlWriterEditableSmartContent"&gt;Technorati Tags: &lt;a href="http://technorati.com/tags/SQL+Server+2008" rel="tag"&gt;SQL Server 2008&lt;/a&gt;,&lt;a href="http://technorati.com/tags/T-SQL" rel="tag"&gt;T-SQL&lt;/a&gt;,&lt;a href="http://technorati.com/tags/programming" rel="tag"&gt;programming&lt;/a&gt;,&lt;a href="http://technorati.com/tags/database+design" rel="tag"&gt;database design&lt;/a&gt;&lt;/div&gt;&lt;img src="http://sqlblog.com/aggbug.aspx?PostID=34777" width="1" height="1"&gt;</description><category domain="http://sqlblog.com/blogs/piotr_rodak/archive/tags/database+design/default.aspx">database design</category><category domain="http://sqlblog.com/blogs/piotr_rodak/archive/tags/good+practices/default.aspx">good practices</category><category domain="http://sqlblog.com/blogs/piotr_rodak/archive/tags/SQL+Server+2008+R2/default.aspx">SQL Server 2008 R2</category><category domain="http://sqlblog.com/blogs/piotr_rodak/archive/tags/tips+and+tricks/default.aspx">tips and tricks</category><category domain="http://sqlblog.com/blogs/piotr_rodak/archive/tags/T-SQL/default.aspx">T-SQL</category></item><item><title>Uninstalling Reporting Server 2008 on Windows Server 2008</title><link>http://sqlblog.com/blogs/piotr_rodak/archive/2010/12/23/uninstalling-reporting-server-2008-on-windows-server-2008.aspx</link><pubDate>Thu, 23 Dec 2010 00:03:18 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:31909</guid><dc:creator>Piotr Rodak</dc:creator><slash:comments>9</slash:comments><comments>http://sqlblog.com/blogs/piotr_rodak/comments/31909.aspx</comments><wfw:commentRss>http://sqlblog.com/blogs/piotr_rodak/commentrss.aspx?PostID=31909</wfw:commentRss><description>&lt;p&gt;Ha. I had quite disputable pleasure of installing and reinstalling and reinstalling and reinstalling – I think about 5 times before it worked – Reporting Server 2008 on Windows Server with the same year number in name. &lt;/p&gt;  &lt;p&gt;During my struggle I came across an error which seems to be not quite unfamiliar to some more unfortunate developers and admins who happen to uninstall SSRS 2008 from the server. I had the SSRS 2008 installed as named instance, &lt;strong&gt;SQL2008&lt;/strong&gt;. I wanted to uninstall the server and install it to default instance. And this is when it bit me – not the first time and not the last that day &lt;img style="border-bottom-style:none;border-right-style:none;border-top-style:none;border-left-style:none;" class="wlEmoticon wlEmoticon-smile" alt="Smile" src="http://sqlblog.com/blogs/piotr_rodak/wlEmoticon-smile_2FA89F51.png" /&gt;. The setup complained that it couldn’t access a DLL:&lt;/p&gt;    &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:4a1cd680-0f0b-4d78-a2dd-cf4755a047e1" class="wlWriterEditableSmartContent"&gt;&lt;pre style="width:950px;height:324px;background-color:White;overflow:auto;"&gt;&lt;div&gt;&lt;span style="color:#000000;"&gt;Error message:

TITLE: Microsoft SQL Server &lt;/span&gt;&lt;span style="color:#000000;"&gt;2008&lt;/span&gt;&lt;span style="color:#000000;"&gt; Setup

&lt;/span&gt;&lt;span style="color:#000000;"&gt;------------------------------&lt;/span&gt;&lt;span style="color:#000000;"&gt;

The following error has occurred:

Access to the path &lt;/span&gt;&lt;span style="color:#000000;"&gt;'&lt;/span&gt;&lt;span style="color:#000000;"&gt;C:\Windows\SysWOW64\perf-ReportServer$SQL2008-rsctr.dll&lt;/span&gt;&lt;span style="color:#000000;"&gt;'&lt;/span&gt;&lt;span style="color:#000000;"&gt; is denied.

For help, click: http:&lt;/span&gt;&lt;span style="color:#008000;"&gt;//&lt;/span&gt;&lt;span style="color:#008000;"&gt;go.microsoft.com/fwlink?LinkID=20476&amp;amp;ProdName=Microsoft+SQL+Server&amp;amp;EvtSrc=setup.rll&amp;amp;EvtID=50000&amp;amp;ProdVer=10.0.1600.22&amp;amp;EvtType=0x60797DC7%25400x84E8D3C0&lt;/span&gt;&lt;span style="color:#008000;"&gt;
&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#000000;"&gt;------------------------------&lt;/span&gt;&lt;span style="color:#000000;"&gt;

BUTTONS:

OK&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This is a screenshot that shows the above error:&lt;/p&gt;

&lt;p&gt;&lt;img src="http://public.bay.livefilestore.com/y1pCN0negrAA7ln4AX9SQex7E9FLqCpuysstBd-mHMAaMTxUskqK3-xkXdGjdqzoJ-B32ldY9TqcTjadtaozaLAZA/ScreenShot021.jpg?psid=1" /&gt;&lt;/p&gt;

&lt;p&gt;This issue seems to have a bit of literature dedicated to it and even seemingly a KB article &lt;a title="http://support.microsoft.com/kb/956173" href="http://support.microsoft.com/kb/956173"&gt;http://support.microsoft.com/kb/956173&lt;/a&gt; and a similar Connect item: &lt;a title="http://connect.microsoft.com/SQLServer/feedback/details/363653/error-messages-when-upgrading-from-sql-2008-rc0-to-rtm" href="http://connect.microsoft.com/SQLServer/feedback/details/363653/error-messages-when-upgrading-from-sql-2008-rc0-to-rtm"&gt;http://connect.microsoft.com/SQLServer/feedback/details/363653/error-messages-when-upgrading-from-sql-2008-rc0-to-rtm&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The article describes issue as following:&lt;/p&gt;

&lt;p&gt;When you try to uninstall Microsoft SQL Server 2008 Reporting Services from the server, you may receive the following error message: &lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;An error has occurred: 
    &lt;br /&gt;Access to the path 'Drive_Letter:\WINDOWS\system32\perf-ReportServer-rsctr.dll' is denied. &lt;/p&gt;

  &lt;p&gt;&lt;b&gt;Note &lt;/b&gt;Drive_Letter refers to the disc drive into which the SQL Server installation media is inserted.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In my case, the &lt;strong&gt;Note&lt;/strong&gt; was not true; the error pointed to a dll that was located in Windows folder on C:\, not where the installation media were. &lt;/p&gt;

&lt;p&gt;&lt;img src="http://public.bay.livefilestore.com/y1pCN0negrAA7l3qBM1o2I8RtA3GEKcGYJRI0aDbmnL0SmFNJXNdG9_MWDeSZralYa6m_KDdImUOe0pdMrOWqhgEQ/ScreenShot022.jpg?psid=1" /&gt;&lt;/p&gt;

&lt;p&gt;Despite this difference I tried to identify any processes that might be keeping lock on the dll. I downloaded Sysinternals &lt;a href="http://technet.microsoft.com/en-us/sysinternals/bb896653"&gt;process explorer&lt;/a&gt; and ran it to find any processes I could stop. Unfortunately, there was no such process.&lt;/p&gt;

&lt;p&gt;I tried to rerun the installation, but it failed at the same step.&lt;/p&gt;

&lt;p&gt;Eventually I decided to remove the dll before the setup was executed. I changed name of the dll to be able to restore it in case of some issues. Interestingly, Windows let me do it, which means that indeed, it was not locked by any process.&lt;/p&gt;

&lt;p&gt;&lt;img src="http://public.bay.livefilestore.com/y1pRWEBMCwSUz24Phn7VhqsDoAumxEb8vENzx0_OXTBsPuoWkrAh_gLWmOR8QTgQwfJ8vzdGIGXtPjWzCTFLavN8A/ScreenShot024.jpg?psid=1" /&gt;&lt;/p&gt;

&lt;p&gt;I ran the setup and this time it uninstalled the instance without any problems:&lt;/p&gt;

&lt;p&gt;&lt;img src="http://public.bay.livefilestore.com/y1pCN0negrAA7nby79V4ZYIHWkUALMPnDEHk_baD2CRXOzvUwaAPo7wDcaCqbRoOS66mK1PATt8pFjNZVY_gKuywA/ScreenShot023.jpg?psid=1" /&gt;&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;To summarize my experience I should say – be &lt;strong&gt;very&lt;/strong&gt; careful, don’t leave any leftovers after uninstallation – remove/rename any folders that are left after setup has finished. For some reason, setup doesn’t remove folders and certain files. Installation on Windows Server 2008 requires more attention than on Windows 2003 because of the changed security model, some actions can be executed only by administrator in elevated execution mode. In general, you have to get used to &lt;a href="http://en.wikipedia.org/wiki/User_Account_Control"&gt;UAC&lt;/a&gt; and a bit different experience than with Windows Server 2003.&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:5ecca658-04dd-491c-97b9-7b7e7def3df9" class="wlWriterEditableSmartContent"&gt;Technorati Tags: &lt;a href="http://technorati.com/tags/SQL+Server+2008" rel="tag"&gt;SQL Server 2008&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Windows+Server+2008" rel="tag"&gt;Windows Server 2008&lt;/a&gt;,&lt;a href="http://technorati.com/tags/SRS" rel="tag"&gt;SRS&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Reporting+Services" rel="tag"&gt;Reporting Services&lt;/a&gt;&lt;/div&gt;&lt;img src="http://sqlblog.com/aggbug.aspx?PostID=31909" width="1" height="1"&gt;</description><category domain="http://sqlblog.com/blogs/piotr_rodak/archive/tags/SQL+Server+2008/default.aspx">SQL Server 2008</category><category domain="http://sqlblog.com/blogs/piotr_rodak/archive/tags/SSRS/default.aspx">SSRS</category><category domain="http://sqlblog.com/blogs/piotr_rodak/archive/tags/tips+and+tricks/default.aspx">tips and tricks</category><category domain="http://sqlblog.com/blogs/piotr_rodak/archive/tags/Windows+Server+2008/default.aspx">Windows Server 2008</category></item><item><title>BNF – how to read syntax?</title><link>http://sqlblog.com/blogs/piotr_rodak/archive/2010/12/07/bnf-how-to-read-syntax.aspx</link><pubDate>Tue, 07 Dec 2010 21:40:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:31432</guid><dc:creator>Piotr Rodak</dc:creator><slash:comments>0</slash:comments><comments>http://sqlblog.com/blogs/piotr_rodak/comments/31432.aspx</comments><wfw:commentRss>http://sqlblog.com/blogs/piotr_rodak/commentrss.aspx?PostID=31432</wfw:commentRss><description>&lt;p&gt;A few days ago I read &lt;a href="http://www.midnightdba.com/Jen/2010/12/rtfm365/"&gt;post&lt;/a&gt; of Jen McCown (&lt;a href="http://www.midnightdba.com/Jen/"&gt;blog&lt;/a&gt;) about her idea of blogging about random articles from Books Online. I think this is a great idea, even if Jen says that it’s not exciting or sexy. I noticed that many of the questions that appear on forums and other media arise from pure fact that people asking questions didn’t bother to read and understand the manual – Books Online. Jen came up with a brilliant, concise acronym that describes very well the category of posts about Books Online – RTFM365. I take liberty of tagging this post with the same acronym.&lt;/p&gt;  &lt;p&gt;I often come across questions of type – ‘Hey, i am trying to create a table, but I am getting an error’. The error often says that the syntax is invalid. &lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:a2589512-058f-43f5-abd1-272868fb916e" class="wlWriterEditableSmartContent"&gt;&lt;pre style="width:950px;height:92px;background-color:White;overflow:auto;"&gt;&lt;div&gt;&lt;span style="color:#008080;"&gt;1&lt;/span&gt; &lt;span style="color:#0000FF;"&gt;CREATE&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;TABLE&lt;/span&gt;&lt;span style="color:#000000;"&gt; dbo.Employees
&lt;/span&gt;&lt;span style="color:#008080;"&gt;2&lt;/span&gt; &lt;span style="color:#000000;"&gt;    (guid &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;uniqueidentifier&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;CONSTRAINT&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;DEFAULT&lt;/span&gt;&lt;span style="color:#000000;"&gt; Guid_Default NEWSEQUENTIALID() &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;ROWGUIDCOL&lt;/span&gt;&lt;span style="color:#000000;"&gt;,
&lt;/span&gt;&lt;span style="color:#008080;"&gt;3&lt;/span&gt; &lt;span style="color:#000000;"&gt;    Employee_Name &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;varchar&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#800000;font-weight:bold;"&gt;60&lt;/span&gt;&lt;span style="color:#000000;"&gt;)
&lt;/span&gt;&lt;span style="color:#008080;"&gt;4&lt;/span&gt; &lt;span style="color:#000000;"&gt;    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;CONSTRAINT&lt;/span&gt;&lt;span style="color:#000000;"&gt; Guid_PK &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;PRIMARY&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;KEY&lt;/span&gt;&lt;span style="color:#000000;"&gt; (guid) );
&lt;/span&gt;&lt;span style="color:#008080;"&gt;5&lt;/span&gt; &lt;span style="color:#000000;"&gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The answer is usually(1), ‘Ok, let me check it out.. Ah yes – you have to put name of the DEFAULT constraint before the type of constraint:&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:6d8de86f-5fdb-4eb2-b837-360974021d50" class="wlWriterEditableSmartContent"&gt;&lt;pre style="width:950px;height:71px;background-color:White;overflow:auto;"&gt;&lt;div&gt;&lt;span style="color:#008080;"&gt;1&lt;/span&gt; &lt;span style="color:#0000FF;"&gt;CREATE&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;TABLE&lt;/span&gt;&lt;span style="color:#000000;"&gt; dbo.Employees
&lt;/span&gt;&lt;span style="color:#008080;"&gt;2&lt;/span&gt; &lt;span style="color:#000000;"&gt;    (guid &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;uniqueidentifier&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;CONSTRAINT&lt;/span&gt;&lt;span style="color:#000000;"&gt; Guid_Default &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;DEFAULT&lt;/span&gt;&lt;span style="color:#000000;"&gt; NEWSEQUENTIALID() &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;ROWGUIDCOL&lt;/span&gt;&lt;span style="color:#000000;"&gt;,
&lt;/span&gt;&lt;span style="color:#008080;"&gt;3&lt;/span&gt; &lt;span style="color:#000000;"&gt;    Employee_Name &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;varchar&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#800000;font-weight:bold;"&gt;60&lt;/span&gt;&lt;span style="color:#000000;"&gt;)
&lt;/span&gt;&lt;span style="color:#008080;"&gt;4&lt;/span&gt; &lt;span style="color:#000000;"&gt;    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;CONSTRAINT&lt;/span&gt;&lt;span style="color:#000000;"&gt; Guid_PK &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;PRIMARY&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;KEY&lt;/span&gt;&lt;span style="color:#000000;"&gt; (guid) );&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Why many people stumble on syntax errors? Is the syntax poorly documented? No, the issue is, that correct syntax of the &lt;a href="http://msdn.microsoft.com/en-us/library/ms174979.aspx"&gt;CREATE TABLE&lt;/a&gt; statement is documented very well in Books Online and is.. intimidating. Many people can be taken aback by the rather complex block of code that describes all intricacies of the statement. &lt;/p&gt;

&lt;p&gt;However, I don’t know better way of defining syntax of the statement or command.&lt;/p&gt;

&lt;p&gt;The notation that is used to describe syntax in Books Online is a form of &lt;a href="http://en.wikipedia.org/wiki/Backus%E2%80%93Naur_Form"&gt;Backus-Naur notatiion&lt;/a&gt;, called &lt;strong&gt;&lt;em&gt;BNF&lt;/em&gt;&lt;/strong&gt; for short sometimes. This is a notation that was invented around 50 years ago, and some say that even earlier, around 400 BC – would you believe? Originally it was used to define syntax of, rather ancient now, &lt;a href="http://en.wikipedia.org/wiki/ALGOL"&gt;ALGOL&lt;/a&gt; programming language (in 1950’s, not in ancient India).&lt;/p&gt;

&lt;p&gt;If you look closer at the definition of the BNF, it turns out that the principles of this syntax are pretty simple. Here are a few bullet points:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;em&gt;italic_text&lt;/em&gt; is a placeholder for your identifier &lt;/li&gt;

  &lt;li&gt;&amp;lt;&lt;em&gt;italic_text_in_angle_brackets&lt;/em&gt;&amp;gt; is a definition which is described further. &lt;/li&gt;

  &lt;li&gt;[everything in square brackets] is optional &lt;/li&gt;

  &lt;li&gt;{everything in curly brackets} is obligatory &lt;/li&gt;

  &lt;li&gt;everything | separated | by | operator is an alternative &lt;/li&gt;

  &lt;li&gt;::= “assigns” definition to an identifier &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Yes, it looks like these six simple points give you the key to understand even the most complicated syntax definitions in Books Online. Books Online contain an article about &lt;a href="http://msdn.microsoft.com/en-us/library/ms177563.aspx"&gt;syntax conventions&lt;/a&gt; – have you ever read it? &lt;img style="border-bottom-style:none;border-right-style:none;border-top-style:none;border-left-style:none;" class="wlEmoticon wlEmoticon-smile" alt="Smile" src="http://sqlblog.com/blogs/piotr_rodak/wlEmoticon-smile_1E378396.png" /&gt;&lt;/p&gt;

&lt;p&gt;Let’s have a look at fragment of the CREATE TABLE statement:&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" class="wlWriterSmartContent"&gt;
  &lt;pre style="background-color:white;width:950px;height:213px;overflow:auto;"&gt;&lt;div&gt;&lt;span style="color:#008080;"&gt; 1&lt;/span&gt; &lt;span style="color:#000000;"&gt;CREATE TABLE 
&lt;/span&gt;&lt;span style="color:#008080;"&gt; 2&lt;/span&gt; &lt;span style="color:#000000;"&gt;    [ &lt;i&gt;database_name&lt;/i&gt; . [ &lt;i&gt;schema_name&lt;/i&gt; ] . | &lt;i&gt;schema_name&lt;/i&gt; . ] &lt;i&gt;table_name&lt;/i&gt; 
&lt;/span&gt;&lt;span style="color:#008080;"&gt; 3&lt;/span&gt; &lt;span style="color:#000000;"&gt;        ( { &lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;column_definition&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; | &lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;computed_column_definition&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; 
&lt;/span&gt;&lt;span style="color:#008080;"&gt; 4&lt;/span&gt; &lt;span style="color:#000000;"&gt;                | &lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;column_set_definition&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; }
&lt;/span&gt;&lt;span style="color:#008080;"&gt; 5&lt;/span&gt; &lt;span style="color:#000000;"&gt;        [ &lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;table_constraint&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; ] [ ,...n ] ) 
&lt;/span&gt;&lt;span style="color:#008080;"&gt; 6&lt;/span&gt; &lt;span style="color:#000000;"&gt;    [ ON { &lt;i&gt;partition_scheme_name&lt;/i&gt; ( &lt;/i&gt;partition_column_name&lt;/i&gt; ) | &lt;i&gt;filegroup&lt;/i&gt; 
&lt;/span&gt;&lt;span style="color:#008080;"&gt; 7&lt;/span&gt; &lt;span style="color:#000000;"&gt;        | &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;default&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt; } ] 
&lt;/span&gt;&lt;span style="color:#008080;"&gt; 8&lt;/span&gt; &lt;span style="color:#000000;"&gt;    [ { TEXTIMAGE_ON { &lt;i&gt;filegroup&lt;/i&gt; | &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;default&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt; } ] 
&lt;/span&gt;&lt;span style="color:#008080;"&gt; 9&lt;/span&gt; &lt;span style="color:#000000;"&gt;    [ FILESTREAM_ON { &lt;i&gt;partition_scheme_name&lt;/i&gt; | &lt;i&gt;filegroup&lt;/i&gt; 
&lt;/span&gt;&lt;span style="color:#008080;"&gt;10&lt;/span&gt; &lt;span style="color:#000000;"&gt;        | &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;default&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt; } ]
&lt;/span&gt;&lt;span style="color:#008080;"&gt;11&lt;/span&gt; &lt;span style="color:#000000;"&gt;    [ WITH ( &lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;table_option&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; [ ,...n ] ) ]
&lt;/span&gt;&lt;span style="color:#008080;"&gt;12&lt;/span&gt; &lt;span style="color:#000000;"&gt;[ ; ]&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Let’s look at line 2 of the above snippet: This line uses rules 3 and 5 from the list. So you know that you can create table which has specified one of the following.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;just name – table will be created in default user schema &lt;/li&gt;

  &lt;li&gt;schema name and table name – table will be created in specified schema &lt;/li&gt;

  &lt;li&gt;database name, schema name and table name – table will be created in specified database, in specified schema &lt;/li&gt;

  &lt;li&gt;database name, .., table name – table will be created in specified database, in default schema of the user. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Note that this single line of the notation describes each of the naming schemes in deterministic way. The ‘optionality’ of the schema_name element is nested within database_name.. section. You can use either database_name and optional schema name, &lt;strong&gt;or&lt;/strong&gt; just schema name – this is specified by the pipe character ‘|’.&lt;/p&gt;

&lt;p&gt;The error that user gets with execution of the first script fragment in this post is as follows:&lt;/p&gt;

&lt;pre&gt;&lt;pre style="background-color:#ffffff;margin:0em;width:100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;&lt;font color="#ff0000"&gt;Msg 156, Level 15, State 1, Line 2&lt;/font&gt;
&lt;/pre&gt;&lt;pre style="background-color:#ffffff;margin:0em;width:100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;"&gt;&lt;font color="#ff0000"&gt;Incorrect syntax near the keyword 'DEFAULT'.&lt;/font&gt;&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;Ok, let’s have a look how to find out the correct syntax. Line number 3 of the BNF fragment above contains reference to &lt;font face="Courier New"&gt;&amp;lt;column_definition&amp;gt;&lt;/font&gt;. Since column_definition is in angle brackets, we know that this is a reference to notion described further in the code. And indeed, the very next fragment of BNF contains syntax of the column definition.&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" class="wlWriterSmartContent"&gt;
  &lt;pre style="background-color:white;width:950px;height:199px;overflow:auto;"&gt;&lt;div&gt;&lt;span style="color:#008080;"&gt; 1&lt;/span&gt; &lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;column_definition&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; ::&lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt; 2&lt;/span&gt; &lt;span style="color:#000000;"&gt;&lt;i&gt;column_name&lt;/i&gt; &lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;data_&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;type&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt; 3&lt;/span&gt; &lt;span style="color:#000000;"&gt;    [ FILESTREAM ]
&lt;/span&gt;&lt;span style="color:#008080;"&gt; 4&lt;/span&gt; &lt;span style="color:#000000;"&gt;    [ COLLATE &lt;i&gt;collation_name&lt;/i&gt; ] 
&lt;/span&gt;&lt;span style="color:#008080;"&gt; 5&lt;/span&gt; &lt;span style="color:#000000;"&gt;    [ NULL | NOT NULL ]
&lt;/span&gt;&lt;span style="color:#008080;"&gt; 6&lt;/span&gt; &lt;span style="color:#000000;"&gt;    [ 
&lt;/span&gt;&lt;span style="color:#008080;"&gt; 7&lt;/span&gt; &lt;span style="color:#000000;"&gt;        [ CONSTRAINT &lt;i&gt;constraint_name&lt;/i&gt; ] DEFAULT &lt;i&gt;constant_expression&lt;/i&gt; ] 
&lt;/span&gt;&lt;span style="color:#008080;"&gt; 8&lt;/span&gt; &lt;span style="color:#000000;"&gt;      | [ IDENTITY [ ( &lt;i&gt;seed&lt;/i&gt; ,&lt;i&gt;increment&lt;/i&gt; ) ] [ NOT FOR REPLICATION ] 
&lt;/span&gt;&lt;span style="color:#008080;"&gt; 9&lt;/span&gt; &lt;span style="color:#000000;"&gt;    ]
&lt;/span&gt;&lt;span style="color:#008080;"&gt;10&lt;/span&gt; &lt;span style="color:#000000;"&gt;    [ ROWGUIDCOL ] [ &lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;column_constraint&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; [ ...n ] ] 
&lt;/span&gt;&lt;span style="color:#008080;"&gt;11&lt;/span&gt; &lt;span style="color:#000000;"&gt;    [ SPARSE ] &lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Look at line 7 in the above fragment. It says, that the column can have a DEFAULT constraint which, if you want to name it, has to be prepended with [CONSTRAINT constraint_name] sequence. The name of the constraint is optional, but I &lt;a href="http://usepubs.blogspot.com/2010/06/coding-practices-call-your-constraints.html"&gt;strongly recommend&lt;/a&gt; you to make the effort of coming up with some meaningful name yourself. So the correct syntax of the CREATE TABLE statement from the beginning of the article is like this:&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:f5f10477-2fb7-44dc-a78d-671b4802a4da" class="wlWriterEditableSmartContent"&gt;&lt;pre style="width:950px;height:71px;background-color:White;overflow:auto;"&gt;&lt;div&gt;&lt;span style="color:#008080;"&gt;1&lt;/span&gt; &lt;span style="color:#0000FF;"&gt;CREATE&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;TABLE&lt;/span&gt;&lt;span style="color:#000000;"&gt; dbo.Employees
&lt;/span&gt;&lt;span style="color:#008080;"&gt;2&lt;/span&gt; &lt;span style="color:#000000;"&gt;    (guid &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;uniqueidentifier&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;CONSTRAINT&lt;/span&gt;&lt;span style="color:#000000;"&gt; Guid_Default &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;DEFAULT&lt;/span&gt;&lt;span style="color:#000000;"&gt; NEWSEQUENTIALID() &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;ROWGUIDCOL&lt;/span&gt;&lt;span style="color:#000000;"&gt;,
&lt;/span&gt;&lt;span style="color:#008080;"&gt;3&lt;/span&gt; &lt;span style="color:#000000;"&gt;    Employee_Name &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;varchar&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#800000;font-weight:bold;"&gt;60&lt;/span&gt;&lt;span style="color:#000000;"&gt;)
&lt;/span&gt;&lt;span style="color:#008080;"&gt;4&lt;/span&gt; &lt;span style="color:#000000;"&gt;    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;CONSTRAINT&lt;/span&gt;&lt;span style="color:#000000;"&gt; Guid_PK &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;PRIMARY&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;KEY&lt;/span&gt;&lt;span style="color:#000000;"&gt; (guid) );&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;That is practically everything you should know about BNF. I encourage you to study the syntax definitions for various statements and commands in Books Online, you can find really interesting things hidden there.&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:eff33da7-0bc1-4f1a-8dac-c9e849767944" class="wlWriterEditableSmartContent"&gt;Technorati Tags: &lt;a href="http://technorati.com/tags/SQL+Server" rel="tag"&gt;SQL Server&lt;/a&gt;,&lt;a href="http://technorati.com/tags/t-sql" rel="tag"&gt;t-sql&lt;/a&gt;,&lt;a href="http://technorati.com/tags/BNF" rel="tag"&gt;BNF&lt;/a&gt;,&lt;a href="http://technorati.com/tags/syntax" rel="tag"&gt;syntax&lt;/a&gt;&lt;/div&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;(1) No, my answer usually is a question – ‘What error message? What does it say?’. You’d be surprised to know how many people think I can go through time and space and look at their screen at the moment they received the error.&lt;/p&gt;&lt;img src="http://sqlblog.com/aggbug.aspx?PostID=31432" width="1" height="1"&gt;</description><category domain="http://sqlblog.com/blogs/piotr_rodak/archive/tags/BNF/default.aspx">BNF</category><category domain="http://sqlblog.com/blogs/piotr_rodak/archive/tags/database+design/default.aspx">database design</category><category domain="http://sqlblog.com/blogs/piotr_rodak/archive/tags/RTFM365/default.aspx">RTFM365</category><category domain="http://sqlblog.com/blogs/piotr_rodak/archive/tags/SQL+Server+2008+R2/default.aspx">SQL Server 2008 R2</category><category domain="http://sqlblog.com/blogs/piotr_rodak/archive/tags/tips+and+tricks/default.aspx">tips and tricks</category><category domain="http://sqlblog.com/blogs/piotr_rodak/archive/tags/T-SQL/default.aspx">T-SQL</category></item><item><title>Setting reporting database–what happens behind the scenes</title><link>http://sqlblog.com/blogs/piotr_rodak/archive/2010/11/28/setting-reporting-database-what-happens-behind-the-scenes.aspx</link><pubDate>Sun, 28 Nov 2010 14:30:07 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:31080</guid><dc:creator>Piotr Rodak</dc:creator><slash:comments>0</slash:comments><comments>http://sqlblog.com/blogs/piotr_rodak/comments/31080.aspx</comments><wfw:commentRss>http://sqlblog.com/blogs/piotr_rodak/commentrss.aspx?PostID=31080</wfw:commentRss><description>&lt;p&gt;When you install reporting server, you have in general two options available – either you install the reporting server and configure its database at the same time, or install just reporting service and point it to an existing database later. &lt;/p&gt;  &lt;p&gt;We had recently opportunity to configure server to point to existing reporting database. We encountered a security related issue, because the account we were using to connect to the SQL Server database engine was not ‘good enough’ to configure reporting service connectivity. Namely, the installation fails if you are not a sysadmin, security admin, or dbo on master and msdb databases.&lt;/p&gt;  &lt;p&gt;So what is happening if you are configuring reporting server to point to existing database? This is how it seems to work in SQL Server 2008 R2:&lt;/p&gt;  &lt;p&gt;First, new role called RSExecRole is added to master database if it doesn’t exist:&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:7979e16d-e622-46b2-8424-437414e0e116" class="wlWriterEditableSmartContent"&gt;&lt;pre style="width:950px;height:138px;background-color:White;overflow:auto;"&gt;&lt;div&gt;&lt;span style="color:#008080;"&gt;1&lt;/span&gt; &lt;span style="color:#0000FF;"&gt;use&lt;/span&gt;&lt;span style="color:#000000;"&gt; master
&lt;/span&gt;&lt;span style="color:#008080;"&gt;2&lt;/span&gt; &lt;span style="color:#000000;"&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;go&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;3&lt;/span&gt; &lt;span style="color:#000000;"&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;if&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;not&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;exists&lt;/span&gt;&lt;span style="color:#000000;"&gt; (&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;select&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;*&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;from&lt;/span&gt;&lt;span style="color:#000000;"&gt; sysusers &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;where&lt;/span&gt;&lt;span style="color:#000000;"&gt; issqlrole &lt;/span&gt;&lt;span style="color:#808080;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;font-weight:bold;"&gt;1&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;and&lt;/span&gt;&lt;span style="color:#000000;"&gt; name &lt;/span&gt;&lt;span style="color:#808080;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;'&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;RSExecRole&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;'&lt;/span&gt;&lt;span style="color:#000000;"&gt;)
&lt;/span&gt;&lt;span style="color:#008080;"&gt;4&lt;/span&gt; &lt;span style="color:#000000;"&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;BEGIN&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;5&lt;/span&gt; &lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;EXEC&lt;/span&gt;&lt;span style="color:#000000;"&gt; sp_addrole &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;'&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;RSExecRole&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;'&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;6&lt;/span&gt; &lt;span style="color:#000000;"&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;END&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;7&lt;/span&gt; &lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;8&lt;/span&gt; &lt;span style="color:#000000;"&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;go&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Then do the same in the msdb database:&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:3bcec569-f2ec-45f5-925a-243600ace2d3" class="wlWriterEditableSmartContent"&gt;&lt;pre style="width:950px;height:167px;background-color:White;overflow:auto;"&gt;&lt;div&gt;&lt;span style="color:#008080;"&gt;1&lt;/span&gt; &lt;span style="color:#0000FF;"&gt;use&lt;/span&gt;&lt;span style="color:#000000;"&gt; msdb
&lt;/span&gt;&lt;span style="color:#008080;"&gt;2&lt;/span&gt; &lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;3&lt;/span&gt; &lt;span style="color:#000000;"&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;go&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;4&lt;/span&gt; &lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;5&lt;/span&gt; &lt;span style="color:#000000;"&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;if&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;not&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;exists&lt;/span&gt;&lt;span style="color:#000000;"&gt; (&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;select&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;*&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;from&lt;/span&gt;&lt;span style="color:#000000;"&gt; sysusers &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;where&lt;/span&gt;&lt;span style="color:#000000;"&gt; issqlrole &lt;/span&gt;&lt;span style="color:#808080;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;font-weight:bold;"&gt;1&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;and&lt;/span&gt;&lt;span style="color:#000000;"&gt; name &lt;/span&gt;&lt;span style="color:#808080;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;'&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;RSExecRole&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;'&lt;/span&gt;&lt;span style="color:#000000;"&gt;)
&lt;/span&gt;&lt;span style="color:#008080;"&gt;6&lt;/span&gt; &lt;span style="color:#000000;"&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;BEGIN&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;7&lt;/span&gt; &lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;EXEC&lt;/span&gt;&lt;span style="color:#000000;"&gt; sp_addrole &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;'&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;RSExecRole&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;'&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;8&lt;/span&gt; &lt;span style="color:#000000;"&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;END&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;9&lt;/span&gt; &lt;span style="color:#000000;"&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;go&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;And in reporting database and the temp database for reporting:&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:edf9fa6d-3ab2-4e60-bca3-2205fc08df35" class="wlWriterEditableSmartContent"&gt;&lt;pre style="width:950px;height:364px;background-color:White;overflow:auto;"&gt;&lt;div&gt;&lt;span style="color:#008080;"&gt; 1&lt;/span&gt; &lt;span style="color:#0000FF;"&gt;USE&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;[&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;ReportServer$SQL1&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;]&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt; 2&lt;/span&gt; &lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt; 3&lt;/span&gt; &lt;span style="color:#000000;"&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;go&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt; 4&lt;/span&gt; &lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt; 5&lt;/span&gt; &lt;span style="color:#000000;"&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;if&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;not&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;exists&lt;/span&gt;&lt;span style="color:#000000;"&gt; (&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;select&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;*&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;from&lt;/span&gt;&lt;span style="color:#000000;"&gt; sysusers &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;where&lt;/span&gt;&lt;span style="color:#000000;"&gt; issqlrole &lt;/span&gt;&lt;span style="color:#808080;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;font-weight:bold;"&gt;1&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;and&lt;/span&gt;&lt;span style="color:#000000;"&gt; name &lt;/span&gt;&lt;span style="color:#808080;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;'&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;RSExecRole&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;'&lt;/span&gt;&lt;span style="color:#000000;"&gt;)
&lt;/span&gt;&lt;span style="color:#008080;"&gt; 6&lt;/span&gt; &lt;span style="color:#000000;"&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;BEGIN&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt; 7&lt;/span&gt; &lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;EXEC&lt;/span&gt;&lt;span style="color:#000000;"&gt; sp_addrole &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;'&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;RSExecRole&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;'&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt; 8&lt;/span&gt; &lt;span style="color:#000000;"&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;END&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt; 9&lt;/span&gt; &lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;10&lt;/span&gt; &lt;span style="color:#000000;"&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;go&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;11&lt;/span&gt; &lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;12&lt;/span&gt; &lt;span style="color:#000000;"&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;USE&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;[&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;ReportServer$SQL1TempDB&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;]&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;13&lt;/span&gt; &lt;span style="color:#000000;"&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;go&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;14&lt;/span&gt; &lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;15&lt;/span&gt; &lt;span style="color:#000000;"&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;if&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;not&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;exists&lt;/span&gt;&lt;span style="color:#000000;"&gt; (&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;select&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;*&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;from&lt;/span&gt;&lt;span style="color:#000000;"&gt; sysusers &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;where&lt;/span&gt;&lt;span style="color:#000000;"&gt; issqlrole &lt;/span&gt;&lt;span style="color:#808080;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;font-weight:bold;"&gt;1&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;and&lt;/span&gt;&lt;span style="color:#000000;"&gt; name &lt;/span&gt;&lt;span style="color:#808080;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;'&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;RSExecRole&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;'&lt;/span&gt;&lt;span style="color:#000000;"&gt;)
&lt;/span&gt;&lt;span style="color:#008080;"&gt;16&lt;/span&gt; &lt;span style="color:#000000;"&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;BEGIN&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;17&lt;/span&gt; &lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;EXEC&lt;/span&gt;&lt;span style="color:#000000;"&gt; sp_addrole &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;'&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;RSExecRole&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;'&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;18&lt;/span&gt; &lt;span style="color:#000000;"&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;END&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;19&lt;/span&gt; &lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;20&lt;/span&gt; &lt;span style="color:#000000;"&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;go&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;21&lt;/span&gt; &lt;span style="color:#000000;"&gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Next the reporting server configuration manager is mapping user to the login we want to use in each of the databases, interestingly in the order as below:&lt;/p&gt;

&lt;p&gt;
  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:1a98a7e5-0d57-4361-a09d-56d51f0988e7" class="wlWriterEditableSmartContent"&gt;&lt;pre style="width:950px;height:499px;background-color:White;overflow:auto;"&gt;&lt;div&gt;&lt;span style="color:#008080;"&gt; 1&lt;/span&gt; &lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt; 2&lt;/span&gt; &lt;span style="color:#000000;"&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;USE&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;[&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;ReportServer$SQL1&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;]&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt; 3&lt;/span&gt; &lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt; 4&lt;/span&gt; &lt;span style="color:#000000;"&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;go&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt; 5&lt;/span&gt; &lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt; 6&lt;/span&gt; &lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt; 7&lt;/span&gt; &lt;span style="color:#000000;"&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;if&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;not&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;exists&lt;/span&gt;&lt;span style="color:#000000;"&gt; (&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;select&lt;/span&gt;&lt;span style="color:#000000;"&gt; name &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;from&lt;/span&gt;&lt;span style="color:#000000;"&gt; master.dbo.syslogins &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;where&lt;/span&gt;&lt;span style="color:#000000;"&gt; name &lt;/span&gt;&lt;span style="color:#808080;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; N&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;'&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;RSUser&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;'&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;and&lt;/span&gt;&lt;span style="color:#000000;"&gt; sysadmin &lt;/span&gt;&lt;span style="color:#808080;"&gt;=&lt;/span&gt;&lt;span style="color:#800000;font-weight:bold;"&gt;1&lt;/span&gt;&lt;span style="color:#000000;"&gt;)
&lt;/span&gt;&lt;span style="color:#008080;"&gt; 8&lt;/span&gt; &lt;span style="color:#000000;"&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;BEGIN&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt; 9&lt;/span&gt; &lt;span style="color:#000000;"&gt;    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;if&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;not&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;exists&lt;/span&gt;&lt;span style="color:#000000;"&gt; (&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;select&lt;/span&gt;&lt;span style="color:#000000;"&gt; name &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;from&lt;/span&gt;&lt;span style="color:#000000;"&gt; sysusers &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;where&lt;/span&gt;&lt;span style="color:#000000;"&gt; name &lt;/span&gt;&lt;span style="color:#808080;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; N&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;'&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;RSUser&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;'&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;and&lt;/span&gt;&lt;span style="color:#000000;"&gt; issqluser &lt;/span&gt;&lt;span style="color:#808080;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;font-weight:bold;"&gt;1&lt;/span&gt;&lt;span style="color:#000000;"&gt;)
&lt;/span&gt;&lt;span style="color:#008080;"&gt;10&lt;/span&gt; &lt;span style="color:#000000;"&gt;    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;BEGIN&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;11&lt;/span&gt; &lt;span style="color:#000000;"&gt;        &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;EXEC&lt;/span&gt;&lt;span style="color:#000000;"&gt; sp_grantdbaccess N&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;'&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;RSUser&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;'&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;12&lt;/span&gt; &lt;span style="color:#000000;"&gt;    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;END&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;13&lt;/span&gt; &lt;span style="color:#000000;"&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;END&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;14&lt;/span&gt; &lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;15&lt;/span&gt; &lt;span style="color:#000000;"&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;go&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;16&lt;/span&gt; &lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;17&lt;/span&gt; &lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;18&lt;/span&gt; &lt;span style="color:#000000;"&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;USE&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;[&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;ReportServer$SQL1TempDB&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;]&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;19&lt;/span&gt; &lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;20&lt;/span&gt; &lt;span style="color:#000000;"&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;go&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;21&lt;/span&gt; &lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;22&lt;/span&gt; &lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;23&lt;/span&gt; &lt;span style="color:#000000;"&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;if&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;not&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;exists&lt;/span&gt;&lt;span style="color:#000000;"&gt; (&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;select&lt;/span&gt;&lt;span style="color:#000000;"&gt; name &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;from&lt;/span&gt;&lt;span style="color:#000000;"&gt; master.dbo.syslogins &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;where&lt;/span&gt;&lt;span style="color:#000000;"&gt; name &lt;/span&gt;&lt;span style="color:#808080;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; N&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;'&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;RSUser&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;'&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;and&lt;/span&gt;&lt;span style="color:#000000;"&gt; sysadmin &lt;/span&gt;&lt;span style="color:#808080;"&gt;=&lt;/span&gt;&lt;span style="color:#800000;font-weight:bold;"&gt;1&lt;/span&gt;&lt;span style="color:#000000;"&gt;)
&lt;/span&gt;&lt;span style="color:#008080;"&gt;24&lt;/span&gt; &lt;span style="color:#000000;"&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;BEGIN&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;25&lt;/span&gt; &lt;span style="color:#000000;"&gt;    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;if&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;not&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;exists&lt;/span&gt;&lt;span style="color:#000000;"&gt; (&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;select&lt;/span&gt;&lt;span style="color:#000000;"&gt; name &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;from&lt;/span&gt;&lt;span style="color:#000000;"&gt; sysusers &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;where&lt;/span&gt;&lt;span style="color:#000000;"&gt; name &lt;/span&gt;&lt;span style="color:#808080;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; N&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;'&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;RSUser&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;'&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;and&lt;/span&gt;&lt;span style="color:#000000;"&gt; issqluser &lt;/span&gt;&lt;span style="color:#808080;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;font-weight:bold;"&gt;1&lt;/span&gt;&lt;span style="color:#000000;"&gt;)
&lt;/span&gt;&lt;span style="color:#008080;"&gt;26&lt;/span&gt; &lt;span style="color:#000000;"&gt;    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;BEGIN&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;27&lt;/span&gt; &lt;span style="color:#000000;"&gt;        &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;EXEC&lt;/span&gt;&lt;span style="color:#000000;"&gt; sp_grantdbaccess N&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;'&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;RSUser&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;'&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;28&lt;/span&gt; &lt;span style="color:#000000;"&gt;    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;END&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;29&lt;/span&gt; &lt;span style="color:#000000;"&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;END&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;30&lt;/span&gt; &lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;31&lt;/span&gt; &lt;span style="color:#000000;"&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;go&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;32&lt;/span&gt; &lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;33&lt;/span&gt; &lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;34&lt;/span&gt; &lt;span style="color:#000000;"&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;USE&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;[&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;msdb&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;]&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;35&lt;/span&gt; &lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;36&lt;/span&gt; &lt;span style="color:#000000;"&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;go&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;37&lt;/span&gt; &lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;38&lt;/span&gt; &lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;39&lt;/span&gt; &lt;span style="color:#000000;"&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;if&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;not&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;exists&lt;/span&gt;&lt;span style="color:#000000;"&gt; (&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;select&lt;/span&gt;&lt;span style="color:#000000;"&gt; name &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;from&lt;/span&gt;&lt;span style="color:#000000;"&gt; master.dbo.syslogins &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;where&lt;/span&gt;&lt;span style="color:#000000;"&gt; name &lt;/span&gt;&lt;span style="color:#808080;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; N&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;'&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;RSUser&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;'&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;and&lt;/span&gt;&lt;span style="color:#000000;"&gt; sysadmin &lt;/span&gt;&lt;span style="color:#808080;"&gt;=&lt;/span&gt;&lt;span style="color:#800000;font-weight:bold;"&gt;1&lt;/span&gt;&lt;span style="color:#000000;"&gt;)
&lt;/span&gt;&lt;span style="color:#008080;"&gt;40&lt;/span&gt; &lt;span style="color:#000000;"&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;BEGIN&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;41&lt;/span&gt; &lt;span style="color:#000000;"&gt;    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;if&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;not&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;exists&lt;/span&gt;&lt;span style="color:#000000;"&gt; (&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;select&lt;/span&gt;&lt;span style="color:#000000;"&gt; name &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;from&lt;/span&gt;&lt;span style="color:#000000;"&gt; sysusers &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;where&lt;/span&gt;&lt;span style="color:#000000;"&gt; name &lt;/span&gt;&lt;span style="color:#808080;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; N&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;'&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;RSUser&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;'&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;and&lt;/span&gt;&lt;span style="color:#000000;"&gt; issqluser &lt;/span&gt;&lt;span style="color:#808080;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;font-weight:bold;"&gt;1&lt;/span&gt;&lt;span style="color:#000000;"&gt;)
&lt;/span&gt;&lt;span style="color:#008080;"&gt;42&lt;/span&gt; &lt;span style="color:#000000;"&gt;    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;BEGIN&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;43&lt;/span&gt; &lt;span style="color:#000000;"&gt;        &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;EXEC&lt;/span&gt;&lt;span style="color:#000000;"&gt; sp_grantdbaccess N&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;'&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;RSUser&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;'&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;44&lt;/span&gt; &lt;span style="color:#000000;"&gt;    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;END&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;45&lt;/span&gt; &lt;span style="color:#000000;"&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;END&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;46&lt;/span&gt; &lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;47&lt;/span&gt; &lt;span style="color:#000000;"&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;go&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;48&lt;/span&gt; &lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;49&lt;/span&gt; &lt;span style="color:#000000;"&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;USE&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;[&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;master&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;]&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;50&lt;/span&gt; &lt;span style="color:#000000;"&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;go&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;51&lt;/span&gt; &lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;52&lt;/span&gt; &lt;span style="color:#000000;"&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;if&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;not&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;exists&lt;/span&gt;&lt;span style="color:#000000;"&gt; (&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;select&lt;/span&gt;&lt;span style="color:#000000;"&gt; name &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;from&lt;/span&gt;&lt;span style="color:#000000;"&gt; master.dbo.syslogins &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;where&lt;/span&gt;&lt;span style="color:#000000;"&gt; name &lt;/span&gt;&lt;span style="color:#808080;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; N&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;'&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;RSUser&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;'&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;and&lt;/span&gt;&lt;span style="color:#000000;"&gt; sysadmin &lt;/span&gt;&lt;span style="color:#808080;"&gt;=&lt;/span&gt;&lt;span style="color:#800000;font-weight:bold;"&gt;1&lt;/span&gt;&lt;span style="color:#000000;"&gt;)
&lt;/span&gt;&lt;span style="color:#008080;"&gt;53&lt;/span&gt; &lt;span style="color:#000000;"&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;BEGIN&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;54&lt;/span&gt; &lt;span style="color:#000000;"&gt;    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;if&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;not&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;exists&lt;/span&gt;&lt;span style="color:#000000;"&gt; (&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;select&lt;/span&gt;&lt;span style="color:#000000;"&gt; name &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;from&lt;/span&gt;&lt;span style="color:#000000;"&gt; sysusers &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;where&lt;/span&gt;&lt;span style="color:#000000;"&gt; name &lt;/span&gt;&lt;span style="color:#808080;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; N&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;'&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;RSUser&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;'&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;and&lt;/span&gt;&lt;span style="color:#000000;"&gt; issqluser &lt;/span&gt;&lt;span style="color:#808080;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;font-weight:bold;"&gt;1&lt;/span&gt;&lt;span style="color:#000000;"&gt;)
&lt;/span&gt;&lt;span style="color:#008080;"&gt;55&lt;/span&gt; &lt;span style="color:#000000;"&gt;    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;BEGIN&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;56&lt;/span&gt; &lt;span style="color:#000000;"&gt;        &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;EXEC&lt;/span&gt;&lt;span style="color:#000000;"&gt; sp_grantdbaccess N&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;'&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;RSUser&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;'&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;57&lt;/span&gt; &lt;span style="color:#000000;"&gt;    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;END&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;58&lt;/span&gt; &lt;span style="color:#000000;"&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;END&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;59&lt;/span&gt; &lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;60&lt;/span&gt; &lt;span style="color:#000000;"&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;GO&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;61&lt;/span&gt; &lt;span style="color:#000000;"&gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;

&lt;p&gt;Eventually the user is added to the RSExecRole:&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:a01d8ef5-31d0-4388-a822-3575077cbd96" class="wlWriterEditableSmartContent"&gt;&lt;pre style="width:950px;height:499px;background-color:White;overflow:auto;"&gt;&lt;div&gt;&lt;span style="color:#008080;"&gt; 1&lt;/span&gt; &lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt; 2&lt;/span&gt; &lt;span style="color:#000000;"&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;USE&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;[&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;ReportServer$SQL1&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;]&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt; 3&lt;/span&gt; &lt;span style="color:#000000;"&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;go&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt; 4&lt;/span&gt; &lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt; 5&lt;/span&gt; &lt;span style="color:#000000;"&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;if&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;not&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;exists&lt;/span&gt;&lt;span style="color:#000000;"&gt; (&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;select&lt;/span&gt;&lt;span style="color:#000000;"&gt; name &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;from&lt;/span&gt;&lt;span style="color:#000000;"&gt; master.dbo.syslogins &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;where&lt;/span&gt;&lt;span style="color:#000000;"&gt; name &lt;/span&gt;&lt;span style="color:#808080;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; N&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;'&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;RSUser&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;'&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;and&lt;/span&gt;&lt;span style="color:#000000;"&gt; sysadmin &lt;/span&gt;&lt;span style="color:#808080;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;font-weight:bold;"&gt;1&lt;/span&gt;&lt;span style="color:#000000;"&gt;)
&lt;/span&gt;&lt;span style="color:#008080;"&gt; 6&lt;/span&gt; &lt;span style="color:#000000;"&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;BEGIN&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt; 7&lt;/span&gt; &lt;span style="color:#000000;"&gt;    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;EXEC&lt;/span&gt;&lt;span style="color:#000000;"&gt; sp_addrolemember &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;'&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;RSExecRole&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;'&lt;/span&gt;&lt;span style="color:#000000;"&gt;, N&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;'&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;RSUser&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;'&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt; 8&lt;/span&gt; &lt;span style="color:#000000;"&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;END&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt; 9&lt;/span&gt; &lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;10&lt;/span&gt; &lt;span style="color:#000000;"&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;go&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;11&lt;/span&gt; &lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;12&lt;/span&gt; &lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;13&lt;/span&gt; &lt;span style="color:#000000;"&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;USE&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;[&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;msdb&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;]&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;14&lt;/span&gt; &lt;span style="color:#000000;"&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;go&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;15&lt;/span&gt; &lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;16&lt;/span&gt; &lt;span style="color:#000000;"&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;if&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;not&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;exists&lt;/span&gt;&lt;span style="color:#000000;"&gt; (&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;select&lt;/span&gt;&lt;span style="color:#000000;"&gt; name &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;from&lt;/span&gt;&lt;span style="color:#000000;"&gt; master.dbo.syslogins &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;where&lt;/span&gt;&lt;span style="color:#000000;"&gt; name &lt;/span&gt;&lt;span style="color:#808080;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; N&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;'&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;RSUser&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;'&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;and&lt;/span&gt;&lt;span style="color:#000000;"&gt; sysadmin &lt;/span&gt;&lt;span style="color:#808080;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;font-weight:bold;"&gt;1&lt;/span&gt;&lt;span style="color:#000000;"&gt;)
&lt;/span&gt;&lt;span style="color:#008080;"&gt;17&lt;/span&gt; &lt;span style="color:#000000;"&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;BEGIN&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;18&lt;/span&gt; &lt;span style="color:#000000;"&gt;    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;EXEC&lt;/span&gt;&lt;span style="color:#000000;"&gt; sp_addrolemember &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;'&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;RSExecRole&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;'&lt;/span&gt;&lt;span style="color:#000000;"&gt;, N&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;'&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;RSUser&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;'&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;19&lt;/span&gt; &lt;span style="color:#000000;"&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;END&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;20&lt;/span&gt; &lt;span style="color:#000000;"&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;go&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;21&lt;/span&gt; &lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;22&lt;/span&gt; &lt;span style="color:#000000;"&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;USE&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;[&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;master&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;]&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;23&lt;/span&gt; &lt;span style="color:#000000;"&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;go&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;24&lt;/span&gt; &lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;25&lt;/span&gt; &lt;span style="color:#000000;"&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;if&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;not&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;exists&lt;/span&gt;&lt;span style="color:#000000;"&gt; (&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;select&lt;/span&gt;&lt;span style="color:#000000;"&gt; name &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;from&lt;/span&gt;&lt;span style="color:#000000;"&gt; master.dbo.syslogins &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;where&lt;/span&gt;&lt;span style="color:#000000;"&gt; name &lt;/span&gt;&lt;span style="color:#808080;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; N&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;'&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;RSUser&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;'&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;and&lt;/span&gt;&lt;span style="color:#000000;"&gt; sysadmin &lt;/span&gt;&lt;span style="color:#808080;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;font-weight:bold;"&gt;1&lt;/span&gt;&lt;span style="color:#000000;"&gt;)
&lt;/span&gt;&lt;span style="color:#008080;"&gt;26&lt;/span&gt; &lt;span style="color:#000000;"&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;BEGIN&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;27&lt;/span&gt; &lt;span style="color:#000000;"&gt;    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;EXEC&lt;/span&gt;&lt;span style="color:#000000;"&gt; sp_addrolemember &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;'&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;RSExecRole&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;'&lt;/span&gt;&lt;span style="color:#000000;"&gt;, N&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;'&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;RSUser&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;'&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;28&lt;/span&gt; &lt;span style="color:#000000;"&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;END&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;29&lt;/span&gt; &lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;30&lt;/span&gt; &lt;span style="color:#000000;"&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;go&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;31&lt;/span&gt; &lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;32&lt;/span&gt; &lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;33&lt;/span&gt; &lt;span style="color:#000000;"&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;USE&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;[&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;ReportServer$SQL1TempDB&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;]&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;34&lt;/span&gt; &lt;span style="color:#000000;"&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;go&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;35&lt;/span&gt; &lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;36&lt;/span&gt; &lt;span style="color:#000000;"&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;if&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;not&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;exists&lt;/span&gt;&lt;span style="color:#000000;"&gt; (&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;select&lt;/span&gt;&lt;span style="color:#000000;"&gt; name &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;from&lt;/span&gt;&lt;span style="color:#000000;"&gt; master.dbo.syslogins &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;where&lt;/span&gt;&lt;span style="color:#000000;"&gt; name &lt;/span&gt;&lt;span style="color:#808080;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; N&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;'&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;RSUser&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;'&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;and&lt;/span&gt;&lt;span style="color:#000000;"&gt; sysadmin &lt;/span&gt;&lt;span style="color:#808080;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;font-weight:bold;"&gt;1&lt;/span&gt;&lt;span style="color:#000000;"&gt;)
&lt;/span&gt;&lt;span style="color:#008080;"&gt;37&lt;/span&gt; &lt;span style="color:#000000;"&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;BEGIN&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;38&lt;/span&gt; &lt;span style="color:#000000;"&gt;    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;EXEC&lt;/span&gt;&lt;span style="color:#000000;"&gt; sp_addrolemember &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;'&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;RSExecRole&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;'&lt;/span&gt;&lt;span style="color:#000000;"&gt;, N&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;'&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;RSUser&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;'&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;39&lt;/span&gt; &lt;span style="color:#000000;"&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;END&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;40&lt;/span&gt; &lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;41&lt;/span&gt; &lt;span style="color:#000000;"&gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Ha. I was about to give a rant about implementation of &lt;a href="http://msdn.microsoft.com/en-us/library/ms187750.aspx"&gt;sp_addrolemember&lt;/a&gt; system procedure, which in SQL 2005 is using sysusers and other obsolete objects. The version in SQL Server 2008 R2 has been rewritten from what I see, and is using &lt;a href="http://msdn.microsoft.com/en-us/library/ms187328.aspx"&gt;sys.database_principals&lt;/a&gt; in its logic. Well done Microsoft &lt;img style="border-bottom-style:none;border-right-style:none;border-top-style:none;border-left-style:none;" class="wlEmoticon wlEmoticon-smile" alt="Smile" src="http://sqlblog.com/blogs/piotr_rodak/wlEmoticon-smile_1BE712F6.png" /&gt;.&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:f469c59a-8b16-4bca-8a3d-15d5bfd59dc8" class="wlWriterEditableSmartContent"&gt;Technorati Tags: &lt;a href="http://technorati.com/tags/SQL+Server" rel="tag"&gt;SQL Server&lt;/a&gt;,&lt;a href="http://technorati.com/tags/t-sql" rel="tag"&gt;t-sql&lt;/a&gt;,&lt;a href="http://technorati.com/tags/reporting+services" rel="tag"&gt;reporting services&lt;/a&gt;&lt;/div&gt;&lt;img src="http://sqlblog.com/aggbug.aspx?PostID=31080" width="1" height="1"&gt;</description><category domain="http://sqlblog.com/blogs/piotr_rodak/archive/tags/database+design/default.aspx">database design</category><category domain="http://sqlblog.com/blogs/piotr_rodak/archive/tags/SQL+Server+2008+R2/default.aspx">SQL Server 2008 R2</category><category domain="http://sqlblog.com/blogs/piotr_rodak/archive/tags/SSRS/default.aspx">SSRS</category><category domain="http://sqlblog.com/blogs/piotr_rodak/archive/tags/T-SQL/default.aspx">T-SQL</category></item><item><title>database in use–a little trick to disconnect users</title><link>http://sqlblog.com/blogs/piotr_rodak/archive/2010/11/22/database-in-use-a-little-trick-to-disconnect-users.aspx</link><pubDate>Mon, 22 Nov 2010 18:17:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:30878</guid><dc:creator>Piotr Rodak</dc:creator><slash:comments>0</slash:comments><comments>http://sqlblog.com/blogs/piotr_rodak/comments/30878.aspx</comments><wfw:commentRss>http://sqlblog.com/blogs/piotr_rodak/commentrss.aspx?PostID=30878</wfw:commentRss><description>&lt;p&gt;While I am still waiting for my SQL Server 2008 R2 Dev Edition to come, there’s a little trick you can do if you want to drop users from your database and you don’t have sysadmin rights to kill sessions.&lt;/p&gt;  &lt;p&gt;You can set database to single user mode with this command:&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:9edafabf-9039-482f-998b-bea05fdb5dad" class="wlWriterEditableSmartContent"&gt;&lt;pre style="width:709px;height:36px;background-color:White;overflow:auto;"&gt;&lt;div&gt;&lt;span style="color:#008080;"&gt;1&lt;/span&gt; &lt;span style="color:#0000FF;"&gt;alter&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;database&lt;/span&gt;&lt;span style="color:#000000;"&gt; your_database &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;set&lt;/span&gt;&lt;span style="color:#000000;"&gt; single_user &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;with&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;rollback&lt;/span&gt;&lt;span style="color:#000000;"&gt; immediate&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This command disconnects &lt;strong&gt;&lt;font color="#ff0000"&gt;all&lt;/font&gt;&lt;/strong&gt; connected users and rolls back any running transactions. It doesn’t allow you to disconnect particular users, but it may be handy if you are db_owner of the database but don’t have sa right to run KILL command.&lt;/p&gt;

&lt;p&gt;I use this command to &lt;a href="http://sqlblogcasts.com/blogs/piotr_rodak/archive/2009/03/26/generate-create-database-snapshot-script.aspx"&gt;restore database from snapshot&lt;/a&gt; – the restore command requires that there are no active connection to database while it is executed.&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;That’s about it for today, next article will be longer, I promise &lt;img style="border-bottom-style:none;border-right-style:none;border-top-style:none;border-left-style:none;" class="wlEmoticon wlEmoticon-smile" alt="Smile" src="http://sqlblog.com/blogs/piotr_rodak/wlEmoticon-smile_1FBEA131.png" /&gt;.&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:d96c87df-8e44-41f3-a838-738bb6e2cab3" class="wlWriterEditableSmartContent"&gt;Technorati Tags: &lt;a href="http://technorati.com/tags/t-sql" rel="tag"&gt;t-sql&lt;/a&gt;,&lt;a href="http://technorati.com/tags/sql+server" rel="tag"&gt;sql server&lt;/a&gt;&lt;/div&gt;&lt;img src="http://sqlblog.com/aggbug.aspx?PostID=30878" width="1" height="1"&gt;</description><category domain="http://sqlblog.com/blogs/piotr_rodak/archive/tags/tips+and+tricks/default.aspx">tips and tricks</category><category domain="http://sqlblog.com/blogs/piotr_rodak/archive/tags/T-SQL/default.aspx">T-SQL</category></item><item><title>DEFAULT constraints and sp_bindefault</title><link>http://sqlblog.com/blogs/piotr_rodak/archive/2010/09/21/default-constraints-and-sp-bindefault.aspx</link><pubDate>Mon, 20 Sep 2010 22:22:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:28872</guid><dc:creator>Piotr Rodak</dc:creator><slash:comments>3</slash:comments><comments>http://sqlblog.com/blogs/piotr_rodak/comments/28872.aspx</comments><wfw:commentRss>http://sqlblog.com/blogs/piotr_rodak/commentrss.aspx?PostID=28872</wfw:commentRss><description>&lt;p&gt;When you deal with old databases, with origins somewhere around second half of nineteen nineties, you can expect all kinds of oddities, like funny naming conventions, (lack of) referential integrity, absence of clustered indexes and things like rules and defaults created separately from tables DDL.&lt;/p&gt;  &lt;p&gt;I just happen to work with such database. I see many tables which some time ago were migrated from a Sybase system that have their DEFAULT constraints defined with &lt;a href="http://msdn.microsoft.com/en-us/library/ms177503.aspx"&gt;sp_bindefault&lt;/a&gt; stored procedure. You don’t have to memorize exact syntax of the invocation of this procedure because it is marked as obsolete and should not be used anymore:&lt;/p&gt;  &lt;p&gt;&lt;font color="#800000"&gt;This feature will be removed in a future version of Microsoft SQL Server. Do not use this feature in new development work, and modify applications that currently use this feature as soon as possible. We recommend that you create default definitions by using the DEFAULT keyword of the &lt;/font&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/ms190273.aspx"&gt;&lt;font color="#800000"&gt;ALTER TABLE&lt;/font&gt;&lt;/a&gt;&lt;font color="#800000"&gt; or &lt;/font&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/ms174979.aspx"&gt;&lt;font color="#800000"&gt;CREATE TABLE&lt;/font&gt;&lt;/a&gt;&lt;font color="#800000"&gt; statements instead. For more information, see &lt;span&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/ms191289.aspx"&gt;Creating and Modifying DEFAULT Definitions&lt;/a&gt;&lt;/span&gt;.&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;Now, when I read the documentation, the sp_bindefault has some interesting capability. If you &lt;a href="http://msdn.microsoft.com/en-us/library/ms173565.aspx"&gt;create default&lt;/a&gt; and user defined type (alias), and you bind this default to the type with sp_bindefault, you can inadvertently affect all columns of that type in all tables in the database if they don’t have any default or are using existing default of the alias type:&lt;/p&gt;  &lt;div id="codeSnippetWrapper" style="border:1px solid silver;padding:4px;font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;direction:ltr;max-height:200px;line-height:12pt;font-family:'Courier New',courier,monospace;text-align:left;"&gt;   &lt;div id="codeSnippet" style="padding:0px;font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;text-align:left;"&gt;     &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;background-color:white;text-align:left;"&gt;&lt;span id="lnum1"&gt;   1:&lt;/span&gt; &lt;span&gt;create&lt;/span&gt; &lt;span&gt;table&lt;/span&gt; dbo.t1&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;text-align:left;"&gt;&lt;span id="lnum2"&gt;   2:&lt;/span&gt; (&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;background-color:white;text-align:left;"&gt;&lt;span id="lnum3"&gt;   3:&lt;/span&gt; col1 &lt;span&gt;int&lt;/span&gt; &lt;span&gt;not&lt;/span&gt; &lt;span&gt;null&lt;/span&gt;,&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;text-align:left;"&gt;&lt;span id="lnum4"&gt;   4:&lt;/span&gt; col2 &lt;span&gt;int&lt;/span&gt; &lt;span&gt;not&lt;/span&gt; &lt;span&gt;null&lt;/span&gt; &lt;span&gt;constraint&lt;/span&gt; DF_t1col2 &lt;span&gt;default&lt;/span&gt;(0)&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;background-color:white;text-align:left;"&gt;&lt;span id="lnum5"&gt;   5:&lt;/span&gt; )&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;text-align:left;"&gt;&lt;span id="lnum6"&gt;   6:&lt;/span&gt; &lt;span&gt;go&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;background-color:white;text-align:left;"&gt;&lt;span id="lnum7"&gt;   7:&lt;/span&gt; ---&lt;span&gt;create&lt;/span&gt; &lt;span&gt;default&lt;/span&gt; &lt;span&gt;for&lt;/span&gt; the &lt;span&gt;column&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;text-align:left;"&gt;&lt;span id="lnum8"&gt;   8:&lt;/span&gt; &lt;span&gt;create&lt;/span&gt; &lt;span&gt;default&lt;/span&gt; dbo.DFValue_1 &lt;span&gt;as&lt;/span&gt; 1&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;background-color:white;text-align:left;"&gt;&lt;span id="lnum9"&gt;   9:&lt;/span&gt; &lt;span&gt;go&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;text-align:left;"&gt;&lt;span id="lnum10"&gt;  10:&lt;/span&gt; ---bind &lt;span&gt;default&lt;/span&gt; &lt;span&gt;to&lt;/span&gt; the &lt;span&gt;column&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;background-color:white;text-align:left;"&gt;&lt;span id="lnum11"&gt;  11:&lt;/span&gt; &lt;span&gt;exec&lt;/span&gt; sp_bindefault &lt;span&gt;'dbo.DFValue_1'&lt;/span&gt;, &lt;span&gt;'dbo.t1.col1'&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;text-align:left;"&gt;&lt;span id="lnum12"&gt;  12:&lt;/span&gt; &lt;span&gt;go&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;background-color:white;text-align:left;"&gt;&lt;span id="lnum13"&gt;  13:&lt;/span&gt; ---&lt;span&gt;create&lt;/span&gt; udt&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;text-align:left;"&gt;&lt;span id="lnum14"&gt;  14:&lt;/span&gt; &lt;span&gt;create&lt;/span&gt; type ssn&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;background-color:white;text-align:left;"&gt;&lt;span id="lnum15"&gt;  15:&lt;/span&gt; &lt;span&gt;from&lt;/span&gt; &lt;span&gt;varchar&lt;/span&gt;(15) &lt;span&gt;not&lt;/span&gt; &lt;span&gt;null&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;text-align:left;"&gt;&lt;span id="lnum16"&gt;  16:&lt;/span&gt; &lt;span&gt;go&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;background-color:white;text-align:left;"&gt;&lt;span id="lnum17"&gt;  17:&lt;/span&gt; --&lt;span&gt;create&lt;/span&gt; &lt;span&gt;default&lt;/span&gt; - empty string&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;text-align:left;"&gt;&lt;span id="lnum18"&gt;  18:&lt;/span&gt; &lt;span&gt;create&lt;/span&gt; &lt;span&gt;default&lt;/span&gt; dbo.DF_UDTEmpty &lt;span&gt;as&lt;/span&gt; &lt;span&gt;''&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;background-color:white;text-align:left;"&gt;&lt;span id="lnum19"&gt;  19:&lt;/span&gt; &lt;span&gt;go&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;text-align:left;"&gt;&lt;span id="lnum20"&gt;  20:&lt;/span&gt; --bind &lt;span&gt;default&lt;/span&gt; &lt;span&gt;to&lt;/span&gt; the udt&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;background-color:white;text-align:left;"&gt;&lt;span id="lnum21"&gt;  21:&lt;/span&gt; &lt;span&gt;exec&lt;/span&gt; sp_bindefault &lt;span&gt;'dbo.DF_UDTEmpty'&lt;/span&gt;, &lt;span&gt;'dbo.ssn'&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;text-align:left;"&gt;&lt;span id="lnum22"&gt;  22:&lt;/span&gt; &lt;span&gt;go&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;background-color:white;text-align:left;"&gt;&lt;span id="lnum23"&gt;  23:&lt;/span&gt; --&lt;span&gt;add&lt;/span&gt; &lt;span&gt;new&lt;/span&gt; &lt;span&gt;column&lt;/span&gt; &lt;span&gt;to&lt;/span&gt; the &lt;span&gt;table&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;text-align:left;"&gt;&lt;span id="lnum24"&gt;  24:&lt;/span&gt; &lt;span&gt;alter&lt;/span&gt; &lt;span&gt;table&lt;/span&gt; dbo.t1 &lt;span&gt;add&lt;/span&gt; secnumber1 ssn&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;background-color:white;text-align:left;"&gt;&lt;span id="lnum25"&gt;  25:&lt;/span&gt; &lt;span&gt;go&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;text-align:left;"&gt;&lt;span id="lnum26"&gt;  26:&lt;/span&gt; ---insert &lt;span&gt;into&lt;/span&gt; the &lt;span&gt;table&lt;/span&gt; &lt;span&gt;and&lt;/span&gt; &lt;span&gt;select&lt;/span&gt; results&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;background-color:white;text-align:left;"&gt;&lt;span id="lnum27"&gt;  27:&lt;/span&gt; insert dbo.t1(col1, col2, secnumber1) &lt;span&gt;default&lt;/span&gt; &lt;span&gt;values&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;text-align:left;"&gt;&lt;span id="lnum28"&gt;  28:&lt;/span&gt;&amp;nbsp; &lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;background-color:white;text-align:left;"&gt;&lt;span id="lnum29"&gt;  29:&lt;/span&gt; ---secnumber1 &lt;span&gt;is&lt;/span&gt; empty string&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;text-align:left;"&gt;&lt;span id="lnum30"&gt;  30:&lt;/span&gt; &lt;span&gt;select&lt;/span&gt; * &lt;span&gt;from&lt;/span&gt; dbo.t1&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;background-color:white;text-align:left;"&gt;&lt;span id="lnum31"&gt;  31:&lt;/span&gt; &lt;span&gt;go&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;text-align:left;"&gt;&lt;span id="lnum32"&gt;  32:&lt;/span&gt; ---&lt;span&gt;create&lt;/span&gt; &lt;span&gt;second&lt;/span&gt; &lt;span&gt;table&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;background-color:white;text-align:left;"&gt;&lt;span id="lnum33"&gt;  33:&lt;/span&gt; &lt;span&gt;create&lt;/span&gt; &lt;span&gt;table&lt;/span&gt; dbo.t2&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;text-align:left;"&gt;&lt;span id="lnum34"&gt;  34:&lt;/span&gt; (&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;background-color:white;text-align:left;"&gt;&lt;span id="lnum35"&gt;  35:&lt;/span&gt;     col1 &lt;span&gt;int&lt;/span&gt; &lt;span&gt;constraint&lt;/span&gt; DF_t2_col1 &lt;span&gt;default&lt;/span&gt;(10),&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;text-align:left;"&gt;&lt;span id="lnum36"&gt;  36:&lt;/span&gt;     secnumber ssn&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;background-color:white;text-align:left;"&gt;&lt;span id="lnum37"&gt;  37:&lt;/span&gt; )&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;text-align:left;"&gt;&lt;span id="lnum38"&gt;  38:&lt;/span&gt; &lt;span&gt;go&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;background-color:white;text-align:left;"&gt;&lt;span id="lnum39"&gt;  39:&lt;/span&gt; &lt;span&gt;create&lt;/span&gt; &lt;span&gt;default&lt;/span&gt; dbo.DF_SSNNotEmpty &lt;span&gt;as&lt;/span&gt; &lt;span&gt;'&amp;lt;unknown ssn&amp;gt;'&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;text-align:left;"&gt;&lt;span id="lnum40"&gt;  40:&lt;/span&gt; &lt;span&gt;go&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;background-color:white;text-align:left;"&gt;&lt;span id="lnum41"&gt;  41:&lt;/span&gt; ---note here - &lt;span&gt;no&lt;/span&gt; @futureonly param provided&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;text-align:left;"&gt;&lt;span id="lnum42"&gt;  42:&lt;/span&gt; &lt;span&gt;exec&lt;/span&gt; sp_bindefault &lt;span&gt;'dbo.DF_SSNNotEmpty'&lt;/span&gt;, &lt;span&gt;'dbo.ssn'&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;background-color:white;text-align:left;"&gt;&lt;span id="lnum43"&gt;  43:&lt;/span&gt; &lt;span&gt;go&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;text-align:left;"&gt;&lt;span id="lnum44"&gt;  44:&lt;/span&gt; --insert &lt;span&gt;into&lt;/span&gt; t1 &lt;span&gt;and&lt;/span&gt; t2&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;background-color:white;text-align:left;"&gt;&lt;span id="lnum45"&gt;  45:&lt;/span&gt; insert dbo.t1(col1, col2, secnumber1) &lt;span&gt;default&lt;/span&gt; &lt;span&gt;values&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;text-align:left;"&gt;&lt;span id="lnum46"&gt;  46:&lt;/span&gt; insert dbo.t2(col1, secnumber) &lt;span&gt;default&lt;/span&gt; &lt;span&gt;values&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;background-color:white;text-align:left;"&gt;&lt;span id="lnum47"&gt;  47:&lt;/span&gt; &lt;span&gt;go&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;text-align:left;"&gt;&lt;span id="lnum48"&gt;  48:&lt;/span&gt; ---&lt;span&gt;check&lt;/span&gt; contents &lt;span&gt;of&lt;/span&gt; the tables&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;background-color:white;text-align:left;"&gt;&lt;span id="lnum49"&gt;  49:&lt;/span&gt; &lt;span&gt;select&lt;/span&gt; * &lt;span&gt;from&lt;/span&gt; dbo.t1 --what?&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;text-align:left;"&gt;&lt;span id="lnum50"&gt;  50:&lt;/span&gt; &lt;span&gt;select&lt;/span&gt; * &lt;span&gt;from&lt;/span&gt; dbo.t2&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;background-color:white;text-align:left;"&gt;&lt;span id="lnum51"&gt;  51:&lt;/span&gt; &lt;span&gt;go&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;If you run the above script you will see that the default value of the column secnumber1 in table dbo.t1&lt;b&gt; has changed&lt;/b&gt; to&lt;font color="#ff0000" face="Courier New"&gt; ‘&amp;lt;unknown ssn&amp;gt;’&lt;/font&gt;! You can really hurt yourself if you make a mistake and omit the parameter @futureonly in the call to sp_bindefault. If you use @futureonly parameter, the above behavior will not occur. It’s pretty easy to make changes you don’t know about until user calls you at 3 am on Sunday.&lt;/p&gt;

&lt;p&gt;It is good idea to change tables to use standard column DEFAULT constraints. This should be of course made with care, probably only when you change the tables anyway. But, if you were curious how many tables you have in your database that use the DEFAULT objects, you can run this script:&lt;/p&gt;

&lt;div id="codeSnippetWrapper" style="border:1px solid silver;padding:4px;font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;direction:ltr;max-height:200px;line-height:12pt;font-family:'Courier New',courier,monospace;text-align:left;"&gt;
  &lt;div id="codeSnippet" style="padding:0px;font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;text-align:left;"&gt;
    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;background-color:white;text-align:left;"&gt;&lt;span id="lnum1"&gt;   1:&lt;/span&gt; ---&lt;span&gt;all&lt;/span&gt; &lt;span&gt;default&lt;/span&gt; objects &lt;span&gt;with&lt;/span&gt; &lt;span&gt;names&lt;/span&gt; &lt;span&gt;of&lt;/span&gt; tables they &lt;span&gt;are&lt;/span&gt; assigned &lt;span&gt;to&lt;/span&gt;.&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;text-align:left;"&gt;&lt;span id="lnum2"&gt;   2:&lt;/span&gt; &lt;span&gt;select&lt;/span&gt; b.default_object_id, a.name default_object_name, &lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;background-color:white;text-align:left;"&gt;&lt;span id="lnum3"&gt;   3:&lt;/span&gt; b.object_id table_id, object_schema_name(b.object_id) table_schema, &lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;text-align:left;"&gt;&lt;span id="lnum4"&gt;   4:&lt;/span&gt; object_name(b.object_id) table_name, b.name column_name, b.column_id&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;background-color:white;text-align:left;"&gt;&lt;span id="lnum5"&gt;   5:&lt;/span&gt; &lt;span&gt;from&lt;/span&gt; sys.objects a &lt;span&gt;inner&lt;/span&gt; &lt;span&gt;join&lt;/span&gt; sys.all_columns b&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;text-align:left;"&gt;&lt;span id="lnum6"&gt;   6:&lt;/span&gt; &lt;span&gt;on&lt;/span&gt; a.object_id = b.default_object_id&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;background-color:white;text-align:left;"&gt;&lt;span id="lnum7"&gt;   7:&lt;/span&gt; &lt;span&gt;where&lt;/span&gt; 1=1&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;text-align:left;"&gt;&lt;span id="lnum8"&gt;   8:&lt;/span&gt; &lt;span&gt;and&lt;/span&gt; a.object_id &lt;span&gt;not&lt;/span&gt; &lt;span&gt;in&lt;/span&gt; (&lt;span&gt;select&lt;/span&gt; object_id &lt;span&gt;from&lt;/span&gt; sys.default_constraints)&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;Full testing script is attached to the post as usual, enjoy :).&lt;/p&gt;&lt;img src="http://sqlblog.com/aggbug.aspx?PostID=28872" width="1" height="1"&gt;</description><enclosure url="http://sqlblog.com/blogs/piotr_rodak/attachment/28872.ashx" length="2234" type="application/x-zip-compressed" /><category domain="http://sqlblog.com/blogs/piotr_rodak/archive/tags/database+design/default.aspx">database design</category><category domain="http://sqlblog.com/blogs/piotr_rodak/archive/tags/tips+and+tricks/default.aspx">tips and tricks</category><category domain="http://sqlblog.com/blogs/piotr_rodak/archive/tags/T-SQL/default.aspx">T-SQL</category></item><item><title>Partitioned views</title><link>http://sqlblog.com/blogs/piotr_rodak/archive/2010/09/11/partitioned-views.aspx</link><pubDate>Sat, 11 Sep 2010 00:27:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:28689</guid><dc:creator>Piotr Rodak</dc:creator><slash:comments>1</slash:comments><comments>http://sqlblog.com/blogs/piotr_rodak/comments/28689.aspx</comments><wfw:commentRss>http://sqlblog.com/blogs/piotr_rodak/commentrss.aspx?PostID=28689</wfw:commentRss><description>&lt;p&gt;I must say I am a big fan of views. They give flexibility of changing the logic of the data retrieval without affecting DAL layers in your application. I posted series of articles about &lt;a href="http://usepubs.blogspot.com/2010/06/updateable-views-how-to-use-them.html"&gt;updateable views&lt;/a&gt; and &lt;a href="http://usepubs.blogspot.com/2010/06/instead-of-triggers-on-views.html"&gt;triggers on views&lt;/a&gt;, their &lt;a href="http://usepubs.blogspot.com/2010/07/instead-of-triggers-using-views-in-ssis.html"&gt;application in SSIS&lt;/a&gt; for example.&lt;/p&gt;  &lt;p&gt;Recently we finished development of system that loads data from several external sources, makes some calculations on the data and sends data over FTP to downstream systems. I designed the whole data interface to use views and stored procedures. This way, even though we used ORM to perform some of the activities, it was not accessing bare tables. This is good, because you can implement simpler and more flexible security model. The other advantage is that you if you use some simple techniques, you can load data to table and then expose the new data through a view without exposing the logic to the upper layers of your application.&lt;/p&gt;  &lt;p&gt;It is quite common requirement to be able to maintain multiple versions of data within table. The requirement is also that data import should not affect other processes which read current version of data. With views it is really simple to achieve. There are many ways of doing this, but let’s have a look at partitioned views technique.&lt;/p&gt;  &lt;p&gt;Partitioned view is a view that selects data from more than one table. These tables have to follow specific requirements. Amongst the other, there has to be so called partitioning column defined, which has trusted CHECK constraint defined on it. The tables have to be joined by UNION ALL operator. If you want the view to be updateable, the partitioning column has to be part of the primary key of all tables belonging to the view. And you have to select all columns from the table.&lt;/p&gt;  &lt;p&gt;OK, but what is the purpose of creating partitioned views? The short answer is performance. Tables tend to grow bigger and bigger. It becomes more and more expensive to run index updates, backups, and queries against them. If you are able to partition your data and you do it in a right way, you can alleviate some of the issues related to big tables. Classic example would be partitioning by date – you can keep month worth of data in each table and span the view over 12 tables to cover the whole year. Nice thing about partitioned views is that you can index underlying tables in a different way, so for example history tables have more indexes allowing for faster data retrieval, while current month table can be optimized for faster inserts and updates.&lt;/p&gt;  &lt;p&gt;Let’s have a look at this scenario. I will use as an example table Sales.CurrencyRate from AdventureWorks database. This table contains 13532 rows in my database, but let’s imagine we have 135 million rows.&lt;/p&gt;  &lt;p&gt;When I run this query, I get 37 rows spanning years 2001 - 2004:&lt;/p&gt;  &lt;div id="codeSnippetWrapper" style="border:1px solid silver;padding:4px;font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;direction:ltr;max-height:200px;line-height:12pt;font-family:'Courier New',courier,monospace;text-align:left;"&gt;   &lt;div id="codeSnippet" style="padding:0px;font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;text-align:left;"&gt;     &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;background-color:white;text-align:left;"&gt;&lt;span id="lnum1"&gt;   1:&lt;/span&gt; &lt;span&gt;select&lt;/span&gt; &lt;span&gt;count&lt;/span&gt;(*) NumberOfRates, &lt;span&gt;Year&lt;/span&gt;([CurrencyRateDate]) RateYear, &lt;span&gt;Month&lt;/span&gt;([CurrencyRateDate]) RateMonth &lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;text-align:left;"&gt;&lt;span id="lnum2"&gt;   2:&lt;/span&gt; &lt;span&gt;from&lt;/span&gt; Sales.CurrencyRate    &lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;background-color:white;text-align:left;"&gt;&lt;span id="lnum3"&gt;   3:&lt;/span&gt; &lt;span&gt;group&lt;/span&gt; &lt;span&gt;by&lt;/span&gt; &lt;span&gt;Year&lt;/span&gt;([CurrencyRateDate]), &lt;span&gt;Month&lt;/span&gt;([CurrencyRateDate])   &lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;text-align:left;"&gt;&lt;span id="lnum4"&gt;   4:&lt;/span&gt; &lt;span&gt;order&lt;/span&gt; &lt;span&gt;by&lt;/span&gt; RateYear, RateMonth&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;We will create four tables, each for each year that is represented by CurrencyRateDate column.&lt;/p&gt;

&lt;p&gt;A slightly abbreviated DDL for the Sales.CurrencyRate table is as follows:&lt;/p&gt;

&lt;div id="codeSnippetWrapper" style="border:1px solid silver;padding:4px;font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;direction:ltr;max-height:200px;line-height:12pt;font-family:'Courier New',courier,monospace;text-align:left;"&gt;
  &lt;div id="codeSnippet" style="padding:0px;font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;text-align:left;"&gt;
    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;background-color:white;text-align:left;"&gt;&lt;span id="lnum1"&gt;   1:&lt;/span&gt; &lt;span&gt;CREATE&lt;/span&gt; &lt;span&gt;TABLE&lt;/span&gt; [Sales].[CurrencyRate](&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;text-align:left;"&gt;&lt;span id="lnum2"&gt;   2:&lt;/span&gt;     [CurrencyRateID] [&lt;span&gt;int&lt;/span&gt;] &lt;span&gt;IDENTITY&lt;/span&gt;(1,1) &lt;span&gt;NOT&lt;/span&gt; &lt;span&gt;NULL&lt;/span&gt;,&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;background-color:white;text-align:left;"&gt;&lt;span id="lnum3"&gt;   3:&lt;/span&gt;     [CurrencyRateDate] [datetime] &lt;span&gt;NOT&lt;/span&gt; &lt;span&gt;NULL&lt;/span&gt;,&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;text-align:left;"&gt;&lt;span id="lnum4"&gt;   4:&lt;/span&gt;     [FromCurrencyCode] [&lt;span&gt;nchar&lt;/span&gt;](3) &lt;span&gt;COLLATE&lt;/span&gt; Latin1_General_CI_AS &lt;span&gt;NOT&lt;/span&gt; &lt;span&gt;NULL&lt;/span&gt;,&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;background-color:white;text-align:left;"&gt;&lt;span id="lnum5"&gt;   5:&lt;/span&gt;     [ToCurrencyCode] [&lt;span&gt;nchar&lt;/span&gt;](3) &lt;span&gt;COLLATE&lt;/span&gt; Latin1_General_CI_AS &lt;span&gt;NOT&lt;/span&gt; &lt;span&gt;NULL&lt;/span&gt;,&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;text-align:left;"&gt;&lt;span id="lnum6"&gt;   6:&lt;/span&gt;     [AverageRate] [money] &lt;span&gt;NOT&lt;/span&gt; &lt;span&gt;NULL&lt;/span&gt;,&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;background-color:white;text-align:left;"&gt;&lt;span id="lnum7"&gt;   7:&lt;/span&gt;     [EndOfDayRate] [money] &lt;span&gt;NOT&lt;/span&gt; &lt;span&gt;NULL&lt;/span&gt;,&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;text-align:left;"&gt;&lt;span id="lnum8"&gt;   8:&lt;/span&gt;     [ModifiedDate] [datetime] &lt;span&gt;NOT&lt;/span&gt; &lt;span&gt;NULL&lt;/span&gt; &lt;span&gt;CONSTRAINT&lt;/span&gt; [DF_CurrencyRate_ModifiedDate]  &lt;span&gt;DEFAULT&lt;/span&gt; (getdate()),&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;background-color:white;text-align:left;"&gt;&lt;span id="lnum9"&gt;   9:&lt;/span&gt;  &lt;span&gt;CONSTRAINT&lt;/span&gt; [PK_CurrencyRate_CurrencyRateID] &lt;span&gt;PRIMARY&lt;/span&gt; &lt;span&gt;KEY&lt;/span&gt; &lt;span&gt;CLUSTERED&lt;/span&gt; &lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;text-align:left;"&gt;&lt;span id="lnum10"&gt;  10:&lt;/span&gt; (&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;background-color:white;text-align:left;"&gt;&lt;span id="lnum11"&gt;  11:&lt;/span&gt;     [CurrencyRateID] &lt;span&gt;ASC&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;text-align:left;"&gt;&lt;span id="lnum12"&gt;  12:&lt;/span&gt; )&lt;span&gt;WITH&lt;/span&gt; (IGNORE_DUP_KEY = &lt;span&gt;OFF&lt;/span&gt;) &lt;span&gt;ON&lt;/span&gt; [&lt;span&gt;PRIMARY&lt;/span&gt;]&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;background-color:white;text-align:left;"&gt;&lt;span id="lnum13"&gt;  13:&lt;/span&gt; ) &lt;span&gt;ON&lt;/span&gt; [&lt;span&gt;PRIMARY&lt;/span&gt;]&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;If we want to have updateable partitioned view, we need to modify the constraint to include date part, which will allow the execution engine to identify partition that has to be modified during DML operation. The easiest is to add the CurrencyRateDate column to the primary key.&lt;/p&gt;

&lt;p&gt;Let’s create four tables then, one for each year of the data. I will not place here whole DDL for all tables, you can find it in the attached file. This is however the first partition table:&lt;/p&gt;

&lt;div id="codeSnippetWrapper" style="border:1px solid silver;padding:4px;font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;direction:ltr;max-height:200px;line-height:12pt;font-family:'Courier New',courier,monospace;text-align:left;"&gt;
  &lt;div id="codeSnippet" style="padding:0px;font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;text-align:left;"&gt;
    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;background-color:white;text-align:left;"&gt;&lt;span id="lnum1"&gt;   1:&lt;/span&gt; &lt;span&gt;CREATE&lt;/span&gt; &lt;span&gt;TABLE&lt;/span&gt; [Sales].[CurrencyRate_2001](&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;text-align:left;"&gt;&lt;span id="lnum2"&gt;   2:&lt;/span&gt;     [CurrencyRateID] [&lt;span&gt;int&lt;/span&gt;] &lt;span&gt;IDENTITY&lt;/span&gt;(1,1) &lt;span&gt;NOT&lt;/span&gt; &lt;span&gt;NULL&lt;/span&gt;,&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;background-color:white;text-align:left;"&gt;&lt;span id="lnum3"&gt;   3:&lt;/span&gt;     [CurrencyRateDate] [datetime] &lt;span&gt;NOT&lt;/span&gt; &lt;span&gt;NULL&lt;/span&gt;,&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;text-align:left;"&gt;&lt;span id="lnum4"&gt;   4:&lt;/span&gt;     [FromCurrencyCode] [&lt;span&gt;nchar&lt;/span&gt;](3) &lt;span&gt;COLLATE&lt;/span&gt; Latin1_General_CI_AS &lt;span&gt;NOT&lt;/span&gt; &lt;span&gt;NULL&lt;/span&gt;,&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;background-color:white;text-align:left;"&gt;&lt;span id="lnum5"&gt;   5:&lt;/span&gt;     [ToCurrencyCode] [&lt;span&gt;nchar&lt;/span&gt;](3) &lt;span&gt;COLLATE&lt;/span&gt; Latin1_General_CI_AS &lt;span&gt;NOT&lt;/span&gt; &lt;span&gt;NULL&lt;/span&gt;,&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;text-align:left;"&gt;&lt;span id="lnum6"&gt;   6:&lt;/span&gt;     [AverageRate] [money] &lt;span&gt;NOT&lt;/span&gt; &lt;span&gt;NULL&lt;/span&gt;,&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;background-color:white;text-align:left;"&gt;&lt;span id="lnum7"&gt;   7:&lt;/span&gt;     [EndOfDayRate] [money] &lt;span&gt;NOT&lt;/span&gt; &lt;span&gt;NULL&lt;/span&gt;,&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;text-align:left;"&gt;&lt;span id="lnum8"&gt;   8:&lt;/span&gt;     [ModifiedDate] [datetime] &lt;span&gt;NOT&lt;/span&gt; &lt;span&gt;NULL&lt;/span&gt; &lt;span&gt;CONSTRAINT&lt;/span&gt; [DF_CurrencyRate_2001_ModifiedDate]  &lt;span&gt;DEFAULT&lt;/span&gt; (getdate()),&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;background-color:white;text-align:left;"&gt;&lt;span id="lnum9"&gt;   9:&lt;/span&gt;     &lt;span&gt;constraint&lt;/span&gt; CHK_CurrencyRateDate_2001 &lt;span&gt;CHECK&lt;/span&gt; (CurrencyRateDate&amp;gt;= &lt;span&gt;'20010101'&lt;/span&gt; &lt;span&gt;and&lt;/span&gt; CurrencyRateDate &amp;lt; &lt;span&gt;'20020101'&lt;/span&gt;),&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;text-align:left;"&gt;&lt;span id="lnum10"&gt;  10:&lt;/span&gt;  &lt;span&gt;CONSTRAINT&lt;/span&gt; [PK_CurrencyRate_2001_CurrencyRate_2001ID] &lt;span&gt;PRIMARY&lt;/span&gt; &lt;span&gt;KEY&lt;/span&gt; &lt;span&gt;CLUSTERED&lt;/span&gt; &lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;background-color:white;text-align:left;"&gt;&lt;span id="lnum11"&gt;  11:&lt;/span&gt; (&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;text-align:left;"&gt;&lt;span id="lnum12"&gt;  12:&lt;/span&gt;     [CurrencyRateID] &lt;span&gt;ASC&lt;/span&gt;,&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;background-color:white;text-align:left;"&gt;&lt;span id="lnum13"&gt;  13:&lt;/span&gt;     [CurrencyRateDate] &lt;span&gt;ASC&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;text-align:left;"&gt;&lt;span id="lnum14"&gt;  14:&lt;/span&gt; )&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;background-color:white;text-align:left;"&gt;&lt;span id="lnum15"&gt;  15:&lt;/span&gt; ) &lt;span&gt;ON&lt;/span&gt; [&lt;span&gt;PRIMARY&lt;/span&gt;]&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;As you see above, the CurrencyRateDate column has been included into the primary key of the table and I added constraint CHK_CurrencyRateDate_2001 that will take care of the range of dates that can exist in the table. You can use inequality constraint if you’d like to store rows for dates from 2001 or older. Tables for 2002, 2003 and 2004 contain constraints disallowing inserting data outside of the ranges, similar to the the constraint shown above.&lt;/p&gt;

&lt;p&gt;I removed identity column from partition tables to make the view updateable – this is one of requirements.&lt;/p&gt;

&lt;p&gt;The view is defined as follows:&lt;/p&gt;

&lt;div id="codeSnippetWrapper" style="border:1px solid silver;padding:4px;font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;direction:ltr;max-height:200px;line-height:12pt;font-family:'Courier New',courier,monospace;text-align:left;"&gt;
  &lt;div id="codeSnippet" style="padding:0px;font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;text-align:left;"&gt;
    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;background-color:white;text-align:left;"&gt;&lt;span id="lnum1"&gt;   1:&lt;/span&gt; &lt;span&gt;create&lt;/span&gt; &lt;span&gt;view&lt;/span&gt; Sales.vCurrencyRate&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;text-align:left;"&gt;&lt;span id="lnum2"&gt;   2:&lt;/span&gt; &lt;span&gt;as&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;background-color:white;text-align:left;"&gt;&lt;span id="lnum3"&gt;   3:&lt;/span&gt; &lt;span&gt;select&lt;/span&gt; * &lt;span&gt;from&lt;/span&gt; Sales.CurrencyRate_2001&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;text-align:left;"&gt;&lt;span id="lnum4"&gt;   4:&lt;/span&gt; &lt;span&gt;union&lt;/span&gt; &lt;span&gt;all&lt;/span&gt; &lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;background-color:white;text-align:left;"&gt;&lt;span id="lnum5"&gt;   5:&lt;/span&gt; &lt;span&gt;select&lt;/span&gt; * &lt;span&gt;from&lt;/span&gt; Sales.CurrencyRate_2002&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;text-align:left;"&gt;&lt;span id="lnum6"&gt;   6:&lt;/span&gt; &lt;span&gt;union&lt;/span&gt; &lt;span&gt;all&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;background-color:white;text-align:left;"&gt;&lt;span id="lnum7"&gt;   7:&lt;/span&gt; &lt;span&gt;select&lt;/span&gt; * &lt;span&gt;from&lt;/span&gt; Sales.CurrencyRate_2003&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;text-align:left;"&gt;&lt;span id="lnum8"&gt;   8:&lt;/span&gt; &lt;span&gt;union&lt;/span&gt; &lt;span&gt;all&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;background-color:white;text-align:left;"&gt;&lt;span id="lnum9"&gt;   9:&lt;/span&gt; &lt;span&gt;select&lt;/span&gt; * &lt;span&gt;from&lt;/span&gt; Sales.CurrencyRate_2004&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;Let’s insert some data to the view – that is, the underlying partition tables:&lt;/p&gt;

&lt;div id="codeSnippetWrapper" style="border:1px solid silver;padding:4px;font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;direction:ltr;max-height:200px;line-height:12pt;font-family:'Courier New',courier,monospace;text-align:left;"&gt;
  &lt;div id="codeSnippet" style="padding:0px;font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;text-align:left;"&gt;
    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;background-color:white;text-align:left;"&gt;&lt;span id="lnum1"&gt;   1:&lt;/span&gt; insert Sales.vCurrencyRate&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;text-align:left;"&gt;&lt;span id="lnum2"&gt;   2:&lt;/span&gt; (&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;background-color:white;text-align:left;"&gt;&lt;span id="lnum3"&gt;   3:&lt;/span&gt;     [CurrencyRateID],&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;text-align:left;"&gt;&lt;span id="lnum4"&gt;   4:&lt;/span&gt;     [CurrencyRateDate],&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;background-color:white;text-align:left;"&gt;&lt;span id="lnum5"&gt;   5:&lt;/span&gt;     [FromCurrencyCode],&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;text-align:left;"&gt;&lt;span id="lnum6"&gt;   6:&lt;/span&gt;     [ToCurrencyCode],&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;background-color:white;text-align:left;"&gt;&lt;span id="lnum7"&gt;   7:&lt;/span&gt;     [AverageRate],&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;text-align:left;"&gt;&lt;span id="lnum8"&gt;   8:&lt;/span&gt;     [EndOfDayRate],&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;background-color:white;text-align:left;"&gt;&lt;span id="lnum9"&gt;   9:&lt;/span&gt;     [ModifiedDate]&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;text-align:left;"&gt;&lt;span id="lnum10"&gt;  10:&lt;/span&gt; )&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;background-color:white;text-align:left;"&gt;&lt;span id="lnum11"&gt;  11:&lt;/span&gt; &lt;span&gt;select&lt;/span&gt; [CurrencyRateID],&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;text-align:left;"&gt;&lt;span id="lnum12"&gt;  12:&lt;/span&gt;     [CurrencyRateDate],&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;background-color:white;text-align:left;"&gt;&lt;span id="lnum13"&gt;  13:&lt;/span&gt;     [FromCurrencyCode],&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;text-align:left;"&gt;&lt;span id="lnum14"&gt;  14:&lt;/span&gt;     [ToCurrencyCode],&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;background-color:white;text-align:left;"&gt;&lt;span id="lnum15"&gt;  15:&lt;/span&gt;     [AverageRate],&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;text-align:left;"&gt;&lt;span id="lnum16"&gt;  16:&lt;/span&gt;     [EndOfDayRate],&lt;/pre&gt;


    &lt;pre style="padding:0px;font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;direction:ltr;border-style:none;line-height:12pt;font-family:'Courier New',courier,monospace;background-color:white;text-align:left;"&gt;&lt;span id="lnum17"&gt;  17:&lt;/span&gt;     [ModifiedDate] &lt;span&gt;from&lt;/span&gt; Sales.CurrencyRate&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt; This is the execution plan created for the insert:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://sqlblog.com/blogs/piotr_rodak/PVInsert1_031453D3.jpg"&gt;&lt;img src="http://sqlblog.com/blogs/piotr_rodak/PVInsert1_thumb_0DD43BD9.jpg" title="PVInsert1" style="border:0px none;display:inline;" alt="PVInsert1" border="0" height="381" width="826"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt; As you see, database engine split data amongst all partition tables according to the partitioning column constraints. This way, you can easily move data from one table to several smaller partitions. Of course, you wouldn’t want to run simple insert statement as above for 135 million rows, but you get the idea.&lt;/p&gt;

&lt;p&gt;Partitioned views can be very useful, but apart from the conditions that I listed earlier in this article, there are some catches that might bite you and you have to carefully ponder on your design if you want to make them updateable.&lt;/p&gt;

&lt;p&gt;These are some of these gotchas:&lt;/p&gt;

&lt;p&gt;Tables referenced in updateable partitioned views cannot have identity column. If they have, you will get following error message:&lt;/p&gt;

&lt;p&gt;&lt;font color="#ff0000" face="Courier New" size="2"&gt;Msg 4433, Level 16, State 4, Line 3 
    &lt;br&gt;Cannot INSERT into partitioned view 'AdventureWorks.Sales.vCurrencyRate' because table '[AdventureWorks].[Sales].[CurrencyRate_2001]' has an IDENTITY constraint.&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;Tables cannot have computed columns if you want to have updateable partitioned view. If you try to insert rows into view on tables that have such columns, the error message is following:&lt;/p&gt;

&lt;p&gt;&lt;font color="#ff0000" face="Courier New" size="2"&gt;Msg 4406, Level 16, State 1, Line 1 
    &lt;br&gt;Update or insert of view or function 'Sales.vSalesOrderHeader' failed because it contains a derived or constant field.&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;The partitioning column has to be defined using equality or inequality operators. For example, you cannot use datepart(year, …) to defined date range for the column, you have to use either BETWEEN.. AND.. or &amp;gt;=.. &amp;lt; operator. Otherwise the error message will be as follows:&lt;/p&gt;

&lt;p&gt;&lt;font color="#ff0000" face="Courier New" size="2"&gt;Msg 4436, Level 16, State 12, Line 3 
    &lt;br&gt;UNION ALL view 'AdventureWorks.Sales.vCurrencyRate' is not updatable because a partitioning column was not found.&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;br&gt;&lt;/p&gt;

&lt;p&gt;The partitioned views do not have to be updateable. You can design tables with computed columns, IDENTITY columns and still use partitioned views to offload certain queries. One of the scenarios is for example daily upload of data. You can create a staging table which will accept the data and after ETL is finished, you alter view definition to include new table and possibly exclude one with older data.&lt;/p&gt;

&lt;p&gt;I attach the script for your convenience. You can run it on AdventureWorks database or use it as a template for your code.&lt;/p&gt;&lt;img src="http://sqlblog.com/aggbug.aspx?PostID=28689" width="1" height="1"&gt;</description><enclosure url="http://sqlblog.com/blogs/piotr_rodak/attachment/28689.ashx" length="14924" type="application/x-zip-compressed" /><category domain="http://sqlblog.com/blogs/piotr_rodak/archive/tags/database+design/default.aspx">database design</category><category domain="http://sqlblog.com/blogs/piotr_rodak/archive/tags/partitioned+views/default.aspx">partitioned views</category><category domain="http://sqlblog.com/blogs/piotr_rodak/archive/tags/performance/default.aspx">performance</category><category domain="http://sqlblog.com/blogs/piotr_rodak/archive/tags/tips+and+tricks/default.aspx">tips and tricks</category><category domain="http://sqlblog.com/blogs/piotr_rodak/archive/tags/T-SQL/default.aspx">T-SQL</category></item><item><title>Hello</title><link>http://sqlblog.com/blogs/piotr_rodak/archive/2010/08/21/hello.aspx</link><pubDate>Sun, 22 Aug 2010 00:39:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:28124</guid><dc:creator>Piotr Rodak</dc:creator><slash:comments>5</slash:comments><comments>http://sqlblog.com/blogs/piotr_rodak/comments/28124.aspx</comments><wfw:commentRss>http://sqlblog.com/blogs/piotr_rodak/commentrss.aspx?PostID=28124</wfw:commentRss><description>&lt;p&gt;Hello.&lt;br&gt;&lt;/p&gt;  &lt;p&gt;This is my first post on SQLBlog. I have been blogging for a few years now. Initially I used &lt;a href="http://sqlblogcasts.com/blogs/piotr_rodak/" target="_blank"&gt;SQLBlogCasts&lt;/a&gt; platform, but in June I moved my blog to &lt;a href="http://usepubs.blogspot.com/" target="_blank"&gt;Blogspot&lt;/a&gt;. I underestimated though the drop of the visibility of my blog. I fought hard, but the visibility hasn’t increased to appropriate levels – and it is hard to maintain the determination if your work is not useful. Thankfully &lt;a href="http://sqlblog.com/blogs/adam_machanic/" target="_blank"&gt;Adam Machanic&lt;/a&gt; offered me helping hand and the blog on SQLBlog.com. This is a proposition very hard to refuse. I feel honored to be listed on the same page along with the biggest names of the SQL Server world. &lt;/p&gt;  &lt;p&gt;I am going to write mainly about topics related to the design of database, issues I encounter in my work and their solutions. I may write sometimes about SSIS and SSRS, because these products are also in scope of my attention.&lt;/p&gt;  &lt;p&gt;It took a while to set up everything, plus I was on vacations, but eventually I am ready to go.&lt;/p&gt;&lt;img src="http://sqlblog.com/aggbug.aspx?PostID=28124" width="1" height="1"&gt;</description></item></channel></rss>