创建存储过程给出错误

时间:2013-10-09 17:03:39

标签: plsql

我创建了一个存储过程

create or replace
   PROCEDURE "USP_USER_ADD" (
       USERNAME                  IN VARCHAR2 ,
       P_PASSWORD                  IN VARCHAR2 ,
       SALT                      IN BLOB ,
       EMAIL                     IN VARCHAR2 ,
       FIRST_NAME                IN VARCHAR2 ,
       LAST_NAME                 IN VARCHAR2 ,
       ip_address                IN VARCHAR2 ,
       EMAIL_VERIFIED            IN NUMBER ,
       ACTIVE                    IN NUMBER ,
       CREATEDBY                 IN VARCHAR2 ,
       CREATED                   IN DATE ,
       MODIFIED                  IN DATE ,
       MODIFIEDBY                IN VARCHAR2 ,
       USER_GROUP_ID             IN NUMBER ,
       LAST_PASSWORD_CHANGE_DATE IN DATE ,
       P_failed_login_attempts     IN NUMBER )
    AS

   BEGIN  

  declare
         user_id_tmp number(20);     

INSERT INTO users( "username" ,
    "password" ,
    "salt" ,
    "email" ,
    "first_name" ,
    "last_name" ,
    "email_verified" ,
    "active" ,
    "ip_address" ,
    "created" ,
    "createdby" ,
    "modified" ,
    "modifiedby" ,
    "user_group_id" ,
    "last_password_change_date" ,
    "FAILED_LOGIN_ATTEMPTS"
  )
  VALUES
  (
    username ,
    p_password ,
    salt ,
    email ,
    first_name ,
    last_name ,
    email_verified ,
    active ,
    ip_address ,
    created ,
    createdby ,
    modified ,
    modifiedby ,
    user_group_id ,
    last_password_change_date ,
    p_failed_login_attempts
  );      

SELECT MAX(id) INTO user_id_tmp FROM users ;

INSERT INTO user_passwords
  (
    "user_id" ,
    "password" ,
    "created"
  )
  VALUES
  (
    user_id_tmp,
    p_password,
    created
  );

  END USP_USER_ADD;

它给了我两个错误

  

1:错误(26,5):PLS-00103:遇到以下其中一项时遇到符号“INSERT”:begin function package pragma procedure子类型使用表单当前游标符号“begin”替换为“INSERT” “ 接着说。

     

2:错误(78,19):PLS-00103:当遇到以下情况之一时遇到符号“文件结束”:如果循环mod为空pragma raise返回选择更新,则为goto开始大小写声明结束异常退出与<< close current delete fetch lock insert open rollback savepoint set sql execute commit forall merge pipe

这些是我的表

          --------------------------------------------------------
          --  DDL for Table USER_PASSWORDS
          --------------------------------------------------------

            CREATE TABLE "NEWS1.0"."USER_PASSWORDS" 
             (  "ID" NUMBER(11,0), 
            "USER_ID" NUMBER(11,0), 
            "PASSWORD" VARCHAR2(255 BYTE), 
            "SALT" VARCHAR2(255 BYTE), 
            "IP" VARCHAR2(15 BYTE), 
            "CREATEDBY" VARCHAR2(255 BYTE), 
            "CREATED" DATE, 
            "MODIFIED" DATE, 
            "MODIFIEDBY" VARCHAR2(255 BYTE)
             ) PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING
            STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
            PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)
            TABLESPACE "USERS" ;
          --------------------------------------------------------
          --  DDL for Index USERS_PK
          --------------------------------------------------------

            CREATE UNIQUE INDEX "NEWS1.0"."USERS_PK" ON "NEWS1.0"."USER_PASSWORDS" 
             ("ID") 
            PCTFREE 10 INITRANS 2 MAXTRANS 255 COMPUTE STATISTICS 
            STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
            PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)
            TABLESPACE "USERS" ;
          --------------------------------------------------------
          --  Constraints for Table USER_PASSWORDS
          --------------------------------------------------------

            ALTER TABLE "NEWS1.0"."USER_PASSWORDS" MODIFY ("ID" NOT NULL ENABLE);

            ALTER TABLE "NEWS1.0"."USER_PASSWORDS" MODIFY ("USER_ID" NOT NULL ENABLE);

            ALTER TABLE "NEWS1.0"."USER_PASSWORDS" MODIFY ("PASSWORD" NOT NULL ENABLE);

            ALTER TABLE "NEWS1.0"."USER_PASSWORDS" ADD CONSTRAINT "USERS_PK" PRIMARY       
            KEY ("ID")
            USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 COMPUTE STATISTICS 
            STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
            PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)
            TABLESPACE "USERS"  ENABLE;

          --------------------------------------------------------
          --  DDL for Trigger BI_USER_PASSWORDS_ID
          --------------------------------------------------------

            CREATE OR REPLACE TRIGGER "NEWS1.0"."BI_USER_PASSWORDS_ID" 
             before insert on "USER_PASSWORDS" 
             for each row 
          begin  
             if inserting then 
                if :NEW."ID" is null then 
                   select USER_PASSWORDS_SEQ.nextval into :NEW."ID" from dual; 
                end if; 
             end if; 
          end;
          /
          ALTER TRIGGER "NEWS1.0"."BI_USER_PASSWORDS_ID" ENABLE;





          --------------------------------------------------------
          --  DDL for Table USERS
          --------------------------------------------------------

            CREATE TABLE "NEWS1.0"."USERS" 
             (  "ID" NUMBER(*,0), 
            "USERNAME" VARCHAR2(100 BYTE), 
            "PASSWORD" VARCHAR2(255 BYTE), 
            "SALT" BLOB, 
            "EMAIL" VARCHAR2(100 BYTE), 
            "FIRST_NAME" VARCHAR2(100 BYTE), 
            "LAST_NAME" VARCHAR2(100 BYTE), 
            "EMAIL_VERIFIED" NUMBER(*,0) DEFAULT 1, 
            "ACTIVE" NUMBER(*,0) DEFAULT 1, 
            "IP_ADDRESS" VARCHAR2(50 BYTE), 
            "USER_GROUP_ID" NUMBER(*,0), 
            "LAST_PASSWORD_CHANGE_DATE" DATE, 
            "FAILED_LOGIN_ATTEMPTS" NUMBER(*,0), 
            "CREATED" DATE, 
            "CREATEDBY" VARCHAR2(255 BYTE), 
            "MODIFIED" DATE, 
            "MODIFIEDBY" VARCHAR2(255 BYTE)
             ) PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING
            STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
            PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)
            TABLESPACE "USERS" 
           LOB ("SALT") STORE AS (
            TABLESPACE "USERS" ENABLE STORAGE IN ROW CHUNK 8192 PCTVERSION 10
            NOCACHE LOGGING 
            STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
            PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)) ;
          --------------------------------------------------------
          --  DDL for Index USERS_UK2
          --------------------------------------------------------

            CREATE UNIQUE INDEX "NEWS1.0"."USERS_UK2" ON "NEWS1.0"."USERS" ("EMAIL") 
            PCTFREE 10 INITRANS 2 MAXTRANS 255 COMPUTE STATISTICS 
            STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
            PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)
            TABLESPACE "USERS" ;
          --------------------------------------------------------
          --  DDL for Index USERS_UK1
          --------------------------------------------------------

            CREATE UNIQUE INDEX "NEWS1.0"."USERS_UK1" ON "NEWS1.0"."USERS" ("USERNAME") 
            PCTFREE 10 INITRANS 2 MAXTRANS 255 COMPUTE STATISTICS 
            STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
            PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)
            TABLESPACE "USERS" ;
          --------------------------------------------------------
          --  Constraints for Table USERS
          --------------------------------------------------------

            ALTER TABLE "NEWS1.0"."USERS" MODIFY ("ID" NOT NULL ENABLE);

            ALTER TABLE "NEWS1.0"."USERS" MODIFY ("USERNAME" NOT NULL ENABLE);

            ALTER TABLE "NEWS1.0"."USERS" MODIFY ("PASSWORD" NOT NULL ENABLE);

            ALTER TABLE "NEWS1.0"."USERS" ADD CONSTRAINT "USERS_UK1" UNIQUE 
           ("USERNAME")
            USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 COMPUTE STATISTICS 
            STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
            PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)
            TABLESPACE "USERS"  ENABLE;

            ALTER TABLE "NEWS1.0"."USERS" ADD CONSTRAINT "USERS_UK2" UNIQUE ("EMAIL")
            USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 COMPUTE STATISTICS 
            STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
            PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)
            TABLESPACE "USERS"  ENABLE;

3 个答案:

答案 0 :(得分:2)

问题出在这段代码中:

       P_failed_login_attempts     IN NUMBER )
    AS

   BEGIN  

  declare
         user_id_tmp number(20);     

INSERT INTO users( "username" ,
   -- rest omitted

移除declare并在user_idAS之间移动BEGIN声明:

       P_failed_login_attempts     IN NUMBER )
    AS
         user_id_tmp number(20);     

   BEGIN  

INSERT INTO users( "username" ,
   -- rest omitted

在Oracle PL / SQL中,的格式为

DECLARE
  -- some variable declarations
BEGIN
  -- some code
EXCEPTION
  -- some exception-handling
END;

变量声明和异常处理部分是可选的。如果没有变量声明,则可以删除关键字DECLARE(如果将其保留在那里则不是错误)。但是,如果该块没有异常处理,则必须删除EXCEPTION关键字。

在声明程序或功能时,CREATE OR REPLACE PROCEDURE ... AS部分代替DECLARE

PL / SQL块的-- some code部分可以在其中包含更多块。事实上,这是PL / SQL编译器在看到您的declare关键字时所想要的。它以为你做的事情如下:

CREATE OR REPLACE PROCEDURE USP_USER_ADD (
  -- parameters omitted
)
AS
BEGIN
  DECLARE
    -- some variable declarations
  BEGIN
    -- some code
  END;
END USP_USER_ADD;

但是,您在INSERT之后有一个declare。编译器没有预料到这一点,这就是你遇到错误的原因。你还得到一个关于文件结束的错误,那是因为PL / SQL编译器需要两个END,但在找到第二个存储过程之前就已经到了存储过程的末尾。

答案 1 :(得分:0)

我认为你应该删除DECLARE

Oracle 11g Create Procedure Documentation

答案 2 :(得分:0)

以前的答案已经清除了编译错误,所以我不会解决这些错误。但是你有一个潜在的错误; id插入" user_password"表格可能与" user"中的表格不同table在多用户环境中,在插入set之后但在执行select max之前,其他用户可以插入并提交。这有望提高指数重复率,但很难找到。

你可以通过使用插件本身的return选项来消除这种可能性,并通过获取触发器指定的id来减少/清除(?)代码吗?)

插入用户(...)值(...)将id返回到user_id_tmp;

并删除select max(id)...语句。