如何将布尔数据类型作为存储过程的参数传递?

Posted

技术标签:

【中文标题】如何将布尔数据类型作为存储过程的参数传递?【英文标题】:How do you pass a boolean data type as a parameter for a stored procedure? 【发布时间】:2018-07-17 19:55:31 【问题描述】:

假设您有一个接受单个参数的现有存储过程,该参数恰好是the BOOLEAN Informix data type:

test_bool_param(BOOLEAN)

对于这样的存储过程,EXECUTE PROCEDURE 语句会是什么样子?

这是我尝试过但失败的方法:

EXECUTE PROCEDURE test_bool_param('true'); -- [Error Code: -1260, SQL State: IX000]  It is not possible to convert between the specified types.
EXECUTE PROCEDURE test_bool_param('false');-- [Error Code: -1260, SQL State: IX000]  It is not possible to convert between the specified types.
EXECUTE PROCEDURE test_bool_param('TRUE'); -- [Error Code: -1260, SQL State: IX000]  It is not possible to convert between the specified types.
EXECUTE PROCEDURE test_bool_param('FALSE');-- [Error Code: -1260, SQL State: IX000]  It is not possible to convert between the specified types.
EXECUTE PROCEDURE test_bool_param(true);   -- [Error Code:  -201, SQL State: 42000]  A syntax error has occurred.
EXECUTE PROCEDURE test_bool_param(false);  -- [Error Code:  -201, SQL State: 42000]  A syntax error has occurred.
EXECUTE PROCEDURE test_bool_param(TRUE);   -- [Error Code:  -201, SQL State: 42000]  A syntax error has occurred.
EXECUTE PROCEDURE test_bool_param(FALSE);  -- [Error Code:  -201, SQL State: 42000]  A syntax error has occurred.
EXECUTE PROCEDURE test_bool_param(1);      -- [Error Code:  -674, SQL State: IX000]  Routine (test_bool_param) can not be resolved.
EXECUTE PROCEDURE test_bool_param(0);      -- [Error Code:  -674, SQL State: IX000]  Routine (test_bool_param) can not be resolved.
EXECUTE PROCEDURE test_bool_param('\1');   -- [Error Code: -1260, SQL State: IX000]  It is not possible to convert between the specified types.
EXECUTE PROCEDURE test_bool_param('\0');   -- [Error Code: -1260, SQL State: IX000]  It is not possible to convert between the specified types.
EXECUTE PROCEDURE test_bool_param('t');    -- [Error Code: -1260, SQL State: IX000]  It is not possible to convert between the specified types.
EXECUTE PROCEDURE test_bool_param('f');    -- [Error Code: -1260, SQL State: IX000]  It is not possible to convert between the specified types.
EXECUTE PROCEDURE test_bool_param('T');    -- [Error Code: -1260, SQL State: IX000]  It is not possible to convert between the specified types.
EXECUTE PROCEDURE test_bool_param('F');    -- [Error Code: -1260, SQL State: IX000]  It is not possible to convert between the specified types.
EXECUTE PROCEDURE test_bool_param(t);      -- [Error Code:  -201, SQL State: 42000]  A syntax error has occurred.
EXECUTE PROCEDURE test_bool_param(f);      -- [Error Code:  -201, SQL State: 42000]  A syntax error has occurred.
EXECUTE PROCEDURE test_bool_param(T);      -- [Error Code:  -201, SQL State: 42000]  A syntax error has occurred.
EXECUTE PROCEDURE test_bool_param(F);      -- [Error Code:  -201, SQL State: 42000]  A syntax error has occurred.
EXECUTE PROCEDURE test_bool_param(\1);     -- [Error Code:  -202, SQL State: IX000]  An illegal character has been found in the statement.
EXECUTE PROCEDURE test_bool_param(\0);     -- [Error Code:  -202, SQL State: IX000]  An illegal character has been found in the statement.

【问题讨论】:

【参考方案1】:

这些EXECUTE PROCEDURE 调用实际上成功了:

EXECUTE PROCEDURE test_bool_param('t');     -- Result set fetched - SUCCESS
EXECUTE PROCEDURE test_bool_param('f');     -- Result set fetched - FAILURE
EXECUTE PROCEDURE test_bool_param('T');     -- Result set fetched - SUCCESS
EXECUTE PROCEDURE test_bool_param('F');     -- Result set fetched - FAILURE

但仅当存储过程中的任何检查也与't''f''T''F' 之一进行比较时:

CREATE PROCEDURE test_bool_param
(in_param BOOLEAN)
RETURNING VARCHAR(8)
IF (in_param = 't') THEN
        RETURN 'SUCCESS';
ELSE
        RETURN 'FAILURE';
END IF;
END PROCEDURE;

【讨论】:

来自文档:You can compare two BOOLEAN values to test for equality or inequality. You can also compare a BOOLEAN value to the Boolean literals 't' and 'f'. BOOLEAN values are not case-sensitive; 't' is equivalent to 'T' and 'f' to 'F'.,所以是的,它适用于您成功执行该过程时使用的文字。 @LuísMarques 这来自什么文档?问题中链接的 IBM 官方文档仅提及 '\0''\1',两者都不起作用。 其实在Guide to SQL: Reference,数据类型部分:Description of Data Types

以上是关于如何将布尔数据类型作为存储过程的参数传递?的主要内容,如果未能解决你的问题,请参考以下文章

如何将临时表作为参数传递到单独的存储过程中

如何给SQLSERVER存储过程传递数组参数

在 C# 中仅将类型作为参数传递

在 PL/SQL 存储过程中将表作为参数传递

使用ado.net将多个SQL表值参数传递到单个存储过程

如何将结果集作为输入从java传递到oracle存储过程