4/3 PL/SQL: SQL Statement ignored 8/5 PL/SQL: ORA-00904: "DBMS_CRYPTO"."CHAIN_IV": invalid identifier 8/17 PLS-00302: component 'CHAIN_IV' must be declared SQL> CREATE OR REPLACE PROCEDURE ENCRYPT_PRICE AS 2 C_KEY VARCHAR2(32) := 'MY_SECRET_KEY_FOR_AES256'; 3 BEGIN 4 UPDATE PRODUCT SET PRICE = DBMS_CRYPTO.ENCRYPT( 5 UTL_I18N.STRING_TO_RAW(TO_CHAR(PRICE), 'AL32UTF8'), 6 DBMS_CRYPTO.ENCRYPT_AES256 + DBMS_CRYPTO.CHAIN_CBC + DBMS_CRYPTO.PAD_PKCS5, 7 UTL_I18N.STRING_TO_RAW(C_KEY, 'AL32UTF8'), 8 NULL 9 ) WHERE PRICE IS NOT NULL; 10 END; 11 / Warning: Procedure created with compilation errors. SQL> SHOW ERRORS; Errors for PROCEDURE ENCRYPT_PRICE: LINE/COL ERROR -------- ----------------------------------------------------------------- 4/3 PL/SQL: SQL Statement ignored 4/42 PL/SQL: ORA-00932: inconsistent datatypes: expected NUMBER got BINARY给参数
时间: 2023-08-15 12:13:47 浏览: 169
根据错误信息,可以看出问题出在 UPDATE 语句中的 `DBMS_CRYPTO.ENCRYPT` 函数上,因为它需要的参数类型与 `PRICE` 的数据类型不一致。
这是因为 `TO_CHAR` 函数将 `PRICE` 转换为文本字符串,而 `DBMS_CRYPTO.ENCRYPT` 函数需要的是二进制数据。为了解决这个问题,我们可以在 `TO_CHAR` 函数之前使用 `CAST` 函数将 `PRICE` 转换为二进制数据类型 `RAW`,然后再将其转换为文本字符串。
修改后的代码如下:
```
CREATE OR REPLACE PROCEDURE ENCRYPT_PRICE AS
C_KEY VARCHAR2(32) := 'MY_SECRET_KEY_FOR_AES256';
BEGIN
UPDATE PRODUCT SET PRICE = DBMS_CRYPTO.ENCRYPT(
UTL_I18N.STRING_TO_RAW(CAST(PRICE AS RAW), 'AL32UTF8'),
DBMS_CRYPTO.ENCRYPT_AES256 + DBMS_CRYPTO.CHAIN_CBC + DBMS_CRYPTO.PAD_PKCS5,
UTL_I18N.STRING_TO_RAW(C_KEY, 'AL32UTF8'),
NULL
) WHERE PRICE IS NOT NULL;
END;
/
相关问题
SQL> CREATE OR REPLACE PROCEDURE ENCRYPT_PRICE AS 2 C_KEY VARCHAR2(32) := 'MY_SECRET_KEY_FOR_AES256'; 3 BEGIN 4 UPDATE PRODUCT SET PRICE = DBMS_CRYPTO.ENCRYPT( 5 UTL_I18N.STRING_TO_RAW(TO_CHAR(PRICE), 'AL32UTF8'), 6 DBMS_CRYPTO.ENCRYPT_AES256 + DBMS_CRYPTO.CHAIN_CBC + DBMS_CRYPTO.PAD_PKCS5, 7 UTL_I18N.STRING_TO_RAW(C_KEY, 'AL32UTF8'), 8 DBMS_CRYPTO.CHAIN_IV 9 ) WHERE PRICE IS NOT NULL; 10 END; 11 / Warning: Procedure created with compilation errors. SQL> SQL> SHOW ERRORS; Errors for PROCEDURE ENCRYPT_PRICE: LINE/COL ERROR -------- ----------------------------------------------------------------- 4/3 PL/SQL: SQL Statement ignored 8/5 PL/SQL: ORA-00904: "DBMS_CRYPTO"."CHAIN_IV": invalid identifier 8/17 PLS-00302: component 'CHAIN_IV' must be declared
根据错误信息,可以看出问题出在 `DBMS_CRYPTO.CHAIN_IV` 上,因为该标识符无效。实际上,DBMS_CRYPTO 包中没有 `CHAIN_IV` 这个组件,所以需要将其替换为 `NULL`。
修改后的代码如下:
```
CREATE OR REPLACE PROCEDURE ENCRYPT_PRICE AS
C_KEY VARCHAR2(32) := 'MY_SECRET_KEY_FOR_AES256';
BEGIN
UPDATE PRODUCT SET PRICE = DBMS_CRYPTO.ENCRYPT(
UTL_I18N.STRING_TO_RAW(TO_CHAR(PRICE), 'AL32UTF8'),
DBMS_CRYPTO.ENCRYPT_AES256 + DBMS_CRYPTO.CHAIN_CBC + DBMS_CRYPTO.PAD_PKCS5,
UTL_I18N.STRING_TO_RAW(C_KEY, 'AL32UTF8'),
NULL
) WHERE PRICE IS NOT NULL;
END;
/
4/3 PL/SQL: SQL Statement ignored 4/42 PL/SQL: ORA-00932: inconsistent datatypes: expected NUMBER got BINARY SQL> CREATE OR REPLACE PROCEDURE ENCRYPT_PRICE AS 2 C_KEY VARCHAR2(32) := 'MY_SECRET_KEY_FOR_AES256'; 3 BEGIN 4 UPDATE PRODUCT SET PRICE = DBMS_CRYPTO.ENCRYPT( 5 UTL_I18N.STRING_TO_RAW(CAST(PRICE AS RAW), 'AL32UTF8'), 6 DBMS_CRYPTO.ENCRYPT_AES256 + DBMS_CRYPTO.CHAIN_CBC + DBMS_CRYPTO.PAD_PKCS5, 7 UTL_I18N.STRING_TO_RAW(C_KEY, 'AL32UTF8'), 8 NULL 9 ) WHERE PRICE IS NOT NULL; 10 END; 11 / Warning: Procedure created with compilation errors. SQL> show errors; Errors for PROCEDURE ENCRYPT_PRICE: LINE/COL ERROR -------- ----------------------------------------------------------------- 4/3 PL/SQL: SQL Statement ignored 5/45 PL/SQL: ORA-00906: missing left parenthesis
根据错误信息,可以看出问题出在 `UPDATE` 语句中的 `DBMS_CRYPTO.ENCRYPT` 函数上,因为缺少左括号。这可能是因为 `DBMS_CRYPTO.ENCRYPT_AES256`、`DBMS_CRYPTO.CHAIN_CBC` 和 `DBMS_CRYPTO.PAD_PKCS5` 这三个常量之间应该使用竖线 `|` 进行位运算符的 OR 运算,而不是加号 `+`。
修改后的代码如下:
```
CREATE OR REPLACE PROCEDURE ENCRYPT_PRICE AS
C_KEY VARCHAR2(32) := 'MY_SECRET_KEY_FOR_AES256';
BEGIN
UPDATE PRODUCT SET PRICE = DBMS_CRYPTO.ENCRYPT(
UTL_I18N.STRING_TO_RAW(CAST(PRICE AS RAW), 'AL32UTF8'),
DBMS_CRYPTO.ENCRYPT_AES256 | DBMS_CRYPTO.CHAIN_CBC | DBMS_CRYPTO.PAD_PKCS5,
UTL_I18N.STRING_TO_RAW(C_KEY, 'AL32UTF8'),
NULL
) WHERE PRICE IS NOT NULL;
END;
/
阅读全文