|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: Thursday, May 16, 2013 5:30 AM
Points: 861,
Visits: 1,436
|
|
Hugo Kornelis (11/29/2011)
INSERT INTO #ATable (col1, col2, col3) SELECT 2, 1, 3; (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: INSERT INTO #BTable (col1, col2, col3, col4, col5, col6, col7, col8) SELECT 2, 1, 3, 4, 5, 6, 7, 8;
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:
INSERT INTO #ATable ( Col1, Col2, Col3 ) VALUES ( 2 AS Col1, 1 AS Col2, 3 AS Col3 );
I'm not sure if it's a good way of doing it but it looks clear to me. 
Best regards,
Best regards,
Andre Guerreiro Neto
Database Analyst http://www.softplan.com.br MCITPx1/MCTSx2
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 2:24 AM
Points: 5,235,
Visits: 7,040
|
|
codebyo (11/29/2011)
I usually code my INSERTs like this whenever possible: INSERT INTO #ATable ( Col1, Col2, Col3 ) VALUES ( 2 AS Col1, 1 AS Col2, 3 AS Col3 );
I'm not sure if it's a good way of doing it but it looks clear to me.  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.
Hugo Kornelis, SQL Server MVP Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
|
|
|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: Thursday, May 16, 2013 5:30 AM
Points: 861,
Visits: 1,436
|
|
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,
Best regards,
Andre Guerreiro Neto
Database Analyst http://www.softplan.com.br MCITPx1/MCTSx2
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Thursday, January 31, 2013 8:53 AM
Points: 1,176,
Visits: 778
|
|
|
|
|
|
Hall of Fame
       
Group: General Forum Members
Last Login: Yesterday @ 10:31 AM
Points: 3,454,
Visits: 2,529
|
|
| Thanks for the question -- it was a really easy one!
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Yesterday @ 8:52 AM
Points: 2,574,
Visits: 1,532
|
|
| I too was looking for a catch. Good back to basics question. Thanks!
|
|
|
|
|
Mr or Mrs. 500
      
Group: General Forum Members
Last Login: 2 days ago @ 6:47 AM
Points: 581,
Visits: 694
|
|
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
|
|
|
|
|
Right there with Babe
      
Group: General Forum Members
Last Login: 2 days ago @ 10:58 AM
Points: 727,
Visits: 1,086
|
|
Thanks for the question HESPO, good to see you again.
--TPET
|
|
|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: Saturday, March 16, 2013 9:53 AM
Points: 847,
Visits: 768
|
|
Thank you for the question Iulian
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Today @ 1:49 AM
Points: 1,131,
Visits: 855
|
|
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
|
|
|
|