January 19, 2012 at 5:38 am
Comments posted to this topic are about the item Stairway to T-SQL DML Level 9: Adding Records to a table using INSERT Statement
Gregory A. Larsen, MVP
September 5, 2012 at 7:19 am
I was unaware of the output option for this. I'm not sure I'll ever really need it but it is cool to know I have the option.
Cheers
September 5, 2012 at 9:48 am
Another useful variation of the INSERT command is the "SIF" format to create a table with fields based on the source table:
SELECT Id, Name, Quantity
INTO SomeOtherTable -- Or even @SomeOtherTable
FROM Fruit
This will create a table ("SomeOtherTable") with the Id, Name, and Quantity columns from the Fruit table. You can specify a WHERE clause to limit the records you pull out (WHERE Quantity > 2). Useful for on-the-fly requirements to get a subset of records out of a larger table to manipulate or scan through.
October 29, 2012 at 11:23 am
Why does the Id column in listing 9 start with 21 after the previous is dropped and the new one is made an identity?
Sorry... newbie here.
October 29, 2012 at 9:41 pm
Very good question. Looks like the article output is incorrect. The id values should be 19 and 20. I'll will get this update. Thank you for pointing this out.
Greg
Gregory A. Larsen, MVP
July 31, 2014 at 10:25 am
In listing 6 you use some logic for Id in the select part of the statement, but I can't figure out what that part is doing. Specifically the "7+(6-Id)", what is this doing?
July 31, 2014 at 10:40 am
Ok, I figured it out, but the syntax is not intuitive. I had to start at the end of the statement and work backwards.
January 6, 2015 at 11:03 pm
Hi
I could not figure this out either, can you share your insight.
I also struggled with - "SELECT b.Id + 9, a.Name + b.name"
As the stairway is a learning tool for beginners it seems to have jumped ahead a bit as this was not covered off in the select section
August 24, 2016 at 9:52 am
While the column list is not necessarily required by the INSERT statement, I have learned that it is at least a best practice to always include it.
If the order of the columns within the table changes, you may start receiving errors or corrupt data (arguably worse) that are difficult to troubleshoot. By always including the column list, you avoid such issues because you are explicitly specifying which column to insert which value into.
Always including the column list also keeps your insert statement from breaking when a new column is added to the table (assuming the column is nullable or has a default value constraint).
August 24, 2016 at 8:34 pm
@greg Larsen,
Your the one that got paid some decent money for posting this series. How about you answer some of the questions for a change!
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply