?
Solved

Question About Inserting Values from one Table to Another

Posted on 2012-08-13
6
Medium Priority
?
508 Views
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.
0
Comment
Question by:lm1189
6 Comments
 
LVL 66

Accepted Solution

by:
Jim Horn earned 2000 total points
ID: 38288968
Yes.

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

Expert Comment

by:Jim Horn
ID: 38288988
>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.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 38290375
And to be clear the INTO TableB comes before the FROM TableA, as in:
SELECT  '1',
        [Column1],
        [Column2],
        [Column3]
INTO    TableB
FROM    TableA

Open in new window

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 9

Expert Comment

by:sarabhai
ID: 38290831
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.
0
 
LVL 9

Expert Comment

by:keyu
ID: 38290882
just one change with your query...

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

Expert Comment

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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have a large data set and a SSIS package. How can I load this file in multi threading?
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Suggested Courses

864 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