Creating an SQL insert statement with both parameters and a select operation

I am trying to come up with a DB2 SQL statement that does the following, but I'm not sure if this is allowed.

I know that this is possible insert into tableA ( ... ) Values (?,?,?,...)
and then assign values ​​to these parameters ?.

Is it possible to pre-determine the value of one of the parameters?

For example, one of the columns I'm trying to insert is this ID column, and I would like to do something like select max(id) + 1 from tableA.

This is what I'm trying to get to - is this syntax possible in db2?

insert into tableA (ID, Text1, Text2) VALUES (select max(id)+1 from tableA, ?, ?)

In any case - any help would be appreciated!

thank!!

+3
source share
3 answers

this should work:

 insert into tableA values((select max(id)+1 from tableA),'text1','text')
+3
source

, , .

db2 alter table tableA add primary key (id)

, . (http://www.ibm.com/developerworks/data/library/techarticle/dm-0401melnyk/)

0

You can also try using the parameter OVERRIDING USER VALUE:

INSERT INTO TableA
OVERRIDING USER VALUE 
SELECT 0,Text1, Text2
From TableB 
0
source

All Articles