{"id":39,"date":"2013-01-12T16:22:00","date_gmt":"2013-01-12T16:22:00","guid":{"rendered":"\/lisa\/post\/2013\/01\/12\/We-has-ourselves-a-situation-here.aspx"},"modified":"2013-01-12T16:22:00","modified_gmt":"2013-01-12T16:22:00","slug":"we-has-ourselves-a-situation-here","status":"publish","type":"post","link":"https:\/\/spacefold.com\/lisa\/2013\/01\/12\/we-has-ourselves-a-situation-here\/","title":{"rendered":"We has ourselves a situation here"},"content":{"rendered":"<p>I&#8217;m mulling over some design decisions that will probably result in my using Table-Valued Parameters (TVPs) for the first time. Let&#8217;s see what you think.<\/p>\n<p class=\"NB\">Please note: this post applies to SQL 2008 and above.&nbsp;&nbsp;No real differences in the restrictions or behavior in SQL 2012, AFAICS.&nbsp;<\/p>\n<h2>The problem to solve<\/h2>\n<p>We have a complex SSIS flow that handles batch imports, with a large number of validation steps on staging tables before the eventual consumption of the correctly transformed, and correctly reduced, set of rows by production tables.<\/p>\n<p>For various reasons, I need to reproduce this logic without using SSIS, and without using staging tables, in some circumstances.<\/p>\n<p>An external application will invoke a single &#8220;master process&#8221; method, which in turn will invoke all the logic necessary to handle the original SSIS behavior.<\/p>\n<p>I want the sequence of steps to remain faithful to the original SSIS process,&nbsp;to ensure&nbsp;that data can be consumed both ways with the same results.&nbsp;<\/p>\n<p>For the purpose of performance considerations, please understand that the SSIS process handles large-scale imports, whereas this new one is light-weight.&nbsp; I don&#8217;t have to worry about bulk processing in the new design.&nbsp; In fact, the expected use cases for it are real-time integrations with only a single row being passed in &#8212; although I&#8217;d like my methodology to handle multiple rows, in small numbers, as well.<\/p>\n<h2>Expected solution<\/h2>\n<p>I like the idea of creating a custom or user-defined table type for this purpose.&nbsp; Rather than manipulating a staging table, it seems ideal to move the data into a variable shaped like that staging table, passing it down the line to additional steps, massaging it as I go, until eventually the TVP is ready for upsert.<\/p>\n<h2>What&#8217;s the big deal?<\/h2>\n<p>When you pass a TVP into a called function or a called sproc, however, you must use the READONLY keyword.&nbsp; You can&#8217;t massage the contents of the TVP in the called code and then see the changes reflected in the parent procedure.&nbsp;<\/p>\n<p>I guess another way to say this (if you&#8217;re a&nbsp;.NET programmer)&nbsp;is that it&#8217;s not like passing by reference in the .NET world &#8212; even though you <em>are<\/em>&nbsp; passing by reference in the sense that a copy of the data is not made,&nbsp;and you do reap perf benefits from that fact ordinarily.&nbsp; (As you will see, I&#8217;m probably not going to be reaping that benefit in this solution.)<\/p>\n<p>Still another way to say this (if you&#8217;re a T-SQL guy) is &#8220;you can&#8217;t declare a TVP as an OUT param or, if you could, it wouldn&#8217;t make any difference.&#8221;<\/p>\n<p>So what&#8217;s the best way to resolve this?<\/p>\n<h2>Moving a TVP down a line<\/h2>\n<p>At first I thought I would declare and fill the TVP and then pass it into a series of functions.&nbsp; Each function will do what it has to do, and return a TVP of the same type back.&nbsp; Naturally the return value, declared by and manipulated within the function, is readwrite for the function.&nbsp; The master proc would have code something like this:<\/p>\n<p class=\"code\"><span style=\"color: #0000ff; font-family: Consolas; font-size: x-small;\"><span style=\"color: #0000ff; font-family: Consolas; font-size: x-small;\"><span style=\"color: #0000ff; font-family: Consolas; font-size: x-small;\">DECLARE<\/span><\/span><\/span><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\">@TestTVPInstance<\/span><\/span><\/span><span style=\"color: #0000ff; font-family: Consolas; font-size: x-small;\"><span style=\"color: #0000ff; font-family: Consolas; font-size: x-small;\"><span style=\"color: #0000ff; font-family: Consolas; font-size: x-small;\">as<\/span><\/span><\/span><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\">dbo<\/span><\/span><\/span><span style=\"color: #808080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #808080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #808080; font-family: Consolas; font-size: x-small;\">.<\/span><\/span><\/span><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\">TestTVP<span style=\"color: #808080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #808080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #808080; font-family: Consolas; font-size: x-small;\">;<\/span><\/span><\/span><\/span><\/span><\/span><\/p>\n<p><span style=\"font-family: Consolas; font-size: x-small;\"><span style=\"font-family: Consolas; font-size: x-small;\">&nbsp;<span style=\"color: #008000;\">&#8212; use a function to update for each step,<br \/>&nbsp;&#8212; like this:<br \/><\/span><\/span><\/span><span style=\"color: #ff00ff; font-family: Consolas; font-size: x-small;\"><span style=\"color: #ff00ff; font-family: Consolas; font-size: x-small;\"><span style=\"color: #ff00ff; font-family: Consolas; font-size: x-small;\">UPDATE <\/span><\/span><\/span><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\">@TestTVPInstance<br \/><\/span><\/span><\/span><span style=\"color: #0000ff; font-family: Consolas; font-size: x-small;\"><span style=\"color: #0000ff; font-family: Consolas; font-size: x-small;\"><span style=\"color: #0000ff; font-family: Consolas; font-size: x-small;\">&nbsp;&nbsp; SET <\/span><\/span><\/span><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\">col2 <\/span><\/span><\/span><span style=\"color: #808080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #808080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #808080; font-family: Consolas; font-size: x-small;\">= <\/span><\/span><\/span><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\">tvp2<\/span><\/span><\/span><span style=\"color: #808080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #808080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #808080; font-family: Consolas; font-size: x-small;\">.<\/span><\/span><\/span><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\">col2<br \/><\/span><\/span><\/span><span style=\"color: #0000ff; font-family: Consolas; font-size: x-small;\"><span style=\"color: #0000ff; font-family: Consolas; font-size: x-small;\"><span style=\"color: #0000ff; font-family: Consolas; font-size: x-small;\">FROM <\/span><\/span><\/span><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\">dbo<\/span><\/span><\/span><span style=\"color: #808080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #808080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #808080; font-family: Consolas; font-size: x-small;\">.<\/span><\/span><\/span><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\">TestUpdateTVPByFunction<\/span><\/span><\/span><span style=\"color: #808080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #808080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #808080; font-family: Consolas; font-size: x-small;\">(<\/span><\/span><\/span><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\">@TestTVPInstance<\/span><\/span><\/span><span style=\"color: #808080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #808080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #808080; font-family: Consolas; font-size: x-small;\">) <\/span><\/span><\/span><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\">tvp2<br \/><\/span><\/span><\/span><span style=\"color: #808080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #808080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #808080; font-family: Consolas; font-size: x-small;\">&nbsp;&nbsp; JOIN <\/span><\/span><\/span><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\">@TestTVPInstance<\/span><\/span><\/span><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\">tvp1<br \/><\/span><\/span><\/span><span style=\"color: #0000ff; font-family: Consolas; font-size: x-small;\"><span style=\"color: #0000ff; font-family: Consolas; font-size: x-small;\"><span style=\"color: #0000ff; font-family: Consolas; font-size: x-small;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ON <\/span><\/span><\/span><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\">tvp1<\/span><\/span><\/span><span style=\"color: #808080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #808080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #808080; font-family: Consolas; font-size: x-small;\">.<\/span><\/span><\/span><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\">col1 <\/span><\/span><\/span><span style=\"color: #808080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #808080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #808080; font-family: Consolas; font-size: x-small;\">= <\/span><\/span><\/span><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\">tvp2<\/span><\/span><\/span><span style=\"color: #808080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #808080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #808080; font-family: Consolas; font-size: x-small;\">.<\/span><\/span><\/span><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\">col1<\/span><\/span><\/span><span style=\"color: #808080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #808080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #808080; font-family: Consolas; font-size: x-small;\">;<\/span><\/span><\/span><\/p>\n<p>There are two problems with this approach:<\/p>\n<p>1 &#8211; There are a lot of things you just can&#8217;t do in functions, including dynamic SQL and using temp tables.&nbsp; Each step in the current process does a lot of &#8220;stuff&#8221;, in many cases using techniques not allowed in functions.&nbsp; The further I modify the original code for the new process from the original, the more&nbsp;difficult it will be to get exactly consistent results.<\/p>\n<p>2 &#8211; Looking at the above code, what you don&#8217;t see is the (large) number of columns&nbsp;to be UPDATEd&nbsp;in the real code.&nbsp; Given the (large) number of steps there are all together, each with its clumsy&nbsp;UPDATE statement, &nbsp;the master process starts to look like an ungainly mess. It will be really difficult to follow the flow.&nbsp; Contrast that with the nice visual presentation we had in the SSIS flow, and it&#8217;s not a happy thought.&nbsp; I want some structure in the code that gives you something of the sense of the process, just as the SSIS Designer provides it.&nbsp; Well, near as I can get.<\/p>\n<p>So, I changed it up.<\/p>\n<h2>Chaining TVPs&nbsp;<\/h2>\n<p>At the moment, I&#8217;m looking at a much cleaner presentation in the master proc, using a series of TVPs in the master procedure, each of which is the input to one step and results in the filling of another one.&nbsp; The code looks like this at the top:<\/p>\n<p class=\"code\"><span style=\"color: #0000ff; font-family: Consolas; font-size: x-small;\"><span style=\"color: #0000ff; font-family: Consolas; font-size: x-small;\"><span style=\"color: #0000ff; font-family: Consolas; font-size: x-small;\">DECLARE <\/span><\/span><\/span><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\">@TestTVPInstance <\/span><\/span><\/span><span style=\"color: #0000ff; font-family: Consolas; font-size: x-small;\"><span style=\"color: #0000ff; font-family: Consolas; font-size: x-small;\"><span style=\"color: #0000ff; font-family: Consolas; font-size: x-small;\">as <\/span><\/span><\/span><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\">dbo<\/span><\/span><\/span><span style=\"color: #808080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #808080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #808080; font-family: Consolas; font-size: x-small;\">.<\/span><\/span><\/span><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\">TestTVP<\/span><\/span><\/span><span style=\"color: #808080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #808080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #808080; font-family: Consolas; font-size: x-small;\">,<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><\/span><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\">@TestTVPInstance_2 <\/span><\/span><\/span><span style=\"color: #0000ff; font-family: Consolas; font-size: x-small;\"><span style=\"color: #0000ff; font-family: Consolas; font-size: x-small;\"><span style=\"color: #0000ff; font-family: Consolas; font-size: x-small;\">as <\/span><\/span><\/span><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\">dbo<\/span><\/span><\/span><span style=\"color: #808080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #808080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #808080; font-family: Consolas; font-size: x-small;\">.<\/span><\/span><\/span><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\">TestTVP<\/span><\/span><\/span><span style=\"color: #808080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #808080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #808080; font-family: Consolas; font-size: x-small;\">,<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><\/span><\/span><span style=\"color: #008000; font-family: Consolas; font-size: x-small;\"><span style=\"color: #008000; font-family: Consolas; font-size: x-small;\"><span style=\"color: #008000; font-family: Consolas; font-size: x-small;\">&#8212; lots more here&#8230; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><\/span><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\">@TestTVPInstance_N <\/span><\/span><\/span><span style=\"color: #0000ff; font-family: Consolas; font-size: x-small;\"><span style=\"color: #0000ff; font-family: Consolas; font-size: x-small;\"><span style=\"color: #0000ff; font-family: Consolas; font-size: x-small;\">as <\/span><\/span><\/span><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\">dbo<\/span><\/span><\/span><span style=\"color: #808080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #808080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #808080; font-family: Consolas; font-size: x-small;\">.<\/span><\/span><\/span><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\">TestTVP<\/span><\/span><\/span><span style=\"color: #808080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #808080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #808080; font-family: Consolas; font-size: x-small;\">;<\/span><\/span><\/span><\/p>\n<p>The first step fills the first TVP using the input data, which is coming in from the external application, and of course is not a TVP itself (it&#8217;s actually XML, but that&#8217;s another blog post).<\/p>\n<p>Thereafter,&nbsp;the procedure&#8217;s steps&nbsp;look something like this (with more descriptive proc names, obviously)&nbsp;;<\/p>\n<p class=\"code\"><span style=\"color: #008000; font-family: Consolas; font-size: x-small;\">&#8212; use a procedure to manipulate<br \/>&#8212; and bring back into a second copy:<\/span><\/p>\n<p><span style=\"color: #0000ff; font-family: Consolas; font-size: x-small;\"><span style=\"color: #0000ff; font-family: Consolas; font-size: x-small;\"><span style=\"color: #0000ff; font-family: Consolas; font-size: x-small;\">INSERT <\/span><\/span><\/span><span style=\"color: #0000ff; font-family: Consolas; font-size: x-small;\"><span style=\"color: #0000ff; font-family: Consolas; font-size: x-small;\"><span style=\"color: #0000ff; font-family: Consolas; font-size: x-small;\">INTO <\/span><\/span><\/span><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\">@TestTVPInstance_2<br \/>&nbsp;&nbsp; <\/span><\/span><\/span><span style=\"color: #0000ff; font-family: Consolas; font-size: x-small;\"><span style=\"color: #0000ff; font-family: Consolas; font-size: x-small;\"><span style=\"color: #0000ff; font-family: Consolas; font-size: x-small;\">EXEC <\/span><\/span><\/span><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\">dbo<\/span><\/span><\/span><span style=\"color: #808080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #808080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #808080; font-family: Consolas; font-size: x-small;\">.<\/span><\/span><\/span><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\">TestUpdateTVPByProc <\/span><\/span><\/span><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\">@TestTVPInstance<\/span><\/span><\/span><span style=\"color: #808080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #808080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #808080; font-family: Consolas; font-size: x-small;\">;<\/p>\n<p><\/span><\/span><\/span><span style=\"color: #008000; font-family: Consolas; font-size: x-small;\"><span style=\"color: #008000; font-family: Consolas; font-size: x-small;\"><span style=\"color: #008000; font-family: Consolas; font-size: x-small;\">&#8212; many, many more such calls&#8230; do lots of work&#8230; <br \/><\/span><\/span><\/span><span style=\"color: #0000ff; font-family: Consolas; font-size: x-small;\"><span style=\"color: #0000ff; font-family: Consolas; font-size: x-small;\"><span style=\"color: #0000ff; font-family: Consolas; font-size: x-small;\">INSERT <\/span><\/span><\/span><span style=\"color: #0000ff; font-family: Consolas; font-size: x-small;\"><span style=\"color: #0000ff; font-family: Consolas; font-size: x-small;\"><span style=\"color: #0000ff; font-family: Consolas; font-size: x-small;\">INTO <\/span><\/span><\/span><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\">@TestTVPInstance_N<br \/>&nbsp;&nbsp; <\/span><\/span><\/span><span style=\"color: #0000ff; font-family: Consolas; font-size: x-small;\"><span style=\"color: #0000ff; font-family: Consolas; font-size: x-small;\"><span style=\"color: #0000ff; font-family: Consolas; font-size: x-small;\">EXEC <\/span><\/span><\/span><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\">dbo<\/span><\/span><\/span><span style=\"color: #808080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #808080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #808080; font-family: Consolas; font-size: x-small;\">.<\/span><\/span><\/span><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\">TestUpdateTVPByProc_N <\/span><\/span><\/span><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\">@TestTVPInstance_N_minus_1<\/span><\/span><\/span><span style=\"color: #808080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #808080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #808080; font-family: Consolas; font-size: x-small;\">;<\/p>\n<p><\/span><\/span><\/span><span style=\"color: #008000; font-family: Consolas; font-size: x-small;\"><span style=\"color: #008000; font-family: Consolas; font-size: x-small;\"><span style=\"color: #008000; font-family: Consolas; font-size: x-small;\">&#8212; followed by some work done to update a &#8220;real&#8221;<br \/><\/span><\/span><\/span><span style=\"color: #008000; font-family: Consolas; font-size: x-small;\"><span style=\"color: #008000; font-family: Consolas; font-size: x-small;\"><span style=\"color: #008000; font-family: Consolas; font-size: x-small;\">&#8212; table using the final intermediate instance:<br \/><\/span><\/span><\/span><span style=\"color: #0000ff; font-family: Consolas; font-size: x-small;\"><span style=\"color: #0000ff; font-family: Consolas; font-size: x-small;\"><span style=\"color: #0000ff; font-family: Consolas; font-size: x-small;\">EXEC <\/span><\/span><\/span><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\">dbo<\/span><\/span><\/span><span style=\"color: #808080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #808080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #808080; font-family: Consolas; font-size: x-small;\">.<\/span><\/span><\/span><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\">UpdateSomeTableWithComplicatedLogic <\/span><\/span><\/span><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #008080; font-family: Consolas; font-size: x-small;\">@TestTVPInstance_N <span style=\"color: #808080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #808080; font-family: Consolas; font-size: x-small;\"><span style=\"color: #808080; font-family: Consolas; font-size: x-small;\">;<\/span><\/span><\/span><\/span><\/span><\/span><\/p>\n<p>As you can see,&nbsp;the master proc&nbsp;now has the potential to be neat and clear.&nbsp; If each of the sprocs&nbsp;were named in a way that mirrors the task names in the SSIS task flow you would also be able to see the correlation very clearly.<\/p>\n<p>In the words of <a title=\"Shame on you if you don't know this one\" href=\"http:\/\/www.youtube.com\/watch?v=Sw16FXp5ZJk\" target=\"_blank\" rel=\"noopener\">the masters<\/a>&#8230;<\/p>\n<h2>That&#8217;s so crazy it just might work.<\/h2>\n<p>Honestly, I&#8217;m still turning this around.&nbsp; I don&#8217;t know if it is a really obvious idea, and everybody else does this already to use TVPs with some fluency, to get around the READONLY limitation, or if it&#8217;s a really bad idea, and nobody else would do it.<\/p>\n<p>It doesn&#8217;t get around <em>all<\/em> the limitations of TVPs; you can&#8217;t dot-qualify them so that you can use them across databases.<\/p>\n<p>But it seems like it allows TVPs to function, in more scenarios, more the way I think they should.<\/p>\n<p>What&#8217;s your take?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m mulling over some design decisions that will probably result in my using Table-Valued Parameters (TVPs) for the first time. Let&#8217;s see what you think. Please note: this post applies to SQL 2008 and above.&nbsp;&nbsp;No real differences in the restrictions or behavior in SQL 2012, AFAICS.&nbsp; The problem to solve We have a complex SSIS<a class=\"more-link\" href=\"https:\/\/spacefold.com\/lisa\/2013\/01\/12\/we-has-ourselves-a-situation-here\/\">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,7],"tags":[],"class_list":["post-39","post","type-post","status-publish","format-standard","hentry","category-sql-server","category-ssis"],"_links":{"self":[{"href":"https:\/\/spacefold.com\/lisa\/wp-json\/wp\/v2\/posts\/39","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=39"}],"version-history":[{"count":0,"href":"https:\/\/spacefold.com\/lisa\/wp-json\/wp\/v2\/posts\/39\/revisions"}],"wp:attachment":[{"href":"https:\/\/spacefold.com\/lisa\/wp-json\/wp\/v2\/media?parent=39"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/spacefold.com\/lisa\/wp-json\/wp\/v2\/categories?post=39"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/spacefold.com\/lisa\/wp-json\/wp\/v2\/tags?post=39"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}