SQL 如何在 SELECT 语句中使用 BOOLEAN 类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1465405/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use BOOLEAN type in SELECT statement
提问by Ula Krukar
I have a PL/SQL function with BOOLEAN in parameter:
我有一个参数为 BOOLEAN 的 PL/SQL 函数:
function get_something(name in varchar2, ignore_notfound in boolean);
This function is a part of 3rd party tool, I cannot change this.
此功能是第 3 方工具的一部分,我无法更改。
I would like to use this function inside a SELECT statement like this:
我想在这样的 SELECT 语句中使用这个函数:
select get_something('NAME', TRUE) from dual;
This does not work, I get this exception:
这不起作用,我收到此异常:
ORA-00904: "TRUE": invalid identifier
ORA-00904: "TRUE": 无效标识符
As I understand it, keyword TRUE
is not recognized.
据我了解,TRUE
无法识别关键字。
How can I make this work?
我怎样才能使这项工作?
采纳答案by Tony Andrews
You can build a wrapper function like this:
您可以像这样构建一个包装函数:
function get_something(name in varchar2,
ignore_notfound in varchar2) return varchar2
is
begin
return get_something (name, (upper(ignore_notfound) = 'TRUE') );
end;
then call:
然后调用:
select get_something('NAME', 'TRUE') from dual;
It's up to you what the valid values of ignore_notfound are in your version, I have assumed 'TRUE' means TRUE and anything else means FALSE.
取决于您的版本中 ignore_notfound 的有效值是什么,我假设“TRUE”表示 TRUE,其他任何表示 FALSE。
回答by Ash
You can definitely get Boolean value from a SELECT query, you just can't use a Boolean data-type.
您绝对可以从 SELECT 查询中获取布尔值,只是不能使用布尔数据类型。
You can represent a Boolean with 1/0.
您可以用 1/0 表示一个布尔值。
CASE WHEN (10 > 0) THEN 1 ELSE 0 END (It can be used in SELECT QUERY)
SELECT CASE WHEN (10 > 0) THEN 1 ELSE 0 END AS MY_BOOLEAN_COLUMN
FROM DUAL
Returns, 1 (in Hibernate/Mybatis/etc 1 is true). Otherwise, you can get printable Boolean values from a SELECT.
返回,1(在 Hibernate/Mybatis/etc 中 1 为真)。否则,您可以从 SELECT 获得可打印的布尔值。
SELECT CASE WHEN (10 > 0) THEN 'true' ELSE 'false' END AS MY_BOOLEAN_COLUMN
FROM DUAL
This returns the string 'true'
.
这将返回字符串'true'
。
回答by Quassnoi
From documentation:
从文档:
You cannot insert the values
TRUE
andFALSE
into a database column. You cannot select or fetch column values into aBOOLEAN
variable. Functions called from aSQL
query cannot take anyBOOLEAN
parameters. Neither can built-inSQL
functions such asTO_CHAR
; to representBOOLEAN
values in output, you must useIF-THEN
orCASE
constructs to translateBOOLEAN
values into some other type, such as0
or1
,'Y'
or'N'
,'true'
or'false'
, and so on.
您不能将值
TRUE
和FALSE
插入到数据库列中。您不能将列值选择或提取到BOOLEAN
变量中。从SQL
查询调用的函数不能接受任何BOOLEAN
参数。内置SQL
函数也不能,例如TO_CHAR
; 要BOOLEAN
在输出中表示值,您必须使用IF-THEN
或CASE
构造将BOOLEAN
值转换为其他类型,例如0
or1
、'Y'
or'N'
、'true'
or'false'
等。
You will need to make a wrapper function that takes an SQL
datatype and use it instead.
您将需要创建一个接受SQL
数据类型的包装函数并使用它。
回答by JuanZe
The BOOLEAN data type is a PL/SQL data type. Oracle does not provide an equivalent SQL data type (...) you can create a wrapper function which maps a SQL type to the BOOLEAN type.
BOOLEAN 数据类型是一种 PL/SQL 数据类型。Oracle 不提供等效的 SQL 数据类型 (...) 您可以创建一个包装函数,将 SQL 类型映射到 BOOLEAN 类型。
Check this: http://forums.datadirect.com/ddforums/thread.jspa?threadID=1771&tstart=0&messageID=5284
检查这个:http: //forums.datadirect.com/ddforums/thread.jspa?threadID=1771&tstart=0&messageID=5284
回答by Kanu Mundu
select get_something('NAME', sys.diutil.int_to_bool(1)) from dual;
回答by Eran ben-ari
Compile this in your database and start using boolean statements in your querys.
在您的数据库中编译它并开始在您的查询中使用布尔语句。
note: the function get's a varchar2 param, so be sure to wrap any "strings" in your statement. It will return 1 for true and 0 for false;
注意:函数 get 是一个 varchar2 参数,所以一定要在你的语句中包含任何“字符串”。它将为真返回 1,为假返回 0;
select bool('''abc''<''bfg''') from dual;
CREATE OR REPLACE function bool(p_str in varchar2) return varchar2
is
begin
execute immediate ' begin if '||P_str||' then
:v_res := 1;
else
:v_res := 0;
end if; end;' using out v_res;
return v_res;
exception
when others then
return '"'||p_str||'" is not a boolean expr.';
end;
/
回答by Lukas Eder
With Oracle 12, you can use the WITH
clause to declare your auxiliary functions. I'm assuming your get_something
function returns varchar2
:
在 Oracle 12 中,您可以使用WITH
子句来声明您的辅助函数。我假设你的get_something
函数返回varchar2
:
with
function get_something_(name varchar2, ignore_notfound number)
return varchar2
is
begin
-- Actual function call here
return get_something(name, not ignore_notfound = 0);
end get_something_;
-- Call auxiliary function instead of actual function
select get_something_('NAME', 1) from dual;
Of course, you could have also stored your auxiliary function somewhere in the schema as shown in this answer, but by using WITH
, you don't have any external dependencies just to run this query. I've blogged about this technique more in detail here.
当然,您也可以将辅助函数存储在架构中的某个位置,如本答案所示,但是通过使用WITH
,您没有任何外部依赖项来运行此查询。我已经在博客上更详细地介绍了这项技术。
回答by SherlockSpreadsheets
The answer to this question simply put is: Don't use BOOLEAN with Oracle-- PL/SQL is dumb and it doesn't work. Use another data type to run your process.
简单地说,这个问题的答案是:不要将 BOOLEAN 与 Oracle 一起使用——PL/SQL 是愚蠢的,它不起作用。使用另一种数据类型来运行您的流程。
A note to SSRS report developers with Oracle datasource:You can use BOOLEAN parameters, but be careful how you implement. Oracle PL/SQL does not play nicely with BOOLEAN, but you can use the BOOLEAN value in the Tablix Filter if the data resides in your dataset. This really tripped me up, because I have used BOOLEAN parameter with Oracle data source. But in that instance I was filtering against Tablix data, not SQL query.
给使用 Oracle 数据源的 SSRS 报告开发人员的说明:您可以使用 BOOLEAN 参数,但要注意实现方式。Oracle PL/SQL 不能很好地与 BOOLEAN 配合使用,但如果数据位于您的数据集中,您可以在 Tablix 过滤器中使用 BOOLEAN 值。这真的让我绊倒了,因为我在 Oracle 数据源中使用了 BOOLEAN 参数。但在那种情况下,我过滤的是 Tablix 数据,而不是 SQL 查询。
If the data is NOT in your SSRS Dataset Fields, you can rewrite the SQL something like this using an INTEGER parameter:
如果数据不在您的 SSRS 数据集字段中,您可以使用 INTEGER 参数重写 SQL:
__
__
<ReportParameter Name="paramPickupOrders">
<DataType>Integer</DataType>
<DefaultValue>
<Values>
<Value>0</Value>
</Values>
</DefaultValue>
<Prompt>Pickup orders?</Prompt>
<ValidValues>
<ParameterValues>
<ParameterValue>
<Value>0</Value>
<Label>NO</Label>
</ParameterValue>
<ParameterValue>
<Value>1</Value>
<Label>YES</Label>
</ParameterValue>
</ParameterValues>
</ValidValues>
</ReportParameter>
...
<Query>
<DataSourceName>Gmenu</DataSourceName>
<QueryParameters>
<QueryParameter Name=":paramPickupOrders">
<Value>=Parameters!paramPickupOrders.Value</Value>
</QueryParameter>
<CommandText>
where
(:paramPickupOrders = 0 AND ordh.PICKUP_FLAG = 'N'
OR :paramPickupOrders = 1 AND ordh.PICKUP_FLAG = 'Y' )
If the data is in your SSRS Dataset Fields, you can use a tablix filter with a BOOLEAN parameter:
如果数据在您的 SSRS 数据集字段中,您可以使用带有 BOOLEAN 参数的 tablix 过滤器:
__
__
</ReportParameter>
<ReportParameter Name="paramFilterOrdersWithNoLoad">
<DataType>Boolean</DataType>
<DefaultValue>
<Values>
<Value>false</Value>
</Values>
</DefaultValue>
<Prompt>Only orders with no load?</Prompt>
</ReportParameter>
...
<Tablix Name="tablix_dsMyData">
<Filters>
<Filter>
<FilterExpression>
=(Parameters!paramFilterOrdersWithNoLoad.Value=false)
or (Parameters!paramFilterOrdersWithNoLoad.Value=true and Fields!LOADNUMBER.Value=0)
</FilterExpression>
<Operator>Equal</Operator>
<FilterValues>
<FilterValue DataType="Boolean">=true</FilterValue>
</FilterValues>
</Filter>
</Filters>
回答by Gart
How about using an expression which evaluates to TRUE (or FALSE)?
如何使用计算结果为 TRUE(或 FALSE)的表达式?
select get_something('NAME', 1 = 1) from dual
回答by David Andres
PL/SQL is complaining that TRUE is not a valid identifier, or variable. Set up a local variable, set it to TRUE, and pass it into the get_something function.
PL/SQL 抱怨 TRUE 不是有效的标识符或变量。设置一个局部变量,将其设置为 TRUE,并将其传递给 get_something 函数。