Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8956929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:52:29+00:00 2026-06-15T14:52:29+00:00

I have this working code. Every time I ran INSERT INTO HR.CITY (CITY_ID) VALUES

  • 0

I have this working code. Every time I ran INSERT INTO "HR"."CITY" (CITY_ID) VALUES (0); the data in CITY_ID will increase by one like this CT0001, CT0002 ... CT0015.

 CREATE TABLE "HR"."CITY" 
   (    
    "CITY_ID" VARCHAR2(40 BYTE)
   ) ;


  CREATE OR REPLACE TRIGGER "HR"."PK_MAX_TRIGGER_CITY" 
BEFORE INSERT ON CITY
FOR EACH ROW
DECLARE
    CNT NUMBER;
    PKV CITY.CITY_ID%TYPE;
    NO NUMBER;
BEGIN
    SELECT COUNT(*)INTO CNT FROM CITY;

    IF CNT=0 THEN
        PKV:='CT0001';
    ELSE
        SELECT 'CT'||LPAD(MAX(TO_NUMBER(SUBSTR(CITY_ID,3,LENGTH(CITY_ID)))+1),4,'0') INTO PKV
        FROM CITY;
    END IF;
    :NEW.CITY_ID:=PKV;
END;
/
ALTER TRIGGER "HR"."PK_MAX_TRIGGER_CITY" ENABLE;

What I want to do is this E1, E2 ... E15.

I change the code to:

    IF CNT=0 THEN
        PKV:='E1';
    ELSE
        SELECT 'E'||LPAD(MAX(TO_NUMBER(SUBSTR(CITY_ID,3,LENGTH(CITY_ID)))+1),4,'0') INTO PKV

It work at first adding E1 to database but not the second time. What inside the bracket is just to hard for me to digest. I was hoping anyone here could explain to me what actually happen in the bracket and helping me to solve this thing.

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T14:52:30+00:00Added an answer on June 15, 2026 at 2:52 pm

    What you need to do is to modify your Else part query .

    SELECT 'E'||(MAX(TO_NUMBER(SUBSTR(CITY_ID,2))+1)) INTO PKV
        FROM CITY;
    

    Explaination
    Whenever you want to find what is happening in the brackets start with the inner bracket .

    See the result of this query:  SELECT SUBSTR(CITY_ID,2) FROM CITY;
    

    Here,you will extract the string after E,where E always there in 1st position ,so we will find the string from the 2nd position .
    Then

     SELECT TO_NUMBER(SUBSTR(CITY_ID,2)) FROM CITY ;
    

    –this is converting the string into number.Then it will find the max number from the table CITY,and add 1 after that

    SELECT MAX(TO_NUMBER(SUBSTR(CITY_ID,2)))+1 FROM CITY ;
    

    And finally appending the constant E with the result .

    SELECT 'E'||(MAX(TO_NUMBER(SUBSTR(CITY_ID,2))+1)) INTO PKV
        FROM CITY;
    

    But take my suggestion ,create a sequence city_id_seq,this will be better solution than what you are doing now.

    CREATE SEQUENCE city_id_seq
    MINVALUE 1
    MAXVALUE 99999999999
    START WITH 1
    INCREMENT BY 1
    CACHE 20;
    
    
    CREATE OR REPLACE TRIGGER "HR"."PK_MAX_TRIGGER_CITY" 
    BEFORE INSERT ON CITY
    FOR EACH ROW
    DECLARE
     v_city_id PLS_INTEGER;  
    BEGIN
      SELECT city_id_seq.nextval INTO v_city_id  FROM DUAL;
    
     :NEW.CITY_ID :='E'||v_city_id  ;
    
    /*  if you are using oracle 11g 
    :NEW.CITY_ID:='E'||city_id_seq.nextval; 
    */
    
    END;
    /
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code I am working on but every time I call init
I have this working code, but now i need to be able to change
I have this (working) CPU code: #define NF 3 int ND; typedef double (*POT)(double
As a beginner in Ocaml, I have this current working code: ... let ch_in
I have this code working in C#: var request = (HttpWebRequest)WebRequest.Create(https://x.com/service); request.Method = GET;
I used to have this code working with my Tomcat server: HttpRequestBase targetRequest =
this seems to be weird but it really happened. I have this code working
I have this JQuery code working however I need to know whether how to
i have this code and its working but i want it to return dailyTotals
I have this set of code which was working fine about an hour or

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.