ALTER TABLE ORDERS MODIFY TotalCost NUMBER(35,2); DECLARE i_key RAW(32); BEGIN i_key :=UTL_RAW.cast_to_raw('618272'); UPDATE ORDERS SET TotalCost = DBMS_CRYPTO.ENCRYPT( src=>UTL_RAW.CAST_TO_RAW(TO_CHAR(TotalCost)), typ=>DBMS_CRYPTO.AES_CBC_PKCS5, key=>i_key, iv=>NULL ) WHERE TotalCost IS NOT NULL; END; / 出现了ERROR at line 6: ORA-06550: line 6, column 35: PL/SQL: ORA-00932: inconsistent datatypes: expected NUMBER got BINARY ORA-06550: line 5, column 4: PL/SQL: SQL Statement ignored报错,怎么解决
时间: 2023-12-03 18:45:34 浏览: 117
这个错误是由于在UPDATE语句中将加密后的二进制数据插入到了TotalCost字段中,而TotalCost字段的数据类型是NUMBER,因此导致了数据类型不一致的错误。
为了解决这个问题,你可以将加密后的二进制数据转换为16进制字符串,然后将该字符串插入到TotalCost字段中。修改后的代码如下:
```
ALTER TABLE ORDERS MODIFY TotalCost NUMBER(35,2);
DECLARE
i_key RAW(32);
v_hex VARCHAR2(4000);
BEGIN
i_key :=UTL_RAW.cast_to_raw('618272');
UPDATE ORDERS SET TotalCost = DBMS_CRYPTO.ENCRYPT(
src=>UTL_RAW.CAST_TO_RAW(TO_CHAR(TotalCost)),
typ=>DBMS_CRYPTO.AES_CBC_PKCS5,
key=>i_key,
iv=>NULL
) WHERE TotalCost IS NOT NULL;
SELECT RAWTOHEX(TotalCost) INTO v_hex FROM ORDERS WHERE TotalCost IS NOT NULL;
UPDATE ORDERS SET TotalCost = v_hex WHERE TotalCost IS NOT NULL;
END;
/
```
这样,加密后的数据就以16进制字符串的形式保存在TotalCost字段中了。
阅读全文