Which command would replace IDENTITY INSERT ON/OFF from SQLServer in Oracle?
Asked Answered
B

3

10

I have to migrate this query (simplified here) from T-SQL to ORACLE

SET IDENTITY_INSERT table ON

INSERT INTO table (id, value) VALUES (1, 2)

SET IDENTITY_INSERT table OFF

id being an Identity field in SQLServer.

I have the same table with a sequence in ORACLE, I couldn't find a snippet that shows how to disable the sequence and set it to start again with the MAX(id) + 1.

Any ORACLE expert can help me with this?

Thanks, Rodrigo.

Benitobenjamen answered 3/5, 2010 at 15:4 Comment(0)
M
7

You don't have to disable the identity in Oracle. Since you are using sequences, just don't use it for that insert.

That is, instead of

insert into table (id, values) values (table_seq.nextval, 2)

you use

insert into table (id, values) values (1, 2)

As to your second question about restarting the sequence, I think that is answered here in SO.

Metzler answered 3/5, 2010 at 15:13 Comment(2)
If you intend to keep the identity the same (which appears to be the case) this isn't a solution.Jeramyjerba
@Nate: You missed where it says "MAX(id) +1"Closefitting
P
1

Messing with columns populated by Oracle sequences in this way seems like a Bad Idea. In Oracle, you are typically maintaining a column populated via sequences with a trigger. If you start turning this feature on and off, and resetting the sequence ad lib, you run the risk of a sequence not being available when another process needs it, or getting reset to a value that has been used already but not committed.

Pappano answered 3/5, 2010 at 15:26 Comment(1)
Triggers are unnecessary as long as sequence.NEXTVAL is used in all INSERT statements for the table, which should really only happen in a single stored procedure anyway.Closefitting
J
0

Drop the sequences and re-create them when you're done with the max+1 value.

Jeramyjerba answered 3/5, 2010 at 15:41 Comment(2)
assuming this is a 1-time ETLJeramyjerba
This invalidates code that depends on that sequence and it drops all privileges in the process as well. If that's not a problem then this is the easiest option.Propylite

© 2022 - 2024 — McMap. All rights reserved.