Question About Inserting Values from one Table to Another

Posted on 2012-08-13
Last Modified: 2012-08-17
I know its possible to insert values from one SQL Table to another, but is it possible to pass values along in the same statement.

For example:

Select '1', [Column1], [Column2], [Column3] from TableA
into TableB

Where '1' is always the same value or 1 and column1, column2, and column3 would data from the respective columns.
Question by:lm1189
    LVL 65

    Accepted Solution


    INSERT INTO Table1 (goo, foo, boo)
    SELECT gooey, fooey, 42
    FROM Table2
    LVL 65

    Expert Comment

    by:Jim Horn
    >Select '1', [Column1], [Column2], [Column3] from TableA
    >into TableB
    Just to clarify, SELECT .. INTO means that tableB doesn't exist, and you're creating it from scratch + inserting values from the SELECT clause from TableA.

    INSERT .. INTO assumes the table already exists, and performs the INSERT.
    LVL 75

    Expert Comment

    by:Anthony Perkins
    And to be clear the INTO TableB comes before the FROM TableA, as in:
    SELECT  '1',
    INTO    TableB
    FROM    TableA

    Open in new window

    LVL 9

    Expert Comment

    yes no problem.

    insert into tableName
    Select 1 as ColumnName , column1 , column2 from tableA

     here in tableName data is inserted as
    1, column1_value, column2_value for this table three column.
    LVL 9

    Expert Comment

    just one change with your query...

    Select '1', [Column1], [Column2], [Column3] into TableB
      from TableA
    LVL 65

    Expert Comment

    by:Jim Horn
    Thanks for the grade.  Good luck with your project.  -Jim

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Join & Write a Comment

    Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
    In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
    Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
    Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now