我有不能更改的表,这些表具有以下结构
requests_table
ID PROCESS_ID STATUS DATE
1 1 -1 2002
2 2 1 2003
3 2 4 2004
4 3 5 2005
5 3 14 2005
6 1 15 2006
并且process_id是proccess_table的外键
process_id process_name
1 a
2 b
3 c
我有输入参数:processID
我想做什么
if the paramter = 5
then return records that have STATUS 14 or 15 only
else
if
the paramter is null
then return all records
else if the paramter has any value except 5 , apply this condition
where :processID = requests_table.PROCESS_ID
如何实现? 我无法在表中插入任何新记录
答案 0 :(得分:1)
我建议使用调用SQL的任何内容(例如PHP)在不同的SQL语句之间进行切换,例如
if(processID==5){
query="SELECT * FROM requests_table WHERE STATUS=14 OR STATUS=15"
} else if (processID==null){
query="SELECT * FROM requests_table"
} else {
query="SELECT * FROM requests_table WHERE PROCESS_ID=processID"
}
,然后运行所选的查询。
如果您希望将所有内容都包含在一个SQL语句中,则可以这样做
SELECT * FROM requests_table WHERE (processID=5 AND (STATUS=14 OR STATUS=15)) OR processID NULL OR PROCESS_ID=processID
答案 1 :(得分:1)
您可以在单个语句中执行以下操作:
SELECT *
FROM requests_table
WHERE (:processID = 5 AND STATUS IN (14, 15)) OR
(:processID = processId) OR
(:processID IS NULL);
答案 2 :(得分:0)
您可以使用EXECUTE IMMEDIATE查询 定义一个varchar变量,然后在其中检查参数并设置查询的参数后在其中设置查询 看这个例子:
declare
myQuery varchar(500) ;
parameter number := 5;
begin
myQuery := ' select * from requests_table ';
if parameter = 5 then
myQuery := myQuery || ' where (status=14 OR status=15) ' ;
end if;
EXECUTE IMMEDIATE myQuery;
end;
end;
参数是您要传递给它的值