473,406 Members | 2,312 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.

Multiple INSERT

Hi,

I have to execute an insert like this:

INSERT INTO TAB1 (F1, F2)
SELECT (?), F2_T FROM TAB2
The field F1 of TAB1 is the primary key of my table and is not
auto-increment.
In TAB2 I have more than one record.
In stad of (?) i'd like to put an expression like: SELECT MAX(F1)+1
FROM TAB1

so:

INSERT INTO TAB1 (F1, F2)
SELECT (SELECT MAX(F1)+1 FROM TAB1), F2_T FROM TAB2

Unfortunatelly this don't wotks because the value of (SELECT MAX(F1)+1
FROM TAB1) is always the same, so i have an error of duplicate PK.

Onyone has an idea ?

thanks to all...
Nov 12 '05 #1
2 4023
grigno wrote:
Hi,

I have to execute an insert like this:

INSERT INTO TAB1 (F1, F2)
SELECT (?), F2_T FROM TAB2
The field F1 of TAB1 is the primary key of my table and is not
auto-increment.
In TAB2 I have more than one record.
In stad of (?) i'd like to put an expression like: SELECT MAX(F1)+1
FROM TAB1

so:

INSERT INTO TAB1 (F1, F2)
SELECT (SELECT MAX(F1)+1 FROM TAB1), F2_T FROM TAB2

Unfortunatelly this don't wotks because the value of (SELECT MAX(F1)+1
FROM TAB1) is always the same, so i have an error of duplicate PK.

Onyone has an idea ?

thanks to all...

CREATE SEQUENCE s;
INSERT INTO TAB1 (F1, F2)
SELECT (SELECT MAX(F1) FROM TAB1) + NEXT VALUE FOR s, F2_T
FROM TAB2;

Cheers
Serge

--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab
Nov 12 '05 #2
grigno wrote:
Hi,

I have to execute an insert like this:

INSERT INTO TAB1 (F1, F2)
SELECT (?), F2_T FROM TAB2
The field F1 of TAB1 is the primary key of my table and is not
auto-increment.
In TAB2 I have more than one record.
In stad of (?) i'd like to put an expression like: SELECT MAX(F1)+1
FROM TAB1

so:

INSERT INTO TAB1 (F1, F2)
SELECT (SELECT MAX(F1)+1 FROM TAB1), F2_T FROM TAB2

Unfortunatelly this don't wotks because the value of (SELECT MAX(F1)+1
FROM TAB1) is always the same, so i have an error of duplicate PK.


Untested:

INSERT INTO tab1(f1, f2)
SELECT ( SELECT MAX(f1)
FROM tab1 ) + rn,
f2_t
FROM ( SELECT row_number() over(), f2_t
FROM tab2 ) AS t(rn, f2_t)

--
Knut Stolze
Information Integration
IBM Germany / University of Jena
Nov 12 '05 #3

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

Similar topics

7
by: RotterdamStudents | last post by:
Hello there, i have a strange problem. I can't get php to insert multiple rows at once in a MySQL database. I use the $sql = "INSERT INTO database (a,b,c,d,e) VALUES ('$a', '$b' ,'$c', '$d',...
3
by: jason | last post by:
How does one loop through the contents of a form complicated by dynamic construction of checkboxes which are assigned a 'model' and 'listingID' to the NAME field on the fly in this syntax:...
10
by: shank | last post by:
I have a recordset that contains multiple records of product a user is purchasing. For clarity, I converted the recordset fields to variables. I need to take that entire recordset and insert it...
2
by: Joe | last post by:
Hey, I'm going to give some background on my situation in case anyone can point out a way around my problem altogether... for the problem itself, please skip to the bottom of the post. thanks....
1
by: John Smith | last post by:
I have a user assigned multiple roles and a role can be inherited from multiple parents (see below). How do I answer such questions as "How many roles does the user belongs to?" I answered the...
3
by: DarthMacgyver | last post by:
Hello, I recently wrote a survey application. Each question is very similar. The first questions gives me a problem when there are multiple people taking the survey (The Database connection...
5
by: Arsen V. | last post by:
Hello, What is the optimal way to insert multiple rows (around 1000) from a web application into a table? The user enters multiple lines into a text box (up to 10,000). The ASP.NET...
10
by: ads | last post by:
hi, after binding the dropdownlist to a datasource, ive experience this error "Cannot have multiple items selected in a dropdownlist" after using the code:...
2
by: Diego | last post by:
Hi everybody! I'm using DB2 PE v8.2.3 for linux. I've defined a database with the following schema: ANNOTATION(ID,AUTHOR,TEXT) ANNOTATION_BOOK(ANNOTATION_ID,OBJECT_ID)...
58
by: bonneylake | last post by:
Hey Everyone, Well recently i been inserting multiple fields for a section in my form called "serial". Well now i am trying to insert multiple fields for the not only the serial section but also...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...

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.