Search code examples
oracle-databaseplsqldeveloperembedded-sql

How can implement a procedure in ESQL (an internal procedure) from Oracle database


I create a store procedure in Oracle db that insert Customer to my Table

this is my code :

CREATE OR REPLACE PROCEDURE THP.INSERT_CUSTOMER(

     P_CUSTNAME          IN       VARCHAR2,
     P_CUSTLAST          IN       VARCHAR2,
     P_CUSTFATHER        IN       VARCHAR2,
     P_NATIONNO          IN       NUMBER,
     P_BIRTHDAY          IN       VARCHAR2,
     P_BIRHTPLACE        IN       VARCHAR2,
     P_EMAIL             IN       VARCHAR2,
     P_CUSTENAME         IN       VARCHAR2,
     P_CUSTELAST         IN       VARCHAR2,
     P_OWNID             IN       NUMBER,
     P_CUSTTYPEID        IN       NUMBER,
     P_GENDERID          IN       NUMBER,
     P_BILLSTID          IN       NUMBER,
     P_BILLSPID          IN       NUMBER,
     P_IDNO              IN       varchar2,



     RESULT OUT INTEGER) IS
     CNT NUMBER;

BEGIN
     RESULT := 1;
     CNT := 0;
     SELECT COUNT(1) INTO CNT FROM THP.TBCUSTOMER WHERE NATIONNO=P_NATIONNO ;

     IF CNT=1 THEN  

         COMMIT;
         RESULT := 1;  --IF  RECORD is EXIST

     ELSE
        BEGIN  
            INSERT INTO TBCUSTOMER(CUSTID,CUSTNAME,CUSTLAST,CUSTFATHER,NATIONNO,BIRTHDAY,BIRHTPLACE,EMAIL,CUSTENAME,CUSTELAST,OWNID,CUSTTYPEID,GENDERID,BILLSTID,BILLSPID,IDNO)
            VALUES(CUSTID_SEQ.NEXTVAL,P_CUSTNAME,P_CUSTLAST,P_CUSTFATHER,P_NATIONNO,P_BIRTHDAY,P_BIRHTPLACE,P_EMAIL,P_CUSTENAME,P_CUSTELAST,P_OWNID,P_CUSTTYPEID,P_GENDERID,P_BILLSTID,P_BILLSPID,P_IDNO);
         COMMIT;
         RESULT :=0;  --IF INSERT NEW COLUMN

        END;  
     END IF;
END INSERT_CUSTOMER;
/

now I want use this procedure in ESQL and create it directly in ESQL not CALL it from Oracle database or other DB

would you please guide me a bout it...


Solution

  • General comments, not an answer ...

    count(1)
    

    count(1) = count(*), which is the standard form for "count the number of rows". count(1) has no advantages, so best to use count(*).

    RESULT := 1
    

    is redundant at the beginning of the procedure

    CNT := 0
    

    ... also redundant. The variable name is not very meaningful, and might make people think of a rude word, so perhaps change it to rows_found.

    Prefixing the arguments with P_ is not required. If you use one of them in a SQL statement and need to deconflict it from a database object name then prefix it with the procedure name, so you have:

    WHERE NATIONNO= INSERT_CUSTOMER.NATIONNO
    

    Is NATIONNO constrained to be unique in the customer table? If not, use:

    SELECT COUNT(*)
    INTO   CNT
    FROM   THP.TBCUSTOMER
    WHERE  NATIONNO=INSERT_CUSTOMER.NATIONNO AND
           ROWNUM = 1;
    

    (12g will introduce the LIMIT SQL syntax, by the way).

    Commiting in a procedure is often held to be bad practice, as the procedure often becomes part of a longer business transaction (eg. inserting a new customer and address) and the commit should be controlled by the application code.

    Upper case code is harder to read than lower case -- that's why direction signs on motorways are not uppercase.

    The begin-end block for the insert is not required at all.

    "birhtplace" is spelled wrong.

    So I'd suggest that what you want to convert to ESQL is actually:

    create or replace procedure thp.insert_customer(
        custname   in  varchar2,
        custlast   in  varchar2,
        custfather in  varchar2,
        nationno   in  number  ,
        birthday   in  varchar2,
        birhtplace in  varchar2,
        email      in  varchar2,
        custename  in  varchar2,
        custelast  in  varchar2,
        ownid      in  number  ,
        custtypeid in  number  ,
        genderid   in  number  ,
        billstid   in  number  ,
        billspid   in  number  ,
        idno       in  varchar2,
        result     out integer) is
    rows_found number;
    begin
        select count(*)
        into   rows_found
        from   thp.tbcustomer
        where  nationno=insert_customer.nationno;
    
        if rows_found = 1 then
            result := 1;
        else
            insert into
            tbcustomer(
                custid    ,
                custname  ,
                custlast  ,
                custfather,
                nationno  ,
                birthday  ,
                birthplace,
                email     ,
                custename ,
                custelast ,
                ownid     ,
                custtypeid,
                genderid  ,
                billstid  ,
                billspid  ,
                idno)
            values(
                custid_seq.nextval,
                custname  ,
                custlast  ,
                custfather,
                nationno  ,
                birthday  ,
                birthplace,
                email     ,
                custename ,
                custelast ,
                ownid     ,
                custtypeid,
                genderid  ,
                billstid  ,
                billspid  ,
                idno);
    
            result :=0;
         end if;
    end insert_customer;
    /