views:

368

answers:

2

I need to insert data into particular table through pl/sql stored procedure. My requirements are:

  • while inserting it should generate PRIMARY KEY values for a particular column;
  • it should return that PRIMARY KEY value to an output variable; and
  • for another column it should validate my string such that it should contain only characters, not integers.
A: 

In oracle I believe the "identity" column is best achieved with a sequence and an insert trigger that checks if the primary key columns is null and if so gets the next sequence and inserts it.

you can then use the "returning" clause to get the newly created primary key:

insert into <table> (<columns>) values (<values>) returning <prim_key> into <variable>;

the filtering of the string field I would personally handle in code before going to the database (if that is a possibility). Databases are notoriously inefficient at handling string operations.

Björn
Oracle handles any pl/sql operation very, very fast especially if you allow for native compilation in 10g and above.
Nick
+3  A: 

You can generate primary key values as a surrogate key using an Oracle SEQUENCE. You can create a constraint on a column that uses TRANSLATE to check that no numeric digits exist in newly inserted/updated data.

Some example code, suitable for SQL*Plus:

CREATE SEQUENCE mysequence;
/

CREATE TABLE mytable (
   pkidcol NUMBER PRIMARY KEY,
   stringcol VARCHAR2(100)
);
/

ALTER TABLE mytable ADD (
   CONSTRAINT stringnonnumeric
   CHECK (stringcol = TRANSLATE(stringcol,'A0123456789','A'))
);
/

DECLARE
   mystring mytable.stringcol%TYPE := 'Hello World';
   myid     mytable.pkidcol%TYPE;
BEGIN    
   INSERT INTO mytable (pkidcol, stringcol)
   VALUES (mysequence.NEXTVAL, mystring)
   RETURNING pkidcol INTO myid;
END;
/
Jeffrey Kemp
@Jeffrey, giving a +1. However, you should probably modify your answer just a bit, the translate won't tell him if the field has only characters, it just translates it. However, if you change it to REGEXP_INSTR and make sure it returns zero for any numeric character, I think it'll be a more robust answer.
Nick
Thanks Nick, but the only purpose for the `TRANSLATE` here is to implement the check constraint. It doesn't translate anything - it simply says "if any numbers here, don't allow the insert/update". If he wants the insert/update to succeed, he'll have to either implement the `TRANSLATE` in the application code, or perhaps add a trigger to modify it (but I wouldn't advise it).
Jeffrey Kemp