[英]To parameterize a SQL statement with greater than, less than or equal to
I have a SQL report.我有一份 SQL 报告。 I want to pass a new parameter to this and this parameter should decide below 3 outputs.我想给它传递一个新参数,这个参数应该决定在 3 个输出以下。
select * from po_distributions_all: select * 来自 po_distributions_all:
encumbered_amount is one of the column. encumbered_amount 是其中一列。
Parameter Value = P, return all rows with encumbered_amount values which are > zero参数值 = P,返回所有 encumbered_amount 值 > 零的行
Parameter Value = N, return all with encumbered_amount values which are < zero.参数值 = N,返回所有 encumbered_amount 值 < 零。
Parameter Value = E, return all encumbered_amount values not equal to zero参数值 = E,返回所有不等于零的 encumbered_amount 值
Parameter as Blank, return all the rows.参数为空白,返回所有行。
I know I can achieve this by a union query.我知道我可以通过联合查询来实现这一点。
Is there a way I can achieve without a union?有没有办法我可以在没有工会的情况下实现目标?
The query is already a lengthy one.查询已经很长了。 For the simplicity of this question I mentioned it as a single line query above.为了这个问题的简单性,我在上面将其作为单行查询提到。
You could do this in the where clause:您可以在 where 子句中这样做:
WHERE
(( :parameter = 'P' AND encumbered_amount > 0 ) OR
( :parameter = 'N' AND encumbered_amount < 0 ) OR
( :parameter = 'E' AND encumbered_amount != 0 ) OR
( :parameter IS NULL )
)
What about dynamic SQL?动态SQL呢?
sqlstr VARCAR2(1000);
cur SYS_REFCURSOR;
BEGIN
sqlstr := 'select * from po_distributions_all';
IF Parameter = 'P' THEN
sqlstr := sqlstr || ' WHERE encumbered_amount > 0';
ELSEIF Parameter = 'N' THEN
sqlstr := sqlstr || ' WHERE encumbered_amount < 0';
ELSEIF Parameter = 'E' THEN
sqlstr := sqlstr || ' WHERE encumbered_amount <> 0';
ELSEIF Parameter IS NULL THEN
NULL;
ELSE
RAISE VALUE_ERROR;
END IF;
OPEN cur FOR sqlstr;
...
END;
How about CASE
expression? CASE
表达式怎么样? Could've been simpler if there were no E
option:)如果没有E
选项可能会更简单:)
This is a SQL*Plus example;这是一个 SQL*Plus 示例; depending on tool you use, its '&¶meter'
substitution variable could be :parameter
or something else.根据您使用的工具,其'&¶meter'
替换变量可能是:parameter
或其他。
SQL> WITH
2 po_distributions_all (encumbered_amount)
3 AS
4 (SELECT -200 FROM DUAL
5 UNION ALL
6 SELECT 100 FROM DUAL
7 UNION ALL
8 SELECT 0 FROM DUAL)
9 SELECT *
10 FROM po_distributions_all
11 WHERE ( CASE
12 WHEN '&¶meter' <> 'E'
13 OR '&¶meter' IS NULL
14 THEN
15 SIGN (encumbered_amount)
16 END =
17 CASE
18 WHEN '&¶meter' = 'P' THEN 1
19 WHEN '&¶meter' = 'N' THEN -1
20 WHEN '&¶meter' IS NULL THEN SIGN (encumbered_amount)
21 END
22 OR CASE WHEN '&¶meter' = 'E' THEN encumbered_amount END <> 0);
Enter value for parameter: P
ENCUMBERED_AMOUNT
-----------------
100
Negative values:负值:
SQL> UNDEFINE PARAMETER
SQL> /
Enter value for parameter: N
ENCUMBERED_AMOUNT
-----------------
-200
All values except zero:除零以外的所有值:
SQL> UNDEFINE PARAMETER
SQL> /
Enter value for parameter: E
ENCUMBERED_AMOUNT
-----------------
-200
100
All values (parameter's value is NULL
):所有值(参数值为NULL
):
SQL> UNDEFINE PARAMETER
SQL> /
Enter value for parameter:
ENCUMBERED_AMOUNT
-----------------
-200
100
0
SQL>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.