Dapper making Oracle call with output parameters gives "PL/SQL: numeric or value error: DML Returning: Error writing to host variable"

Dapper making Oracle call with output parameters gives "PL/SQL: numeric or value error: DML Returning: Error writing to host variable"

我正在尝试使用 Dapper 调用一些我需要 运行 的 Oracle SQL 语句,但我收到此错误

ORA-06502: PL/SQL: numeric or value error: DML Returning: Error writing to host variable
ORA-06512: at line 11

我目前有下面的代码,我只是在获取生成的新密钥时遇到问题 NewNameAddRKey 我将其作为输出传递到参数中,但我得到了以上错误,R_KEY 列是 VARCHAR2(5 BYTE) 列,所以出于某种原因它不能将其转换为字符串?,我真的可以做一些帮助解决这个问题,因为它进展得很慢。

var parameters = new DynamicParameters(rowToAdd);

parameters.Add(name: "NewNameAddRKey", dbType: DbType.String, direction: ParameterDirection.Output);
parameters.Add(name: "firstCharacterOfName", value: rowToAdd.FirstCharacterOfName, dbType: DbType.String, direction: ParameterDirection.Input);

await context.Connection.ExecuteAsync(
    @"
    DECLARE 
    initialChar VARCHAR2(1) := :firstCharacterOfName;
    startsWith VARCHAR2(1) := 'L';
    newKey VARCHAR2(5);
    status NUMBER;
    message VARCHAR2(200);
    BEGIN
        CUSTOMER_PKG.name_add_key(initialChar, startsWith, newKey, status, message);
        
        INSERT INTO NAME_ADD(R_KEY, ADDRESS_1, ADDRESS_2,ADDRESS_3, ADDRESS_4, POST_CODE, CONTACT, FAX_NO, NAME, PHONE_NO, TELEX_NO)
        VALUES(newKey, :AddressLine1, :AddressLine2, :AddressLine3, :AddressLine4, :PostCode, :OfficeContact, :Fax, :Name, :HomePhone, :Telex)
        RETURNING R_KEY INTO :NewNameAddRKey;

    END;",
    parameters,
    context.Transaction);

在您发布的代码中,没有定义字符串变量的大小,而在 Oracle 中,每个此类变量都必须具有预定义的长度。

Dapper 可能定义了一些默认长度,这在您的情况下是不够的。

正如我们在评论中讨论的那样,这是解决方案:

parameters.Add(name: "NewNameAddRKey", 
               dbType: DbType.String, 
               /* --> */ size: 5, /* <--- */
               direction: ParameterDirection.Output);