Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Stairway to T-SQL DML Level 9: Adding Records to a table using INSERT Statement Expand / Collapse
Author
Message
Posted Thursday, January 19, 2012 5:38 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Wednesday, August 17, 2016 10:41 PM
Points: 1,043, Visits: 290
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
Post #1238575
Posted Wednesday, September 5, 2012 7:19 AM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Today @ 10:22 AM
Points: 397, Visits: 1,136
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
Post #1354515
Posted Wednesday, September 5, 2012 9:48 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Tuesday, April 1, 2014 3:27 PM
Points: 109, Visits: 957
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.
Post #1354696
Posted Monday, October 29, 2012 11:23 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, February 26, 2013 2:27 PM
Points: 9, Visits: 15
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.
Post #1378379
Posted Monday, October 29, 2012 9:41 PM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Wednesday, August 17, 2016 10:41 PM
Points: 1,043, Visits: 290
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
Post #1378554
Posted Thursday, July 31, 2014 10:25 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, August 22, 2014 8:16 AM
Points: 2, Visits: 4
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?
Post #1598366
Posted Thursday, July 31, 2014 10:40 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, August 22, 2014 8:16 AM
Points: 2, Visits: 4
Ok, I figured it out, but the syntax is not intuitive. I had to start at the end of the statement and work backwards.
Post #1598378
Posted Tuesday, January 6, 2015 11:03 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, January 21, 2015 9:22 PM
Points: 12, Visits: 26
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
Post #1648946
Posted Wednesday, August 24, 2016 9:52 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, September 9, 2016 3:28 PM
Points: 18, Visits: 14
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).
Post #1812105
Posted Wednesday, August 24, 2016 8:34 PM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Monday, September 26, 2016 3:35 PM
Points: 41,436, Visits: 38,770
@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
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

Helpful Links:
How to post code problems
How to post performance problems
Post #1812252
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse