简体   繁体   中英

rownumber in insert into

I wanted to insert the row number in one of the column. But since row_number() is a window ( analytic proper to Oracle ) function which can be used only for select statement is there any other way to insert row number.

eg:

INSERT INTO prnr (pm,GEG_ID,ET_ID,fzg_id,nr,id)
VALUES(p_gpm_id,master_rec.geg_id,master_rec.et_id,
       master_rec.fzg_id,row_number() over (partition by master_rec.geg_id));

i want to use this insert statement in a procudure.

Try refactoring your code to an INSERT INTO... SELECT :

INSERT INTO prnr (pm, GEG_ID, ET_ID, fzg_id, nr, id)
SELECT p_gpm_id, geg_id, et_id, fzg_id, ROW_NUMBER() OVER (PARTITION BY geg_id),
       -- some 6th column here...
FROM master_rec;

If your aim is just simply generating a sequential integer value( as described within the comment ), for the id column, then recreate existing id column with GENERATED ALWAYS AS IDENTITY option such as

ALTER TABLE prnr DROP COLUMN id;
ALTER TABLE prnr ADD id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM