-1

我创建了一个存储过程

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”:开始函数包杂注过程子类型类型使用当前光标符号“开始”替换“插入”以继续.

2:错误(78,19):PLS-00103:在预期以下情况之一时遇到符号“文件结尾”:begin case declare end exception exit for goto if loop mod null pragma raise return select update while with < <关闭当前删除获取锁插入打开回滚保存点设置sql执行提交forall合并管道

这些是我的桌子

          --------------------------------------------------------
          --  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;
4

3 回答 3

2

问题出在这段代码中:

       P_failed_login_attempts     IN NUMBER )
    AS

   BEGIN  

  declare
         user_id_tmp number(20);     

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

删除并在和之间declare移动声明:user_idASBEGIN

       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.

-- some codePL/SQL 块的部分可以在其中包含更多块。事实上,这就是 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 编译器期待两个ENDs,但在找到第二个之前到达了您的存储过程的末尾。

于 2013-10-09T20:02:09.000 回答
0

我认为你应该删除DECLARE.

Oracle 11g 创建过程文档

于 2013-10-09T17:26:53.487 回答
0

以前的答案已经清除了编译错误,所以我不会解决这些问题。但是你有一个潜在的错误;插入“user_password”表中的 id 可能与“user”表中的不同 在多用户环境中,在您插入 set 之后但在执行 select max 之前,另一个用户可能插入并提交。这有望提高 dup-val-on-index,但很难找到。

您可以通过使用插入本身的返回选项来消除这种可能性,并通过获取触发器分配的 id 来稍微减少/清洁(?)代码:

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

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

于 2017-04-06T17:51:06.760 回答