如何在Netezza中的存储过程中使用CASE语法

时间:2014-11-20 08:48:02

标签: sql postgresql stored-procedures netezza

我只是netezza的新手(运行版本netezza版本7.1.0.1-P1 [Build 37788]),我创建了从另一个数据库加载数据的查询,这是我的查询:

CASE INSTR(B.WIFI.WIFI_DETAIL.LOCATION, ';') WHEN 0 THEN B.WIFI.WIFI_DETAIL.LOCATION ELSE BTRIM(SUBSTR(B.WIFI.WIFI_DETAIL.LOCATION, 1, INSTR(B.WIFI.WIFI_DETAIL.LOCATION, ';')-1)) END AS POI_NAME

它运行得很完美,但是当我放入存储过程时,使用@ScottMcG的解决方案进行了修改:

create or replace procedure SP_CREATE_TMP_F_WIFIID_AP_UTILISASI()
LANGUAGE NZPLSQL RETURNS INT4 AS
BEGIN_PROC

DECLARE

BEGIN
    /* CREATE TABLE TMP AP UTILISASI WITHOUT CLIENT */
    EXECUTE IMMEDIATE '
        create TABLE TMP_F_AP_UTILISASI as select AVG(ANN.THROUGPUT_V) as AVG_THROUGHPUT
            , SUM(ANN.BYTES_SENT) AS SUM_SENT
            , SUM(ANN.BYTES_RECIEVE) AS SUM_RECIEVE
            , (SUM(ANN.BYTES_RECIEVE)+SUM(ANN.BYTES_SENT)) AS TOTAL_AMOUNT
            , COUNT(ANN.C_MAC_ADDRESS) AS ASSOCIATED_CLIENT_COUNT
            , COUNT(ANN.USER_NAME) AS AUTHENTICATED_CLIENT_COUNT
            , MAX(ANN.TOTAL_WAKTU) AS MAX_SESSION_DURATION
            , ANN.AP_NAME
            , ANN.ASSO_TIME_V
            , ANN.PAKET_NAME
            , ANN.SSID
            , CASE INSTR(AD.LOCATION, ' || quote_literal (';') || ') WHEN 0 THEN AD.LOCATION ELSE BTRIM(SUBSTR(AD.LOCATION, 1, INSTR(AD.LOCATION, ' || quote_literal (';') || ')-1)) END AS POI_NAME
        FROM
            A.USR3.V_ALL_NMS_DETAIL AS ANN
        JOIN A.USR3.V_NMS_AP_DETAIL AS AD
            ON ANN.AP_NAME = AD.AP_NAME
        GROUP BY ANN.AP_NAME
            , ANN.ASSO_TIME_V
            , ANN.PAKET_NAME
            , ANN.SSID
            , AD.LOCATION
    ';

END;

END_PROC;

当我打电话给程序时,它仍然不起作用:

ERROR: Cross Database Access not supported for this type of command

我从数据库A调用,并在A

中构建过程

也许你有解决我的问题的想法,,,

或者也许任何人有另一个查询相同的查询:

CASE INSTR(AD.LOCATION, ' || quote_literal (';') || ') WHEN 0 THEN AD.LOCATION ELSE BTRIM(SUBSTR(AD.LOCATION, 1, INSTR(AD.LOCATION, ' || quote_literal (';') || ')-1)) END AS POI_NAME

THX

2 个答案:

答案 0 :(得分:1)

在你的情况下,CASE本身不是一个问题。在存储过程中使用动态SQL时(例如,EXECUTE IMMEDIATE),必须转义嵌入在文本中的引号。你可以用两种(至少)方式做到这一点。

以下是使用带字符串文字的CASE语句的存储过程的两个示例。第一个使用两个单引号''来表示转义的单引号。第二个使用了quote_literal函数。

Escaped Quotation Mark Version

CREATE OR REPLACE PROCEDURE SP_SAMPLE()
RETURNS INTEGER
EXECUTE AS CALLER
LANGUAGE NZPLSQL AS
BEGIN_PROC

BEGIN

EXECUTE IMMEDIATE 'UPDATE TESTING_TABLE SET COL1 =  CASE INSTR(COL2,'';'') WHEN 0 THEN ''NOT SEMICOLON'' ELSE ''SEMICOLON'' END';

END;
END_PROC;

QUOTE_LITERAL函数版本

CREATE OR REPLACE PROCEDURE SP_SAMPLE()
RETURNS INTEGER
EXECUTE AS CALLER
LANGUAGE NZPLSQL AS
BEGIN_PROC

BEGIN

EXECUTE IMMEDIATE 'UPDATE TESTING_TABLE SET COL1 =  CASE INSTR(COL2,' || quote_literal (';') ||
    ') WHEN 0 THEN ' || quote_literal('NOT SEMICOLON') || ' ELSE ' || quote_literal('SEMICOLON') || ' END';

END;
END_PROC;

您可以参考文档和示例for running dynamic SQL in Netezza here

答案 1 :(得分:0)

试试这个:

    create or replace procedure SP_CREATE_TMP_F_WIFIID_AP_UTILISASI()
LANGUAGE NZPLSQL RETURNS INT4 AS
BEGIN_PROC

DECLARE

BEGIN
    /* CREATE TABLE TMP AP UTILISASI WITHOUT CLIENT */
    EXECUTE IMMEDIATE '
        create TABLE TMP_F_AP_UTILISASI as select AVG(ANN.THROUGPUT_V) as AVG_THROUGHPUT
            , SUM(ANN.BYTES_SENT) AS SUM_SENT
            , SUM(ANN.BYTES_RECIEVE) AS SUM_RECIEVE
            , (SUM(ANN.BYTES_RECIEVE)+SUM(ANN.BYTES_SENT)) AS TOTAL_AMOUNT
            , COUNT(ANN.C_MAC_ADDRESS) AS ASSOCIATED_CLIENT_COUNT
            , COUNT(ANN.USER_NAME) AS AUTHENTICATED_CLIENT_COUNT
            , MAX(ANN.TOTAL_WAKTU) AS MAX_SESSION_DURATION
            , ANN.AP_NAME
            , ANN.ASSO_TIME_V
            , ANN.PAKET_NAME
            , ANN.SSID
            , CASE INSTR(AD.LOCATION, ";") WHEN 0 THEN AD.LOCATION ELSE BTRIM(SUBSTR(AD.LOCATION, 1, INSTR(AD.LOCATION, ";")-1)) END AS POI_NAME
        FROM
            A.USR3.V_ALL_NMS_DETAIL AS ANN
        JOIN A.USR3.V_NMS_AP_DETAIL AS AD
            ON ANN.AP_NAME = AD.AP_NAME
        GROUP BY ANN.AP_NAME
            , ANN.ASSO_TIME_V
            , ANN.PAKET_NAME
            , ANN.SSID
            , AD.LOCATION
    ';

END;

END_PROC;