Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
169 views
in Technique[技术] by (71.8m points)

Procedure to check for non duplicate rows in table before insert (Oracle)

I wrote a procedure in oracle(using guidance from here) to check for any duplicate values in table so that countries with the same countryId should return some error message as countryId is PK in countries table.Here it is:

   CREATE OR REPLACE PROCEDURE add_values4 (c_cntry_id IN OUT COUNTRIES.COUNTRY_ID%TYPE,
                                       c_cntr_name IN COUNTRIES.COUNTRY_NAME%TYPE, 
                                       c_rgn_id IN COUNTRIES.REGION_ID%TYPE)
IS
DECLARE
   outputValue CHAR  := 'JJ';
BEGIN
  INSERT INTO countries(COUNTRY_ID, COUNTRY_NAME,REGION_ID)
    values (user_seq.nextval, c_cntr_name,c_rgn_id);
  c_cntry_id := user_seq.currval;
EXCEPTION
  WHEN dup_val_on_index
  THEN 
    c_cntry_id := null;
END;
/

However,when I try to create this procedure,it gives me error PLS-00103.Any idea what could be wrong? (Anything in syntax?).I have been trying since 48 hours but with not much success.Appreciate some help here.

Tx in advance

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have a semicolon after your parameter block, and you are missing your IS or AS clause:

CREATE OR REPLACE PROCEDURE add_vals (c_cntry_id OUT COUNTRIES.COUNTRY_ID%TYPE,
                                       c_cntr_name IN COUNTRIES.COUNTRY_NAME%TYPE, 
                                       c_rgn_id IN COUNTRIES.REGION_ID%TYPE)
IS
BEGIN
  INSERT INTO countries(COUNTRY_ID, COUNTRY_NAME,REGION_ID)
    values (user_seq.nextval, c_cntr_name,c_rgn_id);
  c_cntry_id := user_seq.currval;
EXCEPTION
  WHEN dup_val_on_index
  THEN 
    c_cntry_id := null;
END;
/

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...