Unix Technical Forum

Multiple INSERT

This is a discussion on Multiple INSERT within the DB2 forums, part of the Database Server Software category; --> Hi, I have to execute an insert like this: INSERT INTO TAB1 (F1, F2) SELECT (?), F2_T FROM TAB2 ...


Go Back   Unix Technical Forum > Database Server Software > DB2

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-27-2008, 04:09 AM
grigno
 
Posts: n/a
Default 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...
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-27-2008, 04:09 AM
Serge Rielau
 
Posts: n/a
Default Re: Multiple INSERT

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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-27-2008, 04:14 AM
Knut Stolze
 
Posts: n/a
Default Re: Multiple INSERT

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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 08:55 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com