<?xml version="1.0" encoding="utf-8"?>
			
			<rss version="2.0">
			<channel>
			<title>&lt;!--- CFChris ---&gt; - SQL Server</title>
			<link>http://www.cfchris.com/cfchris/index.cfm</link>
			<description>The blog of Chris Phillips, a ColdFusion developer with a passion for improvement.</description>
			<language>en-us</language>
			<pubDate>Sun, 05 Sep 2010 08:04:50 -0700</pubDate>
			<lastBuildDate>Fri, 11 Jul 2008 13:09:00 -0700</lastBuildDate>
			<generator>BlogCFC</generator>
			<docs>http://blogs.law.harvard.edu/tech/rss</docs>
			<managingEditor>me@cfchris.com</managingEditor>
			<webMaster>me@cfchris.com</webMaster>
			
			
			
			
			
			<item>
				<title>SQL Server - Insert Not Working</title>
				<link>http://www.cfchris.com/cfchris/index.cfm/2008/7/11/SQL-Server--Insert-Not-Working</link>
				<description>
				
				Just wanted to share a bit of fun I had yesterday. (Actually it wasn&apos;t very fun). I had a cfquery that was supposed to copy &quot;Template&quot; records linked to one entity inserting them linked to another entity. So, the file was using an Insert/Select. Never seen one? OK, it looks like this.

&lt;code&gt;
	&lt;cfquery datasource=&quot;#request.DSN#&quot;&gt;
		INSERT INTO
			[Template]
			(
				TemplateName,
				OwnerID,
				...
			)
		SELECT
			TemplateName,
			&lt;cfqueryparam value=&quot;#attributes.Target_OwnerID#&quot; cfsqltype=&quot;cf_sql_integer&quot;&gt;,
			...
		FROM
			[Template]
		WHERE
			OwnerID = &lt;cfqueryparam value=&quot;#attributes.Template_OwnerID#&quot; cfsqltype=&quot;cf_sql_integer&quot;&gt;
			...
	&lt;/cfquery&gt;
&lt;/code&gt;

So, we can all agree that insert/select is a great thing. However, this particular one was not working (on the live server). I tried the normal things. I cut the SQL from the .cfm and pasted it into SQL Server Management Studio (SSMS), replaced the params with the values I knew it would be using and ran it (inside of a transaction, of course). Guess what? It worked. So, it works running in SSMS but, not in a cfquery. Hmmm... So, next, I added an &quot;output&quot; clause to the insert/select. That way I could see any records it would be inserting, just to make sure it really wasn&apos;t inserting them. Then I added a cfdump that would only run for me. &quot;What&apos;s an output clause?&quot; you say. Here I&apos;ll show you.

&lt;code&gt;
	&lt;cfquery name=&quot;insTemplates&quot; datasource=&quot;#request.DSN#&quot;&gt;
		INSERT INTO
			[Template]
			(
				TemplateName,
				OwnerID,
				...
			)
		OUTPUT
			INSERTED.*
		SELECT
			TemplateName,
			&lt;cfqueryparam value=&quot;#attributes.Target_OwnerID#&quot; cfsqltype=&quot;cf_sql_integer&quot;&gt;,
			...
		FROM
			[Template]
		WHERE
			OwnerID = &lt;cfqueryparam value=&quot;#attributes.Template_OwnerID#&quot; cfsqltype=&quot;cf_sql_integer&quot;&gt;
			...
	&lt;/cfquery&gt;
	&lt;cfdump var=&quot;#insTemplates#&quot; label=&quot;insTemplates&quot; /&gt;
&lt;/code&gt;

I ran that and guess how many records were inserted... 0. At this point I&apos;m a bit frustrated. But, it begins to dawn on me that I&apos;ve seen similar behavior before from SQL Server. There are time when the SQL Server will cache a bad plan for some SQL. In these cases, you can either have the database flush it&apos;s entire plan cache or you can alter the SQL enough to force it to make a new plan for your SQL. So, I added &quot;AND 1=1&quot; to my &quot;where&quot; clause.

Guess what? It works now.

This is a fairly edge case. I have not seen it very often. But, if you are positive that a cfquery is not returning what it should, before you jump out of your office window, try adding something innocuous to your &quot;where&quot; clause. 

If it does work, feel free to send some happy thoughts my way and curse MS under your breath. :-)
				
				</description>
						
				
				<category>SQL Server</category>				
				
				<category>ColdFusion</category>				
				
				<pubDate>Fri, 11 Jul 2008 13:09:00 -0700</pubDate>
				<guid>http://www.cfchris.com/cfchris/index.cfm/2008/7/11/SQL-Server--Insert-Not-Working</guid>
				
			</item>
			
		 	
			</channel></rss>