473,406 Members | 2,371 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

table copy

GTi
I have two identical tables with one IDENTITY column and several other
columns.
I have tested the COPY * INTO table2 FROM table1 WHERE xx
but it requers that table2 does not exist.
TABLE2 is a history database of TABLE1
How can I copy row(s) from table1 to table2 without conflict with
IDENTITY columns using SQL query.
Or must I do this from a program (C# .NET)

Oct 12 '06 #1
4 5679
How can I copy row(s) from table1 to table2 without conflict with
IDENTITY columns using SQL query.
If you want to let SQL Server assign new identity values during the insert,
simply specify a column list and omit the identity column. For example:

INSERT INTO dbo.table2(MyColumn)
SELECT MyColumn
FROM dbo.table1

If you want to include the identity column values from the source table,
specify IDENTITY_INSERT and include the identity column in the column list:

SET IDENTITY_INSERT dbo.table2 ON

INSERT INTO dbo.table2(MyIdentityColumn, MyColumn)
SELECT MyIdentityColumn, MyColumn
FROM dbo.table1

--
Hope this helps.

Dan Guzman
SQL Server MVP
"GTi" <tu****@gmail.comwrote in message
news:11**********************@c28g2000cwb.googlegr oups.com...
>I have two identical tables with one IDENTITY column and several other
columns.
I have tested the COPY * INTO table2 FROM table1 WHERE xx
but it requers that table2 does not exist.
TABLE2 is a history database of TABLE1
How can I copy row(s) from table1 to table2 without conflict with
IDENTITY columns using SQL query.
Or must I do this from a program (C# .NET)
Oct 12 '06 #2
GTi
Dan Guzman wrote:
How can I copy row(s) from table1 to table2 without conflict with
IDENTITY columns using SQL query.

If you want to let SQL Server assign new identity values during the insert,
simply specify a column list and omit the identity column. For example:

INSERT INTO dbo.table2(MyColumn)
SELECT MyColumn
FROM dbo.table1

If you want to include the identity column values from the source table,
specify IDENTITY_INSERT and include the identity column in the column list:

SET IDENTITY_INSERT dbo.table2 ON

INSERT INTO dbo.table2(MyIdentityColumn, MyColumn)
SELECT MyIdentityColumn, MyColumn
FROM dbo.table1

--
Hope this helps.

Dan Guzman
SQL Server MVP
"GTi" <tu****@gmail.comwrote in message
news:11**********************@c28g2000cwb.googlegr oups.com...
I have two identical tables with one IDENTITY column and several other
columns.
I have tested the COPY * INTO table2 FROM table1 WHERE xx
but it requers that table2 does not exist.
TABLE2 is a history database of TABLE1
How can I copy row(s) from table1 to table2 without conflict with
IDENTITY columns using SQL query.
Or must I do this from a program (C# .NET)
Hope this helps.
Not directly, but u pointet me the directons.
My main problem is that I don't have full control over the colums, only
the basic ones.
Users may add new columns "on the run".
But as I now see it I do it in my c# .NET program. Retrieving all the
columns names and build the query you give me.

Thanks.

Oct 12 '06 #3
>>Users may add new columns "on the run".

Why?
That doesnt sound a good design

Madhivanan
GTi wrote:
Dan Guzman wrote:
How can I copy row(s) from table1 to table2 without conflict with
IDENTITY columns using SQL query.
If you want to let SQL Server assign new identity values during the insert,
simply specify a column list and omit the identity column. For example:

INSERT INTO dbo.table2(MyColumn)
SELECT MyColumn
FROM dbo.table1

If you want to include the identity column values from the source table,
specify IDENTITY_INSERT and include the identity column in the column list:

SET IDENTITY_INSERT dbo.table2 ON

INSERT INTO dbo.table2(MyIdentityColumn, MyColumn)
SELECT MyIdentityColumn, MyColumn
FROM dbo.table1

--
Hope this helps.

Dan Guzman
SQL Server MVP
"GTi" <tu****@gmail.comwrote in message
news:11**********************@c28g2000cwb.googlegr oups.com...
>I have two identical tables with one IDENTITY column and several other
columns.
I have tested the COPY * INTO table2 FROM table1 WHERE xx
but it requers that table2 does not exist.
TABLE2 is a history database of TABLE1
How can I copy row(s) from table1 to table2 without conflict with
IDENTITY columns using SQL query.
Or must I do this from a program (C# .NET)
>
Hope this helps.
Not directly, but u pointet me the directons.
My main problem is that I don't have full control over the colums, only
the basic ones.
Users may add new columns "on the run".
But as I now see it I do it in my c# .NET program. Retrieving all the
columns names and build the query you give me.

Thanks.
Oct 15 '06 #4
GTi
Madhivanan wrote:
>Users may add new columns "on the run".

Why?
That doesnt sound a good design

Madhivanan
After you have designed your tables and it have been deployed for some
time, have you never added more columns after updates?
Have you never wanted one more column on a 3. party table?
I always design my programs and tables so I or the customer can add
extra columns without program failure.

Oct 15 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: yurps | last post by:
Hello here is my html, if you click the missing image in the first column on the left, the div is shown, when clicked again the div disappears...but the bottom border disappears as well...Is there...
3
by: Claudio Lapidus | last post by:
Hello Now perhaps this is a bit dumb, but... I just populated a new table via \copy. After that, I realize that perhaps is a good thing to have a row identifier in it, so I try clapidus=>...
2
by: news.hp.com | last post by:
I have situation where I need to copy multiple records (only certain fields) from a Rules table to an Events table based on a selection identified in a combo box. When the selection is made in a...
21
by: Dan | last post by:
Hi, just ran into my first instance of a backend Access97 database not compacting. I'm getting the "MSACCESS.EXE has generated errors.." message on compact. I've narrowed it down to the largest...
3
by: red zebra | last post by:
The last days we had a lot of crasches using a application made in Access. there appears a screen : this isn't a ms datbase, database needs to be repaired. Repairing the database can't be done....
2
by: Marcin Zmyslowski | last post by:
Hello all! How to copy a table in MS SQL Server 2000 without chaning a structure? I mean, I have one table, which has autoincrement numeric field (ID). When I copy this table by exporting this...
4
by: Jim Devenish | last post by:
I wish to copy a table on a SQL Server backend to a table on an Access (.mdb) front end in the simplest way. I have the following to get the recordset but am seeking something easier. Dim...
4
by: ray well | last post by:
i need to copy in code a table from a legacy dbase III file into an access mdb file. both source and destination tables have the same table names, field names, field types, & field sizes. the...
5
by: mark_aok | last post by:
Hi all, I have a situation where I have a split database. At the back end, I need to - create a new table (I will call it newTable) with the exact fields, and relationships as another table...
9
by: fniles | last post by:
I would like to copy a table(s) from SQL Server 2005 to a CVS file and vice versa. I was thinking to use the BCP command line utility, but I have a few questions: 1. The machine where I am...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.