sql - Firebird 存储过程中的动态 SQL(where)

标签 sql stored-procedures where-clause firebird firebird2.5

我有一个接收 2 个参数的 SP,P1P2,如下所示:

CREATE OR ALTER PROCEDURE MY_PROC (P1 varchar(10), P2 smallint = 1)
RETURNS (
    code      VARCHAR(10),
    name      VARCHAR(70),
    state     VARCHAR(2),
    situation VARCHAR(20)
AS 
    ...
    ...

我需要根据 P2 参数生成 where 子句,如下所示:

if (P2=1) then
    where (state='SP' and situation='stopped')  
elseif (P2=2)
    where (state='MG' and situation='moving')

如何在where子句中使用这种if语句?

最佳答案

对我而言,您的问题可以转化为 SQL 查询的 WHERE 子句中的简单 OR 条件:

WHERE
   (:P2 = 1 AND state='SP' and situation='stopped')
   OR (:P2 = 2 AND state='MG' and situation='moving')

关于sql - Firebird 存储过程中的动态 SQL(where),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55054682/

相关文章:

sql - 在postgres中声明一个表类型的变量

oracle - ORACLE中多列的总和

php - 如何在单个 SQL 查询中执行多个更新?

sql - 如何改进时间线 SQL 查询

sql - 如何检查列(数组类型)是否包含 SQL 中的值(不包含 ANY)?

asp.net - 防止SQL注入(inject)攻击: the differences between mySql and SQL Server 2008

SQL Server 重复行

sql - oracle游标值根据条件而变化

sql - 通配符搜索数字范围

mysql 5.0升级到5.1 Match功能