Trigger To Add Sequence Number For Primary Key On Insert
842271Dec 27 2011 — edited Dec 29 2011I have Oracle 11gR2 x64 installed on my Win7 Enterprise x64 personal system.
I'm trying to convert the SQL Server 2008 TSQL statements to create a database for my C# class to work in Oracle. SQL Server 2008 has an Identity column, which automatically increments each time a record is inserted, but Oracle does not have an equivalent.
I created a sequence and a table and added a record, using the sequence:
create sequence seq_Customers
increment by 1
start with 1;
CREATE TABLE Customers(
CustomerID integer NOT NULL,
Name varchar(100) NOT NULL,
Address varchar(50) NOT NULL,
City varchar(20) NOT NULL,
State char(2) NOT NULL,
ZipCode char(15) NOT NULL,
CONSTRAINT PK_Customers PRIMARY KEY(CustomerID));
INSERT INTO Customers (CustomerID, Name, Address, City, State, ZipCode) VALUES (seq_Customers.NextVal, 'Molunguri, A', '1108 Johanna Bay Drive', 'Birmingham', 'AL', '35216-6909 ');
I created a trigger
create or replace trigger t1
before insert
on Customers
DECLARE
BEGIN
if inserting
then
insert into Customers (CustomerID)
values (seq_Customers.NextVal);
end if;
END;
to add the primary key during insert. The trigger compiles, but when I try to insert a Customers record with
INSERT INTO Customers (Name, Address, City, State, ZipCode) VALUES ('Molunguri, A', '1108 Johanna Bay Drive', 'Birmingham', 'AL', '35216-6909 ');
I get
Error starting at line 1 in command:
INSERT INTO Customers (Name, Address, City, State, ZipCode) VALUES ('Molunguri, A', '1108 Johanna Bay Drive', 'Birmingham', 'AL', '35216-6909 ')
Error report:
SQL Error: ORA-00036: maximum number of recursive SQL levels (50) exceeded
ORA-06512: at "SYSMAN.T1", line 5
ORA-04088: error during execution of trigger 'SYSMAN.T1'
ORA-06512: at "SYSMAN.T1", line 5
ORA-04088: error during execution of trigger 'SYSMAN.T1'
ORA-06512: at "SYSMAN.T1", line 5
ORA-04088: error during execution of trigger 'SYSMAN.T1'
ORA-06512: at "SYSMAN.T1", line 5
ORA-04088: error during execution of trigger 'SYSMAN.T1'
ORA-06512: at "SYSMAN.T1", line 5
ORA-04088: error during execution of trigger 'SYSMAN.T1'
ORA-06512: at "SYSMAN.T1", line 5
ORA-04088: error during execution of trigger 'SYSMAN.T1'
ORA-06512: at "SYSMAN.T1", line 5
ORA-04088: error during execution of trigger 'SYSMAN.T1'
ORA-06512: at "SYSMAN.T1", line 5
ORA-04088: error during execution of trigger 'SYSMAN.T1'
ORA-06512: at "SYSMAN.T1", line 5
ORA-04088: error during execution of trigger 'SYSMAN.T1'
ORA-06512: at "SYSMAN.T1", line 5
ORA-04088: error during execution of trigger 'SYSMAN.T1'
ORA-06512: at "SYSMAN.T1", line 5
ORA-04088: error during execution of trigger 'SYSMAN.T1'
ORA-06512: at "SYSMAN.T1", line 5
ORA-04088: error during execution of trigger 'SYSMAN.T1'
ORA-06512: at "SYSMAN.T1", line 5
ORA-04088: error during execution of trigger 'SYSMAN.T1'
ORA-06512: at "SYSMAN.T1", line 5
ORA-04088: error during execution of trigger 'SYSMAN.T1'
ORA-06512: at "SYSMAN.T1", line 5
ORA-04088: error during execution of trigger 'SYSMAN.T1'
ORA-06512: at "SYSMAN.T1", line 5
00036. 00000 - "maximum number of recursive SQL levels (%s) exceeded"
*Cause: An attempt was made to go more than the specified number
of recursive SQL levels.
*Action: Remove the recursive SQL, possibly a recursive trigger.
T1 is the only trigger I've created.
I've searched this forum and I've Read The Friendly Manual. I thought writing this trigger would be simple, but I guess not.
Suggestions?
Thanks.