Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Using Sequence or Trigger to Enter PK IDs into Lookup Table

Dj SteeleDec 26 2017 — edited Jan 3 2018

Hello Forum,

I am looking to Create a Lookup Table (To Create a Select Drop Down List) and was wondering how to enter the Data using a Sequence or Sequence with Combination of Trigger

I got it to work with Creating a Function and Combining a Created Sequence but was trying to accomplish with Sequence and Trigger

**************************************************************************************************************************************************

I get the following order trying to use seq.nextval ( I did get it to work Below utilizing Function and Sequence)

pastedImage_10.png

************************************************************************************************************************************************

I get the following order trying to use seq.nextval

CREATE TABLE StateLookup_2

(

StateID Number,

StateName NVARCHAR2 (32),

StateAbbrev CHAR (2)

);

ALTER TABLE StateLookup_2 ADD (

CONSTRAINT StateID_2_pk PRIMARY KEY (StateID));

Create Sequence StateID__2_Seq

Start With 1

Increment by 1

NoCache

NoCycle

INSERT ALL

INTO StateLookup_2 (StateID, StateName, StateAbbrev) VALUES (StateID__2_Seq.nextval,'Alabama', 'AL')

INTO StateLookup_2 (StateID, StateName, StateAbbrev) VALUES (StateID__2_Seq.nextval,'Alaska', 'AK')

INTO StateLookup_2 (StateID, StateName, StateAbbrev) VALUES (StateID__2_Seq.nextval,'Arizona', 'AZ')

INTO StateLookup_2 (StateID, StateName, StateAbbrev) VALUES (StateID__2_Seq.nextval,'Arkansas', 'AR')

INTO StateLookup_2 (StateID, StateName, StateAbbrev) VALUES (StateID__2_Seq.nextval,'California', 'CA')

INTO StateLookup_2 (StateID, StateName, StateAbbrev) VALUES (StateID__2_Seq.nextval,'Colorado', 'CO')

INTO StateLookup_2 (StateID, StateName, StateAbbrev) VALUES (StateID__2_Seq.nextval,'Connecticut', 'CT')

Select * from dual;

**************************************************************************************************************************************

I did get it to work utilizing Function and Sequence

**************************************************************************************************************************************

This Works

**************************************************************************************************************************************************

CREATE TABLE StateLookup

(

StateID Number,

StateName NVARCHAR2 (32),

StateAbbrev CHAR (2)

);

ALTER TABLE StateLookup ADD (

CONSTRAINT StateID_pk PRIMARY KEY (StateID));

Create Function Get_StateID_Seq

Return Number IS

Begin

Return StateID_seq.NextVal;

End;

/

Create Sequence StateID_Seq

Start With 1

Increment by 1

NoCache

NoCycle

INSERT ALL

INTO StateLookup (StateID, StateName, StateAbbrev) VALUES (Get_StateID_Seq,'Alabama', 'AL')

INTO StateLookup (StateID, StateName, StateAbbrev) VALUES (Get_StateID_Seq,'Alaska', 'AK')

INTO StateLookup (StateID, StateName, StateAbbrev) VALUES (Get_StateID_Seq,'Arizona', 'AZ')

INTO StateLookup (StateID, StateName, StateAbbrev) VALUES (Get_StateID_Seq,'Arkansas', 'AR')

INTO StateLookup (StateID, StateName, StateAbbrev) VALUES (Get_StateID_Seq,'California', 'CA')

INTO StateLookup (StateID, StateName, StateAbbrev) VALUES (Get_StateID_Seq,'Colorado', 'CO')

INTO StateLookup (StateID, StateName, StateAbbrev) VALUES (Get_StateID_Seq,'Connecticut', 'CT')

INTO StateLookup (StateID, StateName, StateAbbrev) VALUES (Get_StateID_Seq,'Delaware', 'DE')

INTO StateLookup (StateID, StateName, StateAbbrev) VALUES (Get_StateID_Seq,'District of Columbia', 'DC'

etc........)

Select * from Dual;

This post has been answered by Ahmed Haroon on Dec 26 2017
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 31 2018
Added on Dec 26 2017
14 comments
727 views