﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / Article Discussions / Article Discussions by Author / Discuss content posted by Henrik Staun Polsen  / Insert Into problems / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Wed, 22 May 2013 05:26:26 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Insert Into problems</title><link>http://www.sqlservercentral.com/Forums/Topic1212861-1431-1.aspx</link><description>good one. Learn new thing !!!</description><pubDate>Mon, 19 Dec 2011 05:34:01 GMT</pubDate><dc:creator>Danny Ocean</dc:creator></item><item><title>RE: Insert Into problems</title><link>http://www.sqlservercentral.com/Forums/Topic1212861-1431-1.aspx</link><description>Thank for question, but it's easy.</description><pubDate>Mon, 19 Dec 2011 04:04:29 GMT</pubDate><dc:creator>Danny Ocean</dc:creator></item><item><title>RE: Insert Into problems</title><link>http://www.sqlservercentral.com/Forums/Topic1212861-1431-1.aspx</link><description>Simple but Nice one ..</description><pubDate>Wed, 07 Dec 2011 02:52:12 GMT</pubDate><dc:creator>amit_adarsh</dc:creator></item><item><title>RE: Insert Into problems</title><link>http://www.sqlservercentral.com/Forums/Topic1212861-1431-1.aspx</link><description>Good Question.I got it correct .I realised this issue long back when am working with insert statements due to mismatch columns.some it will throw exceptions if data type is not macthing while inserting.</description><pubDate>Tue, 06 Dec 2011 23:13:21 GMT</pubDate><dc:creator>malleswarareddy_m</dc:creator></item><item><title>RE: Insert Into problems</title><link>http://www.sqlservercentral.com/Forums/Topic1212861-1431-1.aspx</link><description>Good question, thanks for submitting.</description><pubDate>Wed, 30 Nov 2011 07:26:55 GMT</pubDate><dc:creator>Britt Cluff</dc:creator></item><item><title>RE: Insert Into problems</title><link>http://www.sqlservercentral.com/Forums/Topic1212861-1431-1.aspx</link><description>Thank you all, for the nice words.A special "thank you" goes to Hugo, for the suggestion for formatting the fields in 3 columns, or how-ever many columns that fit onto one screen.Best regards,Henrik</description><pubDate>Wed, 30 Nov 2011 01:48:21 GMT</pubDate><dc:creator>henrik staun poulsen</dc:creator></item><item><title>RE: Insert Into problems</title><link>http://www.sqlservercentral.com/Forums/Topic1212861-1431-1.aspx</link><description>Thank you for the questionIulian</description><pubDate>Tue, 29 Nov 2011 13:31:24 GMT</pubDate><dc:creator>Iulian -207023</dc:creator></item><item><title>RE: Insert Into problems</title><link>http://www.sqlservercentral.com/Forums/Topic1212861-1431-1.aspx</link><description>Thanks for the question HESPO, good to see you again.--TPET</description><pubDate>Tue, 29 Nov 2011 13:29:44 GMT</pubDate><dc:creator>T_Peters</dc:creator></item><item><title>RE: Insert Into problems</title><link>http://www.sqlservercentral.com/Forums/Topic1212861-1431-1.aspx</link><description>Good question.  Something like this can trip anyone up.  I'm sorry that it took a few hours to recognize the mistake, but we have all been there at some point.  I can recall pouring over code only to find a colon where a semi-colon was required.  Thanks,Matt</description><pubDate>Tue, 29 Nov 2011 10:48:18 GMT</pubDate><dc:creator>Mattrick</dc:creator></item><item><title>RE: Insert Into problems</title><link>http://www.sqlservercentral.com/Forums/Topic1212861-1431-1.aspx</link><description>I too was looking for a catch. Good back to basics question. Thanks!</description><pubDate>Tue, 29 Nov 2011 09:53:55 GMT</pubDate><dc:creator>KWymore</dc:creator></item><item><title>RE: Insert Into problems</title><link>http://www.sqlservercentral.com/Forums/Topic1212861-1431-1.aspx</link><description>Thanks for the question -- it was a really easy one!</description><pubDate>Tue, 29 Nov 2011 08:57:47 GMT</pubDate><dc:creator>Revenant</dc:creator></item><item><title>RE: Insert Into problems</title><link>http://www.sqlservercentral.com/Forums/Topic1212861-1431-1.aspx</link><description>thanks for the questions</description><pubDate>Tue, 29 Nov 2011 08:39:34 GMT</pubDate><dc:creator>OzYbOi d(-_-)b</dc:creator></item><item><title>RE: Insert Into problems</title><link>http://www.sqlservercentral.com/Forums/Topic1212861-1431-1.aspx</link><description>Thanks for the comments, Hugo.I will keep that in mind. :-)We have a lot denormalized tables with 100+ columns and it's a major pain to maintain their DML.Best regards,</description><pubDate>Tue, 29 Nov 2011 08:16:08 GMT</pubDate><dc:creator>codebyo</dc:creator></item><item><title>RE: Insert Into problems</title><link>http://www.sqlservercentral.com/Forums/Topic1212861-1431-1.aspx</link><description>[quote][b]codebyo (11/29/2011)[/b][hr]I usually code my INSERTs like this whenever possible:[code="sql"]INSERT INTO #ATable(	Col1,	Col2,	Col3)VALUES(	2 AS Col1,	1 AS Col2,	3 AS Col3);[/code]I'm not sure if it's a good way of doing it but it looks clear to me. :-)[/quote]I'm not really too fond about the aliases; I think it'd be better to use comments - as that is the only function the alias serves anyway.Other than that, this method is fine - as long as the comments and the actual columns match up. But what happens if a change is made somewhere and the comments are not updated? If that happens a few time, developers will end up ignoring the column names in the comments (or aliases), as they know that those aren't always reliable - and than it's back to counting the 23rd column name from the list. :crying:</description><pubDate>Tue, 29 Nov 2011 07:58:33 GMT</pubDate><dc:creator>Hugo Kornelis</dc:creator></item><item><title>RE: Insert Into problems</title><link>http://www.sqlservercentral.com/Forums/Topic1212861-1431-1.aspx</link><description>[quote][b]Hugo Kornelis (11/29/2011)[/b][hr][code="sql"]INSERT INTO #ATable       (col1,               col2,            col3)SELECT  2,                  1,               3;[/code](Where I change the column positions based on the length of the column names and expressions in the SELECT list).I usually limit myself to three columns per line, because I don't like to scroll horizontally. So when there's a long column list, I use this style:[code="sql"]INSERT INTO #BTable       (col1,               col2,            col3,        col4,               col5,            col6,        col7,               col8)SELECT  2,                  1,               3,        4,                  5,               6,        7,                  8;[/code][/quote]That's a great advice. In my day job, I can see, in many codes, a long list of columns without proper formatting and it gets harder and harder to read once the table's structure changes and you need to update code.I usually code my INSERTs like this whenever possible:[code="sql"]INSERT INTO #ATable(	Col1,	Col2,	Col3)VALUES(	2 AS Col1,	1 AS Col2,	3 AS Col3);[/code]I'm not sure if it's a good way of doing it but it looks clear to me. :-)Best regards,</description><pubDate>Tue, 29 Nov 2011 07:50:01 GMT</pubDate><dc:creator>codebyo</dc:creator></item><item><title>RE: Insert Into problems</title><link>http://www.sqlservercentral.com/Forums/Topic1212861-1431-1.aspx</link><description>@Tol, I like her even better that way :-D</description><pubDate>Tue, 29 Nov 2011 07:00:57 GMT</pubDate><dc:creator>Ninja's_RGR'us</dc:creator></item><item><title>RE: Insert Into problems</title><link>http://www.sqlservercentral.com/Forums/Topic1212861-1431-1.aspx</link><description>Good question.It caught me, although it most certainly shouldn't have :blush:.  Guess I shouldn't try to understand SQL until the hangover subsides :doze:</description><pubDate>Tue, 29 Nov 2011 06:57:34 GMT</pubDate><dc:creator>L' Eomot Inversé</dc:creator></item><item><title>RE: Insert Into problems</title><link>http://www.sqlservercentral.com/Forums/Topic1212861-1431-1.aspx</link><description>What he said.Making it obvious what's going where is, IMHO, really important.  Similarly, I try hard to avoid scrolling past 80 characters (as that seems to be a default for things like command line editing on Unix).[quote][b]Thomas Abraham (11/29/2011)[/b][hr][quote][b]Hugo Kornelis (11/29/2011)[/b][hr]Here's the way I write INSERT statements, to make it easier to say which expression in the SELECT list matches which column in the INSERT list:[code="sql"]INSERT INTO #ATable       (col1,               col2,            col3)SELECT  2,                  1,               3;[/code](Where I change the column positions based on the length of the column names and expressions in the SELECT list).I usually limit myself to three columns per line, because I don't like to scroll horizontally. So when there's a long column list, I use this style:[code="sql"]INSERT INTO #BTable       (col1,               col2,            col3,        col4,               col5,            col6,        col7,               col8)SELECT  2,                  1,               3,        4,                  5,               6,        7,                  8;[/code][/quote]I prefer this method as well. It makes it easier for the next person, or myself, to maintain the code. I work with a programmer that runs the query text out past the edge of a reasonable editor and uses all lower case. That makes it very hard to read.To the OP: Thanks for the question! It was easy, but a good question.[/quote]</description><pubDate>Tue, 29 Nov 2011 06:53:18 GMT</pubDate><dc:creator>marlon.seton</dc:creator></item><item><title>RE: Insert Into problems</title><link>http://www.sqlservercentral.com/Forums/Topic1212861-1431-1.aspx</link><description>I put to much though into that one at first because I though it was going to be a collate issue with the INT vs int.In the end, nice and simple question.</description><pubDate>Tue, 29 Nov 2011 06:26:44 GMT</pubDate><dc:creator>mbova407</dc:creator></item><item><title>RE: Insert Into problems</title><link>http://www.sqlservercentral.com/Forums/Topic1212861-1431-1.aspx</link><description>[quote][b]Hugo Kornelis (11/29/2011)[/b][hr]Here's the way I write INSERT statements, to make it easier to say which expression in the SELECT list matches which column in the INSERT list:[code="sql"]INSERT INTO #ATable       (col1,               col2,            col3)SELECT  2,                  1,               3;[/code](Where I change the column positions based on the length of the column names and expressions in the SELECT list).I usually limit myself to three columns per line, because I don't like to scroll horizontally. So when there's a long column list, I use this style:[code="sql"]INSERT INTO #BTable       (col1,               col2,            col3,        col4,               col5,            col6,        col7,               col8)SELECT  2,                  1,               3,        4,                  5,               6,        7,                  8;[/code][/quote]I prefer this method as well. It makes it easier for the next person, or myself, to maintain the code. I work with a programmer that runs the query text out past the edge of a reasonable editor and uses all lower case. That makes it very hard to read.To the OP: Thanks for the question! It was easy, but a good question.</description><pubDate>Tue, 29 Nov 2011 05:49:28 GMT</pubDate><dc:creator>Thomas Abraham</dc:creator></item><item><title>RE: Insert Into problems</title><link>http://www.sqlservercentral.com/Forums/Topic1212861-1431-1.aspx</link><description>Nice question. Not terribly hard, but interesting. It could have been made a bit harder by omitting the column list in the INSERT:[code="sql"]INSERT INTO #ATableSELECT 2 AS col2, 1 AS col1, 3 AS col3[/code]For the record, I am not condoning the above as good practice. In my book, both omitting the column list of the INSERT statement and adding aliases to the SELECT list of an INSERT ... SELECT are bad practices.A final remark - I don't see how the version in the explanation, that uses comments instead of aliases, is any clearer than the original. The column names in the comments don't match reality, so these comments are obfuscating the code instead of clarifying it.Here's the way I write INSERT statements, to make it easier to say which expression in the SELECT list matches which column in the INSERT list:[code="sql"]INSERT INTO #ATable       (col1,               col2,            col3)SELECT  2,                  1,               3;[/code](Where I change the column positions based on the length of the column names and expressions in the SELECT list).I usually limit myself to three columns per line, because I don't like to scroll horizontally. So when there's a long column list, I use this style:[code="sql"]INSERT INTO #BTable       (col1,               col2,            col3,        col4,               col5,            col6,        col7,               col8)SELECT  2,                  1,               3,        4,                  5,               6,        7,                  8;[/code]</description><pubDate>Tue, 29 Nov 2011 05:23:04 GMT</pubDate><dc:creator>Hugo Kornelis</dc:creator></item><item><title>RE: Insert Into problems</title><link>http://www.sqlservercentral.com/Forums/Topic1212861-1431-1.aspx</link><description>It is simply can be vieved, value inserted in respective columns will be in column order ...</description><pubDate>Tue, 29 Nov 2011 05:19:36 GMT</pubDate><dc:creator>rajucse.kumar</dc:creator></item><item><title>RE: Insert Into problems</title><link>http://www.sqlservercentral.com/Forums/Topic1212861-1431-1.aspx</link><description>well, it was a bug...In my own code!I know; I created it myself.Easy for me to fix, as soon as I noticed that column order was important.It is a bit like this problem in SSIS:Will this round or truncate?[code="other"](DT_I4)(SpeedIntervalFrom / 0.5) [/code]It is an expression in a Derived column component.Well, it should have been [code="other"](DT_I4)FLOOR(SpeedIntervalFrom / 0.5) [/code]because we wanted it to truncate.</description><pubDate>Tue, 29 Nov 2011 05:00:22 GMT</pubDate><dc:creator>henrik staun poulsen</dc:creator></item><item><title>RE: Insert Into problems</title><link>http://www.sqlservercentral.com/Forums/Topic1212861-1431-1.aspx</link><description>thanks for question!</description><pubDate>Tue, 29 Nov 2011 04:15:20 GMT</pubDate><dc:creator>rfr.ferrari</dc:creator></item><item><title>RE: Insert Into problems</title><link>http://www.sqlservercentral.com/Forums/Topic1212861-1431-1.aspx</link><description>[quote][b]Nic-306421 (11/29/2011)[/b][hr][quote]I've spent a few hours, before I noticed the bug.[/quote]It may just be me, but I wouldn't class this a bug...[/quote]It's not just you ;-)</description><pubDate>Tue, 29 Nov 2011 03:08:07 GMT</pubDate><dc:creator>Koen Verbeeck</dc:creator></item><item><title>RE: Insert Into problems</title><link>http://www.sqlservercentral.com/Forums/Topic1212861-1431-1.aspx</link><description>[quote]I've spent a few hours, before I noticed the bug.[/quote]It may just be me, but I wouldn't class this a bug, if that's what are you are saying, the AS is (as has been stated in this post) for the alias, it has no bearing on the order of the insert.None the less a fun question, so thank you.Nic</description><pubDate>Tue, 29 Nov 2011 02:53:12 GMT</pubDate><dc:creator>Nic-306421</dc:creator></item><item><title>RE: Insert Into problems</title><link>http://www.sqlservercentral.com/Forums/Topic1212861-1431-1.aspx</link><description>Simple One........</description><pubDate>Tue, 29 Nov 2011 02:41:26 GMT</pubDate><dc:creator>sharath.chalamgari</dc:creator></item><item><title>RE: Insert Into problems</title><link>http://www.sqlservercentral.com/Forums/Topic1212861-1431-1.aspx</link><description>Well, since I wrote it up as a QotD, you may not need three guesses as to who went into this "trap" big time.I've spent a few hours, before I noticed the bug.</description><pubDate>Tue, 29 Nov 2011 01:56:42 GMT</pubDate><dc:creator>henrik staun poulsen</dc:creator></item><item><title>RE: Insert Into problems</title><link>http://www.sqlservercentral.com/Forums/Topic1212861-1431-1.aspx</link><description>[quote][b]Koen Verbeeck (11/28/2011)[/b][hr](and I was looking frantically for 5 minutes to find the "catch". :-D)[/quote]Glad to see I had so much effects on you all :hehe:.</description><pubDate>Tue, 29 Nov 2011 01:55:08 GMT</pubDate><dc:creator>Ninja's_RGR'us</dc:creator></item><item><title>RE: Insert Into problems</title><link>http://www.sqlservercentral.com/Forums/Topic1212861-1431-1.aspx</link><description>So QOTD is a double bluff today...</description><pubDate>Tue, 29 Nov 2011 01:49:14 GMT</pubDate><dc:creator>paul s-306273</dc:creator></item><item><title>RE: Insert Into problems</title><link>http://www.sqlservercentral.com/Forums/Topic1212861-1431-1.aspx</link><description>Thanks, but easy! :-P</description><pubDate>Tue, 29 Nov 2011 00:42:19 GMT</pubDate><dc:creator>palotaiarpad</dc:creator></item><item><title>RE: Insert Into problems</title><link>http://www.sqlservercentral.com/Forums/Topic1212861-1431-1.aspx</link><description>As said before, the AS clause in the SELECT statement is for aliasing the columns, which is disregarded by the INSERT statement.Your statement should have looked like this:INSERT INTO #ATable (col2, col1, col3)...if you wanted to change the order of inserts.Appreciate the effort of putting this in a QoTD.(and I was looking frantically for 5 minutes to find the "catch". :-D)</description><pubDate>Mon, 28 Nov 2011 23:42:01 GMT</pubDate><dc:creator>Koen Verbeeck</dc:creator></item><item><title>RE: Insert Into problems</title><link>http://www.sqlservercentral.com/Forums/Topic1212861-1431-1.aspx</link><description>Thanks for the question.</description><pubDate>Mon, 28 Nov 2011 23:22:00 GMT</pubDate><dc:creator>SQLRNNR</dc:creator></item><item><title>RE: Insert Into problems</title><link>http://www.sqlservercentral.com/Forums/Topic1212861-1431-1.aspx</link><description>Thank you for the question but I guess that's straightforward.The "AS colX" parts of the SELECT statement are aliases to the columns returned by the SELECT statement and they don't reference column positions. The INSERT statement will follow the order specified inside the parenthesis and if no columns were specified in that statement, it will follow the creation order of the columns.Best regards,</description><pubDate>Mon, 28 Nov 2011 20:24:36 GMT</pubDate><dc:creator>codebyo</dc:creator></item><item><title>Insert Into problems</title><link>http://www.sqlservercentral.com/Forums/Topic1212861-1431-1.aspx</link><description>Comments posted to this topic are about the item [B]&lt;A HREF="/questions/T-SQL/76752/"&gt;Insert Into problems&lt;/A&gt;[/B]</description><pubDate>Mon, 28 Nov 2011 20:21:14 GMT</pubDate><dc:creator>henrik staun poulsen</dc:creator></item></channel></rss>