Unix Technical Forum

Yet another query question...

This is a discussion on Yet another query question... within the DB2 forums, part of the Database Server Software category; --> I am inserting a record into a table that has an identity column that does a GENERATED ALWAYS. It ...


Go Back   Unix Technical Forum > Database Server Software > DB2

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-27-2008, 01:06 PM
shorti
 
Posts: n/a
Default Yet another query question...

I am inserting a record into a table that has an identity column that
does a GENERATED ALWAYS. It is the only column in the table that
gives the record its uniqueness. I want to capture this number when I
insert a record. How can I do it in one step?

I found this query in the DB2 Info Center. It does some other things
but I tried to convert it to what I needed and couldnt get it to
work. Here is the Info Center query:

SELECT inorder.ordernum
FROM (INSERT INTO orders(custno)INCLUDE (insertnum integer)
VALUES(:cnum1, 1), (:cnum2, 2)) InsertedOrders
ORDER BY insertnum;

I tried :

SELECT t1.col1
FROM (INSERT INTO t1 (col2, col3, col4) VALUES (1, 2, 3))

I keep getting an SQL0104N An unexpected token.....Expected tokens may
include: "<space>" after the "SELECT t1.col1 FROM("

Any help would be appreciated.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-27-2008, 01:06 PM
Lennart
 
Posts: n/a
Default Re: Yet another query question...

On Sep 26, 1:42 am, shorti <lbrya...@juno.com> wrote:
> I am inserting a record into a table that has an identity column that
> does a GENERATED ALWAYS. It is the only column in the table that
> gives the record its uniqueness. I want to capture this number when I
> insert a record. How can I do it in one step?
>
> I found this query in the DB2 Info Center. It does some other things
> but I tried to convert it to what I needed and couldnt get it to
> work. Here is the Info Center query:
>
> SELECT inorder.ordernum
> FROM (INSERT INTO orders(custno)INCLUDE (insertnum integer)
> VALUES(:cnum1, 1), (:cnum2, 2)) InsertedOrders
> ORDER BY insertnum;
>


I'm not sure I undestand your sql, but something along the lines of:

SELECT InsertedOrders.ordernum
FROM new table (
INSERT INTO orders(custno)
VALUES(:cnum1), (:cnum2)
) InsertedOrders (ordernum, custno)
ORDER BY insertnum;


/Lennart


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-27-2008, 01:06 PM
Serge Rielau
 
Posts: n/a
Default Re: Yet another query question...

shorti wrote:
> I am inserting a record into a table that has an identity column that
> does a GENERATED ALWAYS. It is the only column in the table that
> gives the record its uniqueness. I want to capture this number when I
> insert a record. How can I do it in one step?

Here is a quick example showing SELECT FORM INSERT:

CREATE TABLE T(ID INT GENERATED ALWAYS AS IDENTITY,
C2 INT,
C3 INT GENERATED ALWAYS AS (c2 + 10),
c4 INT);

CREATE TRIGGER trg BEFORE INSERT ON T
REFERENCING NEW AS n FOR EACH ROW
SET c4 = c2 * 10;

SELECT id, c3, c4 FROM NEW TABLE(INSERT INTO T(c2) VALUES 5);
ID C3 C4
----------- ----------- -----------
1 15 50

1 record(s) selected.

--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-27-2008, 01:06 PM
shorti
 
Posts: n/a
Default Re: Yet another query question...

On Sep 26, 8:31 am, Serge Rielau <srie...@ca.ibm.com> wrote:
> shorti wrote:
> > I am inserting a record into a table that has an identity column that
> > does a GENERATED ALWAYS. It is the only column in the table that
> > gives the record its uniqueness. I want to capture this number when I
> > insert a record. How can I do it in one step?

>
> Here is a quick example showing SELECT FORM INSERT:
>
> CREATE TABLE T(ID INT GENERATED ALWAYS AS IDENTITY,
> C2 INT,
> C3 INT GENERATED ALWAYS AS (c2 + 10),
> c4 INT);
>
> CREATE TRIGGER trg BEFORE INSERT ON T
> REFERENCING NEW AS n FOR EACH ROW
> SET c4 = c2 * 10;
>
> SELECT id, c3, c4 FROM NEW TABLE(INSERT INTO T(c2) VALUES 5);
> ID C3 C4
> ----------- ----------- -----------
> 1 15 50
>
> 1 record(s) selected.
>
> --
> Serge Rielau
> DB2 Solutions Development
> IBM Toronto Lab


I have a couple of questions on this. What is the trigger for? I
tried the SELECT without the trigger and it works the same. I guess
it is unrelated. Also, FROM NEW TABLE is creating a new temporary
table called NEW?

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 09:08 AM.


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