views:

146

answers:

1

Hello world.

Using MyGeneration, doodads, and Oracle XE, is it possible to implement an "auto number primary key" scheme?

Problem facts: I am using Oracle XE. I have implemented the following table and trigger:

CREATE TABLE  "USERS" 
(   
    "ID" NUMBER(38,0), 
    "USER_NAME" VARCHAR2(50), 
    "PASSWORD" VARCHAR2(50), 
    "EMAIL" VARCHAR2(100), 
     CONSTRAINT "USERS_PK" PRIMARY KEY ("ID") ENABLE
)
/

CREATE OR REPLACE TRIGGER  "BI_USERS" 
  before insert on "USERS"               
  for each row  
begin   
    select "USERS_SEQ".nextval into :NEW.ID from dual; 
end; 

/
ALTER TRIGGER  "BI_USERS" ENABLE
/

MyGeneration / Doodads created the following stored proc...

CREATE OR REPLACE PROCEDURE "XXX"."PI_USERS"
(
    p_ID IN USERS.ID%type,
    p_USER_NAME IN USERS.USER_NAME%type,
    p_PASSWORD IN USERS.PASSWORD%type,
    p_EMAIL IN USERS.EMAIL%type
)
IS
BEGIN


    INSERT
    INTO USERS
    (
        ID,
        USER_NAME,
        PASSWORD,
        EMAIL
    )
    VALUES
    (
        p_ID,
        p_USER_NAME,
        p_PASSWORD,
        p_EMAIL
    );
END PI_USERS;

The sequence and trigger combination is working fine. The BusinessEntity class in C# does not receive the new ID.

Any recommended ways to allow the calling code receive the new record ID?

+1  A: 

I haven't used Doodads, so not sure if this is what it is expecting, but if you change the procedure like below using the returning clause and make the p_ID parameter in out the p_ID parameter should hold the newly added ID after it is executed.

CREATE OR REPLACE PROCEDURE "XXX"."PI_USERS"
(
    p_ID IN OUT USERS.ID%type,
    p_USER_NAME IN USERS.USER_NAME%type,
    p_PASSWORD IN USERS.PASSWORD%type,
    p_EMAIL IN USERS.EMAIL%type
)
IS
BEGIN


    INSERT
    INTO USERS
    (
        ID,
        USER_NAME,
        PASSWORD,
        EMAIL
    )
    VALUES
    (
        p_ID,
        p_USER_NAME,
        p_PASSWORD,
        p_EMAIL
    )
    RETURNING ID INTO p_ID;

END PI_USERS;
Dougman
VERY VERY COOL... We used your recommended change to the stored procs... It worked!On the C# side, we will be adjusting the generator to create the following change... private IDbCommand CreateParameters(OracleCommand cmd) { OracleParameter p; p = cmd.Parameters.Add(Parameters.ID); p.SourceColumn = ColumnNames.ID; p.SourceVersion = DataRowVersion.Current; p.Direction = ParameterDirection.InputOutput;
Michael Rosario