{"id":43,"date":"2012-10-09T02:10:00","date_gmt":"2012-10-09T02:10:00","guid":{"rendered":"\/lisa\/post\/2012\/10\/09\/Fixing-Orphaned-Users-The-SQL.aspx"},"modified":"2012-10-09T02:10:00","modified_gmt":"2012-10-09T02:10:00","slug":"fixing-orphaned-users-the-sql","status":"publish","type":"post","link":"https:\/\/spacefold.com\/lisa\/2012\/10\/09\/fixing-orphaned-users-the-sql\/","title":{"rendered":"Fixing Orphaned Users: The SQL"},"content":{"rendered":"<p>A very long time ago, C &amp; I were developers consulting to a large corporation with a not-very-large IT department and a not-very-expert SQL Server DBA.<\/p>\n<p class=\"NB\" style=\"padding-left: 30px;\">The work we did for these folks was partially .NET and partially Visual FoxPro, if you care.&nbsp; You shouldn&#8217;t care.&nbsp; What matters is that we were developers of applications, and we had to partner with a DBA for database stuff.&nbsp; As probably <strong>does <\/strong>happen, in your world too.<\/p>\n<p>Every time this DBA restored a .BAK file from one server to another, we had to go to him and request that he fix the SQL identities in the databases that were used by our applications&#8217; connections.<\/p>\n<p>Every time, this DBA would roll his eyes, apologize, and tediously go into the SQL Mgmt Studio dialogs to drop and re-add the users, who did exist on the server but had been disconnected from their database contexts by the restore from a &#8220;foreign&#8221; server.<\/p>\n<h2>There&#8217;s a better way.&nbsp; Actually two.<\/h2>\n<p>It <strong>was<\/strong> a very long time ago, he <strong>was<\/strong> a rather under-informed DBA,&nbsp;and I had no idea whether anybody else didn&#8217;t know that there was a better way.&nbsp; So, <a title=\"blog post including this question to y'all\" href=\"\/lisa\/2012\/08\/19\/Or-we-could-talk-some-more\/\">recently<\/a>, I wrote in this blog asking whether it was a concern to anybody.&nbsp; Nobody wrote to ask,&nbsp;and I thought it was too obvious to write about.&nbsp; End of subject.<\/p>\n<p>But then, even more recently, I encountered a document specifying exactly the same process that not-very-expert DBA used, complete with copious screen shots.&nbsp; Sigh.<\/p>\n<p>So, I&#8217;m going to quickly tell you how I do this.&nbsp; Just in case there is anybody else.<\/p>\n<h2>When you&#8217;re restoring interactively<\/h2>\n<p>Execute the following system procedure against any database:&nbsp;<\/p>\n<p class=\"code\"><span style=\"color: #0000ff; font-size: medium;\"><span style=\"color: #0000ff; font-size: medium;\"><span style=\"color: #0000ff; font-size: medium;\"><span style=\"color: #800000; font-size: medium;\"><span style=\"color: #800000; font-size: medium;\">sp_change_users_login <\/span><\/span><span style=\"font-size: medium;\">@Action<\/span><span style=\"color: #808080; font-size: medium;\"><span style=\"color: #808080; font-size: medium;\">=<\/span><\/span><span style=\"color: #ff0000; font-size: medium;\"><span style=\"color: #ff0000; font-size: medium;\">&#8216;Report&#8217;<\/span><\/span><\/span><\/span><\/span><\/p>\n<p>&#8230; you will find that it gives you a list of orphaned users.<\/p>\n<p>Upon examining that procedure you will find the code on which I based the following statement:<\/p>\n<p class=\"code\"><span style=\"color: #0000ff; font-size: medium;\"><span style=\"color: #0000ff; font-size: medium;\">select<\/span><\/span><span style=\"font-size: medium;\"> UserName <\/span><span style=\"color: #808080; font-size: medium;\"><span style=\"color: #808080; font-size: medium;\">=<\/span><\/span><span style=\"font-size: medium;\"> name<\/span><span style=\"color: #808080; font-size: medium;\"><span style=\"color: #808080; font-size: medium;\">,<\/span><\/span><span style=\"font-size: medium;\"> UserSID <\/span><span style=\"color: #808080; font-size: medium;\"><span style=\"color: #808080; font-size: medium;\">=<\/span><\/span><span style=\"color: #0000ff; font-size: medium;\"><span style=\"color: #0000ff; font-size: medium;\">sid<\/span><\/span><span style=\"color: #808080; font-size: medium;\"><span style=\"color: #808080; font-size: medium;\">,<\/span><\/span><span style=\"font-size: medium;\"> <br \/><\/span><span style=\"color: #ff0000; font-size: medium;\"><span style=\"color: #ff0000; font-size: medium;\">&nbsp;&nbsp; &#8216;ALTER USER [&#8216;<\/span><\/span><span style=\"color: #808080; font-size: medium;\"><span style=\"color: #808080; font-size: medium;\">+<\/span><\/span><span style=\"font-size: medium;\"> name <\/span><span style=\"color: #808080; font-size: medium;\"><span style=\"color: #808080; font-size: medium;\">+<\/span><\/span><span style=\"color: #ff0000; font-size: medium;\"><span style=\"color: #ff0000; font-size: medium;\">&#8216;] WITH LOGIN = [&#8216;<\/span><\/span><span style=\"color: #808080; font-size: medium;\"><span style=\"color: #808080; font-size: medium;\">+<\/span><\/span><span style=\"font-size: medium;\"> name <\/span><span style=\"color: #808080; font-size: medium;\"><span style=\"color: #808080; font-size: medium;\">+<\/span><\/span><span style=\"color: #ff0000; font-size: medium;\"><span style=\"color: #ff0000; font-size: medium;\">&#8216;] ;&nbsp;&#8216;<br \/><\/span><\/span><span style=\"color: #0000ff; font-size: medium;\"><span style=\"color: #0000ff; font-size: medium;\">&nbsp;&nbsp; as<\/span><\/span><span style=\"font-size: medium;\"> FixUserStatement <br \/><\/span><span style=\"color: #0000ff; font-size: medium;\"><span style=\"color: #0000ff; font-size: medium;\">&nbsp;&nbsp; from <\/span><\/span><span style=\"color: #008000; font-size: medium;\"><span style=\"color: #008000; font-size: medium;\">sysusers<br \/><\/span><\/span><span style=\"color: #0000ff; font-size: medium;\"><span style=\"color: #0000ff; font-size: medium;\">&nbsp;&nbsp; where<\/span><\/span><span style=\"font-size: medium;\"> issqluser <\/span><span style=\"color: #808080; font-size: medium;\"><span style=\"color: #808080; font-size: medium;\">=<\/span><\/span><span style=\"font-size: medium;\"> 1 <br \/><\/span><span style=\"color: #808080; font-size: medium;\"><span style=\"color: #808080; font-size: medium;\">&nbsp;&nbsp; and <\/span><\/span><span style=\"color: #808080; font-size: medium;\"><span style=\"color: #808080; font-size: medium;\">(<\/span><\/span><span style=\"color: #0000ff; font-size: medium;\"><span style=\"color: #0000ff; font-size: medium;\">sid <\/span><\/span><span style=\"color: #808080; font-size: medium;\"><span style=\"color: #808080; font-size: medium;\">is <\/span><\/span><span style=\"color: #808080; font-size: medium;\"><span style=\"color: #808080; font-size: medium;\">not <\/span><\/span><span style=\"color: #808080; font-size: medium;\"><span style=\"color: #808080; font-size: medium;\">null <\/span><\/span><span style=\"color: #808080; font-size: medium;\"><span style=\"color: #808080; font-size: medium;\">and <\/span><\/span><span style=\"color: #0000ff; font-size: medium;\"><span style=\"color: #0000ff; font-size: medium;\">sid<\/span><\/span><span style=\"color: #808080; font-size: medium;\"><span style=\"color: #808080; font-size: medium;\">&lt;&gt;<\/span><\/span><span style=\"font-size: medium;\"> 0x0<\/span><span style=\"color: #808080; font-size: medium;\"><span style=\"color: #808080; font-size: medium;\">)<br \/><\/span><\/span><span style=\"color: #808080; font-size: medium;\"><span style=\"color: #808080; font-size: medium;\">&nbsp;&nbsp; and <\/span><\/span><span style=\"color: #808080; font-size: medium;\"><span style=\"color: #808080; font-size: medium;\">(<\/span><\/span><span style=\"color: #ff00ff; font-size: medium;\"><span style=\"color: #ff00ff; font-size: medium;\">len<\/span><\/span><span style=\"color: #808080; font-size: medium;\"><span style=\"color: #808080; font-size: medium;\">(<\/span><\/span><span style=\"color: #0000ff; font-size: medium;\"><span style=\"color: #0000ff; font-size: medium;\">sid<\/span><\/span><span style=\"color: #808080; font-size: medium;\"><span style=\"color: #808080; font-size: medium;\">)<\/span><\/span><span style=\"color: #808080; font-size: medium;\"><span style=\"color: #808080; font-size: medium;\">&lt;=<\/span><\/span><span style=\"font-size: medium;\"> 16<\/span><span style=\"color: #808080; font-size: medium;\"><span style=\"color: #808080; font-size: medium;\">)<br \/><\/span><\/span><span style=\"color: #808080; font-size: medium;\"><span style=\"color: #808080; font-size: medium;\">&nbsp;&nbsp; and <\/span><\/span><span style=\"color: #ff00ff; font-size: medium;\"><span style=\"color: #ff00ff; font-size: medium;\">suser_sname<\/span><\/span><span style=\"color: #808080; font-size: medium;\"><span style=\"color: #808080; font-size: medium;\">(<\/span><\/span><span style=\"color: #0000ff; font-size: medium;\"><span style=\"color: #0000ff; font-size: medium;\">sid<\/span><\/span><span style=\"color: #808080; font-size: medium;\"><span style=\"color: #808080; font-size: medium;\">) <\/span><\/span><span style=\"color: #808080; font-size: medium;\"><span style=\"color: #808080; font-size: medium;\">is <\/span><\/span><span style=\"color: #808080; font-size: medium;\"><span style=\"color: #808080; font-size: medium;\">null<br \/><\/span><\/span><span style=\"color: #0000ff; font-size: medium;\"><span style=\"color: #0000ff; font-size: medium;\">order <\/span><\/span><span style=\"color: #0000ff; font-size: medium;\"><span style=\"color: #0000ff; font-size: medium;\">by<\/span><\/span><span style=\"font-size: medium;\"> name<\/span><\/p>\n<p>&nbsp;&#8230; which, when you run, you will find provides a list of ALTER USER statements.&nbsp;<\/p>\n<p>If you&#8217;re running the&nbsp;SELECT to a grid, select the FixUserStatement column, copy and paste the statements into another query window, execute what you&#8217;ve pasted, and you&#8217;re done.&nbsp;<\/p>\n<p>If there are a lot of such identities, you&#8217;ve saved yourself at least a tedious half&nbsp;hour over using the dialogs to drop and re-add.<\/p>\n<h2>From an SSIS package<\/h2>\n<p>What if you are doing the restore from an SSIS package as I was, in the environment of that suggested&nbsp;<a title=\"same blog post as mentioned earlier\" href=\"\/lisa\/2012\/08\/19\/Or-we-could-talk-some-more\/\">that earlier post<\/a>?&nbsp; In that case, I didn&#8217;t even want to fix <strong>all<\/strong> orphaned users indiscriminately. I wanted to stipulate which users got de-orphaned.<\/p>\n<p>This is&nbsp;also pretty simple to do.&nbsp; All it takes is a configurable package value with a delimited list of SQL identities you want fixed.&nbsp; (If it&#8217;s ever all of them, you can derive the list of users within the package using&nbsp;the code you saw above, and put a &#8220;*&#8221; in the package value to let your&nbsp;package know.&nbsp; If it&#8217;s <strong>always<\/strong> all of them, in your environment, don&#8217;t even bother with the package value.)<\/p>\n<p>Write some VB or C# script in a script task to generate the ALTER USER statements based on the content of&nbsp;this variable, put the resulting statements in another package variable, execute the script in that value in a sql task later in the flow.&nbsp;<\/p>\n<p>Here&#8217;s my code for it, although I don&#8217;t think it&#8217;s any great shakes, in case you find it handy:<\/p>\n<p class=\"code\"><span style=\"color: #008000;\">&#8216; This function is one of many that set up script statements<br \/><\/span><span style=\"color: #008000;\">&#8216; in ReadWrite variables, during the initial steps of&nbsp;my package.<br \/><\/span><span style=\"color: #008000;\">&#8216; The configuration string SQLUserLoginsToFix&nbsp;variable <br \/>&#8216; (holding the users we want to re-enable, configured outside the pkg)<br \/><\/span><span style=\"color: #008000;\">&#8216; has a list that can be pipe (|), comma, or semi-colon-delimited,<br \/><\/span><span style=\"color: #008000;\">&#8216; per our convention.&nbsp; So there&#8217;s a little extra work for that here.<\/span><\/p>\n<p><span style=\"font-size: medium;\"><span style=\"color: #0000ff; font-size: x-small;\"><span style=\"color: #0000ff; font-size: x-small;\">Private <\/span><\/span><span style=\"color: #0000ff; font-size: x-small;\"><span style=\"color: #0000ff; font-size: x-small;\">Function<\/span><\/span><span style=\"font-size: x-small;\"> ConstructFixLoginsSQL(<\/span><span style=\"font-size: x-small;\">) <\/span><span style=\"color: #0000ff; font-size: x-small;\"><span style=\"color: #0000ff; font-size: x-small;\">As <\/span><\/span><span style=\"color: #0000ff; font-size: x-small;\"><span style=\"color: #0000ff; font-size: x-small;\">String<br \/><\/span><\/span><span style=\"color: #0000ff; font-size: x-small;\"><span style=\"color: #0000ff; font-size: x-small;\">&nbsp;&nbsp; Dim<\/span><\/span><span style=\"font-size: x-small;\"> s <\/span><span style=\"color: #0000ff; font-size: x-small;\"><span style=\"color: #0000ff; font-size: x-small;\">As<\/span><\/span><span style=\"font-size: x-small;\"> System.Text.StringBuilder = <\/span><span style=\"color: #0000ff; font-size: x-small;\"><span style=\"color: #0000ff; font-size: x-small;\">New<\/span><\/span><span style=\"font-size: x-small;\"> System.Text.StringBuilder()<br \/><\/span><span style=\"color: #0000ff; font-size: x-small;\"><span style=\"color: #0000ff; font-size: x-small;\">&nbsp;&nbsp; Dim<\/span><\/span><span style=\"font-size: x-small;\"> l <\/span><span style=\"color: #0000ff; font-size: x-small;\"><span style=\"color: #0000ff; font-size: x-small;\">As <\/span><\/span><span style=\"color: #0000ff; font-size: x-small;\"><span style=\"color: #0000ff; font-size: x-small;\">String<\/span><\/span><span style=\"font-size: x-small;\">, d <\/span><span style=\"color: #0000ff; font-size: x-small;\"><span style=\"color: #0000ff; font-size: x-small;\">As <\/span><\/span><span style=\"color: #0000ff; font-size: x-small;\"><span style=\"color: #0000ff; font-size: x-small;\">String<\/span><\/span><span style=\"font-size: x-small;\">() = {<\/span><span style=\"color: #a31515; font-size: x-small;\"><span style=\"color: #a31515; font-size: x-small;\">&#8220;,&#8221;<\/span><\/span><span style=\"font-size: x-small;\">}, u <\/span><span style=\"color: #0000ff; font-size: x-small;\"><span style=\"color: #0000ff; font-size: x-small;\">As <\/span><\/span><span style=\"color: #0000ff; font-size: x-small;\"><span style=\"color: #0000ff; font-size: x-small;\">String<\/span><\/span><span style=\"font-size: x-small;\">, tu <\/span><span style=\"color: #0000ff; font-size: x-small;\"><span style=\"color: #0000ff; font-size: x-small;\">As <\/span><\/span><span style=\"color: #0000ff; font-size: x-small;\"><span style=\"color: #0000ff; font-size: x-small;\">String<br \/><\/span><\/span><span style=\"color: #0000ff; font-size: x-small;\"><span style=\"color: #0000ff; font-size: x-small;\">&nbsp;&nbsp; Dim<\/span><\/span><span style=\"font-size: x-small;\"> n <\/span><span style=\"color: #0000ff; font-size: x-small;\"><span style=\"color: #0000ff; font-size: x-small;\">As <\/span><\/span><span style=\"color: #0000ff; font-size: x-small;\"><span style=\"color: #0000ff; font-size: x-small;\">String<br \/><\/span><\/span><span style=\"font-size: x-small;\">&nbsp;&nbsp; n = ReadWriteVariable(<\/span><span style=\"color: #a31515; font-size: x-small;\"><span style=\"color: #a31515; font-size: x-small;\">&#8220;TargetDbName&#8221;<\/span><\/span><span style=\"font-size: x-small;\">, <\/span><span style=\"color: #0000ff; font-size: x-small;\"><span style=\"color: #0000ff; font-size: x-small;\">Nothing<\/span><\/span><span style=\"font-size: x-small;\">)<br \/><\/span><span style=\"font-size: x-small;\">&nbsp;&nbsp; l = Trim(ReadWriteVariable(<\/span><span style=\"color: #a31515; font-size: x-small;\"><span style=\"color: #a31515; font-size: x-small;\">&#8220;SQLUserLoginsToFix&#8221;<\/span><\/span><span style=\"font-size: x-small;\">, <\/span><span style=\"color: #0000ff; font-size: x-small;\"><span style=\"color: #0000ff; font-size: x-small;\">Nothing<\/span><\/span><span style=\"font-size: x-small;\">))<br \/><\/span><span style=\"color: #0000ff; font-size: x-small;\"><span style=\"color: #0000ff; font-size: x-small;\">&nbsp;&nbsp; If<\/span><\/span><span style=\"font-size: x-small;\"> l.Length &gt; 0 <\/span><span style=\"color: #0000ff; font-size: x-small;\"><span style=\"color: #0000ff; font-size: x-small;\">Then<br \/><\/span><\/span><span style=\"font-size: x-small;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; l = l.Replace(<\/span><span style=\"color: #a31515; font-size: x-small;\"><span style=\"color: #a31515; font-size: x-small;\">&#8220;;&#8221;<\/span><\/span><span style=\"font-size: x-small;\">, <\/span><span style=\"color: #a31515; font-size: x-small;\"><span style=\"color: #a31515; font-size: x-small;\">&#8220;,&#8221;<\/span><\/span><span style=\"font-size: x-small;\">).Replace(<\/span><span style=\"color: #a31515; font-size: x-small;\"><span style=\"color: #a31515; font-size: x-small;\">&#8220;|&#8221;<\/span><\/span><span style=\"font-size: x-small;\">, <\/span><span style=\"color: #a31515; font-size: x-small;\"><span style=\"color: #a31515; font-size: x-small;\">&#8220;,&#8221;<\/span><\/span><span style=\"font-size: x-small;\">) <\/span><span style=\"color: #008000; font-size: x-small;\"><span style=\"color: #008000; font-size: x-small;\">&#8216; allow different separators<br \/><\/span><\/span><span style=\"color: #0000ff; font-size: x-small;\"><span style=\"color: #0000ff; font-size: x-small;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; For <\/span><\/span><span style=\"color: #0000ff; font-size: x-small;\"><span style=\"color: #0000ff; font-size: x-small;\">Each<\/span><\/span><span style=\"font-size: x-small;\"> u <\/span><span style=\"color: #0000ff; font-size: x-small;\"><span style=\"color: #0000ff; font-size: x-small;\">In<\/span><\/span><span style=\"font-size: x-small;\"> l.Split(d, System.StringSplitOptions.RemoveEmptyEntries)<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; tu = Trim(u) <\/span><span style=\"color: #008000; font-size: x-small;\"><span style=\"color: #008000; font-size: x-small;\">&#8216; Just in case<br \/><\/span><\/span><span style=\"color: #0000ff; font-size: x-small;\"><span style=\"color: #0000ff; font-size: x-small;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; If<\/span><\/span><span style=\"font-size: x-small;\"> Len(tu) &gt; 0 <\/span><span style=\"color: #0000ff; font-size: x-small;\"><span style=\"color: #0000ff; font-size: x-small;\">Then<br \/><\/span><\/span><span style=\"font-size: x-small;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; s.Append(<\/span><span style=\"color: #a31515; font-size: x-small;\"><span style=\"color: #a31515; font-size: x-small;\">&#8220;ALTER USER [&#8220;<\/span><\/span><span style=\"font-size: x-small;\"> &amp; tu &amp; <\/span><span style=\"color: #a31515; font-size: x-small;\"><span style=\"color: #a31515; font-size: x-small;\">&#8220;] WITH LOGIN = &#8220;<\/span><\/span><span style=\"font-size: x-small;\">)<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; s.Append(<\/span><span style=\"color: #a31515; font-size: x-small;\"><span style=\"color: #a31515; font-size: x-small;\">&#8220;[&#8220;<\/span><\/span><span style=\"font-size: x-small;\"> &amp; tu &amp; <\/span><span style=\"color: #a31515; font-size: x-small;\"><span style=\"color: #a31515; font-size: x-small;\">&#8220;] ;&#8221;<\/span><\/span><span style=\"font-size: x-small;\">)<br \/><\/span><span style=\"color: #0000ff; font-size: x-small;\"><span style=\"color: #0000ff; font-size: x-small;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End <\/span><\/span><span style=\"color: #0000ff; font-size: x-small;\"><span style=\"color: #0000ff; font-size: x-small;\">If<br \/><\/span><\/span><span style=\"color: #0000ff; font-size: x-small;\"><span style=\"color: #0000ff; font-size: x-small;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Next<br \/><\/span><\/span><span style=\"color: #0000ff; font-size: x-small;\"><span style=\"color: #0000ff; font-size: x-small;\">&nbsp;&nbsp; End <\/span><\/span><span style=\"color: #0000ff; font-size: x-small;\"><span style=\"color: #0000ff; font-size: x-small;\">If<br \/><\/span><\/span><span style=\"color: #0000ff; font-size: x-small;\"><span style=\"color: #0000ff; font-size: x-small;\">&nbsp;&nbsp; If<\/span><\/span><span style=\"font-size: x-small;\"> s.Length = 0 <\/span><span style=\"color: #0000ff; font-size: x-small;\"><span style=\"color: #0000ff; font-size: x-small;\">Then<br \/><\/span><\/span><span style=\"font-size: x-small;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; s.Append(<\/span><span style=\"color: #a31515; font-size: x-small;\"><span style=\"color: #a31515; font-size: x-small;\">&#8220;SELECT 1 AS Dummy;&#8221;<\/span><\/span><span style=\"font-size: x-small;\">)<br \/><\/span><span style=\"color: #0000ff; font-size: x-small;\"><span style=\"color: #0000ff; font-size: x-small;\">&nbsp;&nbsp; End <\/span><\/span><span style=\"color: #0000ff; font-size: x-small;\"><span style=\"color: #0000ff; font-size: x-small;\">If<br \/><\/span><\/span><span style=\"color: #0000ff; font-size: x-small;\"><span style=\"color: #0000ff; font-size: x-small;\">&nbsp;&nbsp; Return<\/span><\/span><span style=\"font-size: x-small;\"> s.ToString() <span style=\"color: #008000; font-size: x-small;\"><span style=\"color: #008000; font-size: x-small;\">&#8216; this result is placed in a variable during this script setup task, <br \/>&nbsp;&nbsp; &#8216; and it is later executed in a SQL task after the database backup has been <br \/><\/span><\/span><\/span><span style=\"font-size: x-small;\"><span style=\"color: #008000; font-size: x-small;\"><span style=\"color: #008000; font-size: x-small;\">&nbsp;&nbsp; &#8216; successfully copied to the target server and restored there.<br \/><\/span><\/span><\/span><span style=\"color: #0000ff; font-size: x-small;\"><span style=\"color: #0000ff; font-size: x-small;\">End <\/span><\/span><span style=\"color: #0000ff; font-size: x-small;\"><span style=\"color: #0000ff; font-size: x-small;\">Function<\/p>\n<p><\/span><\/span><span style=\"color: #0000ff; font-size: x-small;\"><span style=\"color: #0000ff; font-size: x-small;\"><span style=\"color: #008000;\">&#8216; My function to handle the variable writing is as follows, although<br \/>&#8216; it&#8217;s nothing special and not germane: <br \/><\/span><\/span><\/span><span style=\"color: #0000ff; font-size: x-small;\"><span style=\"color: #0000ff; font-size: x-small;\"><span style=\"color: #0000ff; font-size: x-small;\"><span style=\"color: #0000ff; font-size: x-small;\"><span style=\"color: #0000ff; font-size: x-small;\"><span style=\"color: #0000ff; font-size: x-small;\">Private <\/span><\/span><span style=\"color: #0000ff; font-size: x-small;\"><span style=\"color: #0000ff; font-size: x-small;\">Function<\/span><\/span><span style=\"font-size: x-small;\"> <span style=\"color: #000000;\">ReadWriteVariable<\/span>(<\/span><span style=\"color: #0000ff; font-size: x-small;\"><span style=\"color: #0000ff; font-size: x-small;\">ByVal<\/span><\/span><span style=\"font-size: x-small;\"> <span style=\"color: #000000;\">varName<\/span> <\/span><span style=\"color: #0000ff; font-size: x-small;\"><span style=\"color: #0000ff; font-size: x-small;\">As <\/span><\/span><span style=\"color: #0000ff; font-size: x-small;\"><span style=\"color: #0000ff; font-size: x-small;\">String<\/span><\/span><span style=\"font-size: x-small;\">, <\/span><span style=\"color: #0000ff; font-size: x-small;\"><span style=\"color: #0000ff; font-size: x-small;\">ByVal<\/span><\/span><span style=\"font-size: x-small;\"> <span style=\"color: #000000;\">value<\/span> <\/span><span style=\"color: #0000ff; font-size: x-small;\"><span style=\"color: #0000ff; font-size: x-small;\">As <\/span><\/span><span style=\"color: #0000ff; font-size: x-small;\"><span style=\"color: #0000ff; font-size: x-small;\">Object<\/span><\/span><span style=\"font-size: x-small;\">) <\/span><span style=\"color: #0000ff; font-size: x-small;\"><span style=\"color: #0000ff; font-size: x-small;\">As <\/span><\/span><span style=\"color: #0000ff; font-size: x-small;\"><span style=\"color: #0000ff; font-size: x-small;\">Object<br \/><\/span><\/span><span style=\"color: #0000ff; font-size: x-small;\"><span style=\"color: #0000ff; font-size: x-small;\">&nbsp;&nbsp; Dim<\/span><\/span><span style=\"font-size: x-small;\"> <span style=\"color: #000000;\">rtnValue<\/span> <\/span><span style=\"color: #0000ff; font-size: x-small;\"><span style=\"color: #0000ff; font-size: x-small;\">As <\/span><\/span><span style=\"color: #0000ff; font-size: x-small;\"><span style=\"color: #0000ff; font-size: x-small;\">Object<\/span><\/span><span style=\"font-size: x-small;\"> = <\/span><span style=\"color: #0000ff; font-size: x-small;\"><span style=\"color: #0000ff; font-size: x-small;\">Nothing<br \/><\/span><\/span><span style=\"color: #008000; font-size: x-small;\"><span style=\"color: #008000; font-size: x-small;\">&nbsp;&nbsp; &#8216;Create a variables collection<br \/><\/span><\/span><span style=\"color: #0000ff; font-size: x-small;\"><span style=\"color: #0000ff; font-size: x-small;\">&nbsp;&nbsp; Dim<\/span><\/span><span style=\"font-size: x-small;\"> <span style=\"color: #000000;\">var<\/span> <\/span><span style=\"color: #0000ff; font-size: x-small;\"><span style=\"color: #0000ff; font-size: x-small;\">As<\/span><\/span><span style=\"font-size: x-small;\"> <span style=\"color: #000000;\">Variables<\/span> = <\/span><span style=\"color: #0000ff; font-size: x-small;\"><span style=\"color: #0000ff; font-size: x-small;\">Nothing<br \/><\/span><\/span><span style=\"color: #0000ff; font-size: x-small;\"><span style=\"color: #0000ff; font-size: x-small;\">&nbsp;&nbsp; Try<br \/><\/span><\/span><span style=\"color: #0000ff; font-size: x-small;\"><span style=\"color: #0000ff; font-size: x-small;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; If<\/span><\/span><span style=\"font-size: x-small;\"> <span style=\"color: #000000;\">value<\/span> <\/span><span style=\"color: #0000ff; font-size: x-small;\"><span style=\"color: #0000ff; font-size: x-small;\">Is <\/span><\/span><span style=\"color: #0000ff; font-size: x-small;\"><span style=\"color: #0000ff; font-size: x-small;\">Nothing <\/span><\/span><span style=\"color: #0000ff; font-size: x-small;\"><span style=\"color: #0000ff; font-size: x-small;\">Then<br \/><\/span><\/span><span style=\"font-size: x-small;\"><span style=\"color: #000000;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dts.VariableDispenser.LockOneForRead(varName, var)<br \/><\/span><span style=\"color: #000000;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; rtnValue = var(varName).Value <br \/><\/span><\/span><span style=\"color: #0000ff; font-size: x-small;\"><span style=\"color: #0000ff; font-size: x-small;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Else<br \/><\/span><\/span><span style=\"font-size: x-small;\"><span style=\"color: #000000;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dts.VariableDispenser.LockOneForWrite(varName, var)<br \/><\/span><span style=\"color: #000000;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var(varName).Value = value<br \/><\/span><\/span><span style=\"color: #0000ff; font-size: x-small;\"><span style=\"color: #0000ff; font-size: x-small;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End <\/span><\/span><span style=\"color: #0000ff; font-size: x-small;\"><span style=\"color: #0000ff; font-size: x-small;\">If<br \/><\/span><\/span><span style=\"color: #0000ff; font-size: x-small;\"><span style=\"color: #0000ff; font-size: x-small;\">&nbsp;&nbsp; Catch<\/span><\/span><span style=\"color: #000000; font-size: x-small;\"> ex <\/span><span style=\"color: #0000ff; font-size: x-small;\"><span style=\"color: #0000ff; font-size: x-small;\">As<\/span><\/span><span style=\"font-size: x-small;\"> Exception<br \/><\/span><span style=\"color: #0000ff; font-size: x-small;\"><span style=\"color: #0000ff; font-size: x-small;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Throw<\/span><\/span><span style=\"font-size: x-small;\"> <span style=\"color: #000000;\">ex<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #008000;\">&#8216; handle however appropriate in your env here<\/span><\/span><\/span><\/span><\/span><\/span><\/span><span style=\"font-size: medium;\"><span style=\"color: #0000ff; font-size: x-small;\"><span style=\"color: #0000ff; font-size: x-small;\"><span style=\"color: #0000ff; font-size: x-small;\"><span style=\"color: #0000ff; font-size: x-small;\"><span style=\"font-size: x-small;\"><span style=\"color: #000000;\"><br \/><\/span><\/span><span style=\"color: #0000ff; font-size: x-small;\"><span style=\"color: #0000ff; font-size: x-small;\">&nbsp;&nbsp; Finally<br \/><\/span><\/span><span style=\"color: #0000ff; font-size: x-small;\"><span style=\"color: #0000ff; font-size: x-small;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; If <\/span><\/span><span style=\"color: #0000ff; font-size: x-small;\"><span style=\"color: #0000ff; font-size: x-small;\">Not<\/span><\/span><span style=\"font-size: x-small;\"> var <\/span><span style=\"color: #0000ff; font-size: x-small;\"><span style=\"color: #0000ff; font-size: x-small;\">Is <\/span><\/span><span style=\"color: #0000ff; font-size: x-small;\"><span style=\"color: #0000ff; font-size: x-small;\">Nothing <\/span><\/span><span style=\"color: #0000ff; font-size: x-small;\"><span style=\"color: #0000ff; font-size: x-small;\">Then<br \/><\/span><\/span><span style=\"font-size: x-small;\"><span style=\"color: #000000;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var.Unlock()<br \/><\/span><\/span><span style=\"color: #0000ff; font-size: x-small;\"><span style=\"color: #0000ff; font-size: x-small;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End <\/span><\/span><span style=\"color: #0000ff; font-size: x-small;\"><span style=\"color: #0000ff; font-size: x-small;\">If<br \/><\/span><\/span><span style=\"color: #0000ff; font-size: x-small;\"><span style=\"color: #0000ff; font-size: x-small;\">&nbsp;&nbsp; End <\/span><\/span><span style=\"color: #0000ff; font-size: x-small;\"><span style=\"color: #0000ff; font-size: x-small;\">Try<br \/><\/span><\/span><span style=\"color: #0000ff; font-size: x-small;\"><span style=\"color: #0000ff; font-size: x-small;\">&nbsp;&nbsp; Return<\/span><\/span><span style=\"font-size: x-small;\"> <span style=\"color: #000000;\">rtnValue<br \/><\/span><\/span><span style=\"color: #0000ff; font-size: x-small;\"><span style=\"color: #0000ff; font-size: x-small;\">End <\/span><\/span><span style=\"color: #0000ff; font-size: x-small;\"><span style=\"color: #0000ff; font-size: x-small;\">Function<\/span><\/span><\/span><\/span><\/span><\/span><\/span>&nbsp;<\/p>\n<p>&#8230; that&#8217;s all she wrote.&nbsp;<\/p>\n<p>Have fun!&nbsp; I&#8217;ve got to go back to a huge amount of learning of my own, right now&#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A very long time ago, C &amp; I were developers consulting to a large corporation with a not-very-large IT department and a not-very-expert SQL Server DBA. The work we did for these folks was partially .NET and partially Visual FoxPro, if you care.&nbsp; You shouldn&#8217;t care.&nbsp; What matters is that we were developers of applications,<a class=\"more-link\" href=\"https:\/\/spacefold.com\/lisa\/2012\/10\/09\/fixing-orphaned-users-the-sql\/\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-43","post","type-post","status-publish","format-standard","hentry","category-sql-server"],"_links":{"self":[{"href":"https:\/\/spacefold.com\/lisa\/wp-json\/wp\/v2\/posts\/43","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/spacefold.com\/lisa\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/spacefold.com\/lisa\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/spacefold.com\/lisa\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/spacefold.com\/lisa\/wp-json\/wp\/v2\/comments?post=43"}],"version-history":[{"count":0,"href":"https:\/\/spacefold.com\/lisa\/wp-json\/wp\/v2\/posts\/43\/revisions"}],"wp:attachment":[{"href":"https:\/\/spacefold.com\/lisa\/wp-json\/wp\/v2\/media?parent=43"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/spacefold.com\/lisa\/wp-json\/wp\/v2\/categories?post=43"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/spacefold.com\/lisa\/wp-json\/wp\/v2\/tags?post=43"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}