Firebird存储过程中的动态SQL(位置)

时间:2019-03-07 23:54:16

标签: sql stored-procedures firebird firebird2.5

我有一个SP,它接收两个参数nginx.ingress.kubernetes.io "kind": "Ingress", "apiVersion": "extensions/v1beta1", "metadata": { "name": "example-ingress", "namespace": "master", "selfLink": "/apis/extensions/v1beta1/namespaces/master/ingresses/example-ingress", "uid": "01e81627-3b90-11e9-bb5a-f6bc944a4132", "resourceVersion": "23345275", "generation": 1, "creationTimestamp": "2019-02-28T19:35:30Z", "labels": { }, "annotations": { "ingress.kubernetes.io/backend-protocol": "HTTPS", "ingress.kubernetes.io/limit-rps": "100", "ingress.kubernetes.io/proxy-body-size": "100m", "ingress.kubernetes.io/proxy-read-timeout": "60", "ingress.kubernetes.io/proxy-send-timeout": "60", "ingress.kubernetes.io/secure-backends": "true", "ingress.kubernetes.io/secure-verify-ca-secret": "example-ingress-ssl", "kubernetes.io/ingress.class": "nginx", "nginx.ingress.kubernetes.io/affinity": "cookie", "nginx.ingress.kubernetes.io/backend-protocol": "HTTPS", "nginx.ingress.kubernetes.io/limit-rps": "100", "nginx.ingress.kubernetes.io/proxy-body-size": "100m", "nginx.ingress.kubernetes.io/proxy-buffer-size": "8k", "nginx.ingress.kubernetes.io/proxy-read-timeout": "60", "nginx.ingress.kubernetes.io/proxy-send-timeout": "60", "nginx.ingress.kubernetes.io/secure-verify-ca-secret": "example-ingress-ssl", "nginx.ingress.kubernetes.io/session-cookie-expires": "172800", "nginx.ingress.kubernetes.io/session-cookie-max-age": "172800", "nginx.ingress.kubernetes.io/session-cookie-name": "route", "nginx.org/websocket-services": "example" } }, "spec": { "tls": [ { "hosts": [ "*.example.net" ], "secretName": "example-ingress-ssl" } ], "rules": [ { "host": "*.example.net", "http": { "paths": [ { "path": "/", "backend": { "serviceName": "example", "servicePort": 443 } } ] } } ] }, "status": { "loadBalancer": { "ingress": [ {} ] } } } ,如下所示:

P1

我需要基于P2参数生成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 ... ... 子句,像这样:

where

如何在where子句中使用这种类型的P2语句?

2 个答案:

答案 0 :(得分:2)

对我来说,您的问题在SQL查询的OR子句中转换为简单的WHERE条件:

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

答案 1 :(得分:1)

GMB的答案在大多数情况下都可以正常工作,但在更复杂的情况下,其性能可能不太理想。一种替代解决方案是动态构建查询字符串并使用execute statement执行它:

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 
declare query varchar(2048);
begin
  query = 'select ......';
  if (p2 = 1) then
    query = query || ' where (state=''SP'' and situation=''stopped'')';
  else if (p2 = 2) then
    query = query || ' where (state=''MG'' and situation=''moving'')';

  -- if you expect a single result
  execute statement query into code, name, state, situation;

  -- OR

  -- for multiple results
  for execute statement query into code, name, state, situation do
    suspend;
end