Stairway to T-SQL DML Level 9: Adding Records to a table using INSERT Statement

  • 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

  • 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

  • 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.

  • 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.

  • 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

  • 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?

  • Ok, I figured it out, but the syntax is not intuitive. I had to start at the end of the statement and work backwards.

  • 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

  • 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).

  • @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.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 10 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic. Login to reply