{"id":262,"date":"2011-06-10T00:05:30","date_gmt":"2011-06-10T00:05:30","guid":{"rendered":"http:\/\/www.sitekickr.com\/blog\/?p=262"},"modified":"2011-06-10T00:21:26","modified_gmt":"2011-06-10T00:21:26","slug":"coldfusion-sql-bulk-insert-structure-keys","status":"publish","type":"post","link":"https:\/\/www.sitekickr.com\/blog\/coldfusion-sql-bulk-insert-structure-keys\/","title":{"rendered":"ColdFusion SQL Bulk Insert based on structure keys"},"content":{"rendered":"<p>I realize this particular requirement might be rare, but it comes in handy when you have an XREF (cross-reference) table which ties two tables together. Many times, when this is the case, it is a common requirement to perform multiple SQL inserts into the table within one request.<\/p>\n<p>If we are performing the inserts from an AJAX call, we might have the required insert data right in our query parameters. This would allow us to loop over the <em>url<\/em> structure to perform a bulk update.<\/p>\n<p>But wait! There&#39;s one problem. When you loop over a structure, you don&#39;t know where the loop ends, so you really don&#39;t know when to stop placing that comma between insert blocks.<\/p>\n<p>For instance<\/p>\n<p><code>INSERT INTO table (key, value)<br \/>\n\tVALUES (&#39;key1&#39;, &#39;value1&#39;), (&#39;key2&#39;, &#39;value2&#39;), (&#39;key3&#39;, value3)<br \/>\n\t<\/code><\/p>\n<p>So with a simple loop, you could do the following:<\/p>\n<p><code>&lt;cfset sql = &quot;INSERT INTO table (key, value) VALUES&quot;&gt;<br \/>\n\t&lt;cfloop collection=&quot;#url#&quot; item=&quot;key&quot;&gt;<br \/>\n\t&nbsp; &lt;cfset sql = &quot;(#key#, #url[key]#),&quot;&gt;<br \/>\n\t&lt;\/cfloop&gt;<br \/>\n\t&lt;cfset sql = Left(sql, len(sql) - 1)&gt;&nbsp; &lt;!--- to get rid of that trailing comma ---&gt;<br \/>\n\t<\/code><\/p>\n<p>Then, run the query in a <em>&lt;cfquery&gt;<\/em> statement.<\/p>\n<p>But, wait again! What about securing our queries with <em>&lt;cfqueryparam&gt;<\/em>? That&#39;s a problem here because <em>&lt;cfqueryparam&gt;<\/em> can only be nested inside a <em>&lt;cfquery&gt;<\/em> block.<\/p>\n<p>That&#39;s where <em>&lt;cfsavecontent&gt;<\/em> comes in. See the example below:<\/p>\n<p><code>&lt;cfquery&gt;<br \/>\n\t&nbsp;&nbsp;&nbsp; INSERT INTO table<br \/>\n\t&nbsp;&nbsp;&nbsp; (<br \/>\n\t&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; key,<br \/>\n\t&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; value,<br \/>\n\t&nbsp;&nbsp;&nbsp; )<br \/>\n\t&nbsp;&nbsp;&nbsp; VALUES<br \/>\n\t&nbsp;&nbsp;&nbsp; &lt;cfsavecontent variable=&quot;inserts&quot;&gt;<br \/>\n\t&nbsp;&nbsp;&nbsp; &lt;cfloop collection=&quot;#url#&quot; item=&quot;key&quot;&gt;<br \/>\n\t&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; (<br \/>\n\t&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;cfqueryparam value=&quot;#key#&quot;&gt;,<br \/>\n\t&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;cfqueryparam value=&quot;#url[key]#&quot;&gt;<br \/>\n\t&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ),&lt;\/cfloop&gt;&lt;\/cfsavecontent&gt;<br \/>\n\t&nbsp;&nbsp;&nbsp; #Left(inserts, len(inserts) - 2)#<br \/>\n\t&lt;\/cfquery&gt;<br \/>\n\t<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I realize this particular requirement might be rare, but it comes in handy when you have an XREF (cross-reference) table which ties two tables together.&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"amp_status":""},"categories":[15,34],"tags":[94],"_links":{"self":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts\/262"}],"collection":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/comments?post=262"}],"version-history":[{"count":3,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts\/262\/revisions"}],"predecessor-version":[{"id":265,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts\/262\/revisions\/265"}],"wp:attachment":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/media?parent=262"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/categories?post=262"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/tags?post=262"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}