473,396 Members | 1,749 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,396 software developers and data experts.

Primary key generating

Help again please,
I need to insert rows into a table from another table. The tables are
identical column wise except the table im inserting from does not have
a primary key value. On insert I need to generate a primary key that
is consecutive based on the table im inserting into. Also the table im
inserting into does not have a identity column. Much appreciated.

Aug 24 '06 #1
7 2046

mu******@gmail.com wrote:
Help again please,
I need to insert rows into a table from another table. The tables are
identical column wise except the table im inserting from does not have
a primary key value. On insert I need to generate a primary key that
is consecutive based on the table im inserting into. Also the table im
inserting into does not have a identity column. Much appreciated.
Then create an auto-incrementing identity column in the destination
table and insert away.

Aug 24 '06 #2

ZeldorBlat wrote:
mu******@gmail.com wrote:
Help again please,
I need to insert rows into a table from another table. The tables are
identical column wise except the table im inserting from does not have
a primary key value. On insert I need to generate a primary key that
is consecutive based on the table im inserting into. Also the table im
inserting into does not have a identity column. Much appreciated.

Then create an auto-incrementing identity column in the destination
table and insert away.
Sorry, both tables don't have an identity column.

Aug 24 '06 #3

mutem...@gmail.com wrote:
ZeldorBlat wrote:
mu******@gmail.com wrote:
Help again please,
I need to insert rows into a table from another table. The tables are
identical column wise except the table im inserting from does not have
a primary key value. On insert I need to generate a primary key that
is consecutive based on the table im inserting into. Also the table im
inserting into does not have a identity column. Much appreciated.
Then create an auto-incrementing identity column in the destination
table and insert away.

Sorry, both tables don't have an identity column.
Allow me to reiterate my previous response: create an auto-incrementing
identity column in the destination table, then do an insert.

Aug 24 '06 #4
OK maybe I didn't explain it well enough
I have
TABLE1 TABLE2
First Last ID
First Last
John Smith 1
Lou Price
George Bluth 2
Henry Joe

What I WANT is

TABLE2
ID First Last
1 Lou Price
2 Henry Joe
3 John Smith
4 George Bluth
ZeldorBlat wrote:
mutem...@gmail.com wrote:
ZeldorBlat wrote:
mu******@gmail.com wrote:
Help again please,
I need to insert rows into a table from another table. The tables are
identical column wise except the table im inserting from does not have
a primary key value. On insert I need to generate a primary key that
is consecutive based on the table im inserting into. Also the table im
inserting into does not have a identity column. Much appreciated.
>
Then create an auto-incrementing identity column in the destination
table and insert away.
Sorry, both tables don't have an identity column.

Allow me to reiterate my previous response: create an auto-incrementing
identity column in the destination table, then do an insert.
Aug 24 '06 #5
SET NOCOUNT ON
go

CREATE TABLE Target(TargetID INT PRIMARY KEY, someData CHAR(1))
INSERT Target VALUES(1, 'A')
INSERT Target VALUES(2, 'B')
go
create table staging(someData CHAR(1))
INSERT staging VALUES('C')
INSERT staging VALUES('D')
go
SELECT IDENTITY(INT, 1,1) AS TargetID, someData
INTO #t FROM staging

INSERT Target
SELECT TargetID + (SELECT COALESCE(MAX(TargetID), 0) FROM Target),
someData
FROM #t

go
SELECT * FROM Target
go

TargetID someData
----------- --------
1 A
2 B
3 C
4 D
DROP TABLE Target
DROP TABLE Staging
DROP TABLE #t

Aug 24 '06 #6
Thanks! Worked great. Sorry for not understanding it earlier Zeldor :)
Alexander Kuznetsov wrote:
SET NOCOUNT ON
go

CREATE TABLE Target(TargetID INT PRIMARY KEY, someData CHAR(1))
INSERT Target VALUES(1, 'A')
INSERT Target VALUES(2, 'B')
go
create table staging(someData CHAR(1))
INSERT staging VALUES('C')
INSERT staging VALUES('D')
go
SELECT IDENTITY(INT, 1,1) AS TargetID, someData
INTO #t FROM staging

INSERT Target
SELECT TargetID + (SELECT COALESCE(MAX(TargetID), 0) FROM Target),
someData
FROM #t

go
SELECT * FROM Target
go

TargetID someData
----------- --------
1 A
2 B
3 C
4 D
DROP TABLE Target
DROP TABLE Staging
DROP TABLE #t
Aug 24 '06 #7
(mu******@gmail.com) writes:
I need to insert rows into a table from another table. The tables are
identical column wise except the table im inserting from does not have
a primary key value. On insert I need to generate a primary key that
is consecutive based on the table im inserting into. Also the table im
inserting into does not have a identity column. Much appreciated.
On SQL 2005:

INSERT target (ID, col2, col2, ....)
SELECT row_number OVER (ORDER BY <whatever you want to order by)>,
col1, col2, ...
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Aug 26 '06 #8

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

Similar topics

14
by: Sanjay Minni | last post by:
What is the datatype to be used for Primary Key columns for most optimised access given that - There will be a single column primary key only - The values will only be integers (but as...
13
by: Ron | last post by:
Hi all I'm deciding whether to use the PK also as an account number, invoice number, transaction number, etc that the user will see for the respective files. I understand that sometimes a...
7
by: sea | last post by:
Is it a good idea to programatically create a primary key? For example in a table called names, I have the following fields, (1) firstname (2)lastname (3) ID - will it be ok to create a primary...
4
by: ad | last post by:
The code below can only catch common excption. I want to catch duplicate primary key exeption and do something more. How can I catch duplicate primary key exeption try { Insert a record to...
2
by: Agnes | last post by:
I got a table which without primary key. case 1) I can process insertcommand and daMytable.update..etc case 2) However, if i process updatecommand , As I run daMytable.update .It returns error...
6
by: Irfan | last post by:
hi, when i add a row in a table of a dataset (using dt.rows.add), i expect the primary key value to be gernarated automatically as i have set it to Autonumber in access. But instead of...
3
by: Hugh O | last post by:
Hi, I am not sure if this type of question should be raised in this Newsgroup. If not please direct me. I am new to using RDO.Net data access but I thought I understood it. The 6 lines of...
8
by: Paul Hunter | last post by:
I am new to databases and thus to Access. I have a situation where I am trying to figure out how to key some tables I am working with. Consider that I have a database of my own records which are...
2
by: cloh | last post by:
Hi guys, I was wondering if there was a way to automatically scale either axis depending on which axis has the larger range. For example, if the primary axis automatically ranges between 10 and...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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,...

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.