Solved

insert into SQL 2005 table

Posted on 2015-01-22
3
179 Views
Last Modified: 2015-01-22
Hello! I am executing the following insert query:

insert into table1 select * from table2

If I get no errors and it completes successfully, can I assume all the imported records came in unchanged from table1? I tried to import table2 into table1 but I go the Identity error since I have a rownumber field in table1. I don't care which table it is though as long as both tables are combined. Can I expect that my records combined without issue in table2?
0
Comment
Question by:mrosier
3 Comments
 
LVL 65

Expert Comment

by:Jim Horn
ID: 40565340
>can I assume all the imported records came in unchanged from table1?
yes

>I tried to import table2 into table1 but I go the Identity error since I have a rownumber field in table1
Then you're going to have to list all columns, except for the identity, that you're ok with new identity values.
Unfortunately there is no SELECT * (except the id column) syntax.

Using the * in an insert is not a best practice, as it is dependant on the schema of the two tables to be EXACTLY the same, and forces an insert on all columns.

>I don't care which table it is though as long as both tables are combined.
Ok.  Based on why you're asking another option is to create a view that UNIONs the two tables.
0
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 500 total points
ID: 40565455
If you're importing all columns from table2, you can use * there, you just have to list the columns for table1:

insert into table1 ( col1, col2, ... )
select * from table2


If table2 contains a numeric value you want to use for the rownumber, you can specify:
SET IDENTITY_INSERT 'dbo.table1' ON
and load the existing values from table1 instead of generating new identity/rownumber values.
0
 

Author Closing Comment

by:mrosier
ID: 40565658
this worked perfectly, thank you!!
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
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.

708 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

13 Experts available now in Live!

Get 1:1 Help Now