如何在雪花sql存储过程中传递LIMIT和OFFSET参数?

问题描述 投票:0回答:1

这是我的存储过程:

CREATE OR REPLACE PROCEDURE TCT_WEBAPP.DBO.GET_FILTERED_PAGINATION_PROVIDERS(
    "MONTH_START" VARCHAR(20), 
    "MONTH_END" VARCHAR(20), 
    "IP_FILTER" VARCHAR(100), 
    "PROPOSALS_TABLE_NAME" VARCHAR(100),
    "TIER" VARCHAR(20),
    "CITY" VARCHAR(20),
    "TYPE" VARCHAR(20),
    "PAGE_OFFSET" INTEGER,
    "PAGE_SIZE" INTEGER
)
RETURNS TABLE (
    "PROVIDERNAMEADJUSTED" VARCHAR(250), 
    "PROVIDERTYPEADJUSTED" VARCHAR(250), 
    "PROVIDERCITYADJUSTED" VARCHAR(250), 
    "STANDARDTIER" VARCHAR(250), 
    "TOTALACTUALCOST" FLOAT, 
    "TOTALCHANGE" FLOAT, 
    "TOTALCHANGEPERCENT" FLOAT
)
LANGUAGE SQL
EXECUTE AS OWNER
AS
'
DECLARE
    res RESULTSET DEFAULT (
        SELECT 
            PROVIDER.PROVIDERNAMEADJUSTED, 
            PROVIDER.PROVIDERTYPEADJUSTED, 
            PROVIDER.PROVIDERCITYADJUSTEDGROUP, 
            PROVIDER.STANDARDTIER, 
            SUM(PROPOSAL_CLAIMS.ACTUALCOST) AS TOTALACTUALCOST, 
            SUM(PROPOSAL_CLAIMS.CHANGE) AS TOTALCHANGE, 
            SUM(PROPOSAL_CLAIMS.CHANGE) / SUM(PROPOSAL_CLAIMS.ACTUALCOST) * 100 AS TOTALCHANGEPERCENT
        FROM 
            (
                SELECT 
                    c.PROGRAMNAME, 
                    c.PROGRAMTPA, 
                    c.PROVIDERCODE, 
                    c.SERVICECODE, 
                    c.NOOFUNITS, 
                    c.ACTUALCOST, 
                    c.INCURRALMONTHBRACKET, 
                    c.CLAIMTYPEIBNR, 
                    p.CLEANEDPROPOSAL, 
                    p.CLEANEDPROPOSAL * c.NOOFUNITS AS PROPOSEDCOST, 
                    PROPOSEDCOST - c.ACTUALCOST AS CHANGE 
                FROM 
                    CLAIMSDATATCT c 
                INNER JOIN 
                    TABLE(:PROPOSALS_TABLE_NAME) p ON c.SERVICECODE = p.SERVICECODE 
                WHERE 
                    c.INCURRALMONTHBRACKET >= TO_NUMBER(:MONTH_START) 
                    AND c.INCURRALMONTHBRACKET <= TO_NUMBER(:MONTH_END) 
                    AND c.CLAIMTYPEIBNR NOT LIKE 
                        CASE WHEN LOWER(:IP_FILTER) = ''exclude ip'' THEN ''Inpatient'' END
            ) AS PROPOSAL_CLAIMS
        LEFT JOIN 
            (
                SELECT 
                    programname, 
                    programtpa, 
                    providernameadjusted, 
                    providercode,
                    CASE WHEN lower(providercityadjustedgroup) = ''northern emirates'' THEN ''NE'' ELSE providercityadjustedgroup END AS providercityadjustedgroup, 
                    standardtier, 
                    CASE WHEN providertypeadjusted ILIKE ''hospital'' THEN ''Hospital'' ELSE ''Clinic'' END AS providertypeadjusted
                FROM 
                    tct_webapp.dbo.Map_Provider 
                WHERE 
                    lower(standardtier) = lower(:TIER)
                    AND lower(providercityadjustedgroup) = lower(:CITY)
                    AND lower(providertypeadjusted) = lower(:TYPE)
            ) AS PROVIDER 
        ON 
            PROPOSAL_CLAIMS.PROGRAMNAME = PROVIDER.PROGRAMNAME 
            AND PROPOSAL_CLAIMS.PROGRAMTPA = PROVIDER.PROGRAMTPA 
            AND PROPOSAL_CLAIMS.PROVIDERCODE = PROVIDER.PROVIDERCODE 
        GROUP BY 
            PROVIDER.PROVIDERNAMEADJUSTED, 
            PROVIDER.PROVIDERTYPEADJUSTED, 
            PROVIDER.PROVIDERCITYADJUSTEDGROUP, 
            PROVIDER.STANDARDTIER
         LIMIT :PAGE_SIZE
    );
BEGIN
    RETURN TABLE(res);
END;
';

我正想打电话给它

CALL GET_FILTERED_PAGINATION_PROVIDERS('202201', '202212', 'Exclude IP', 'Proposals_igor', 'Tier 1', 'Dubai', 'Clinic', 15, 15);

为什么我会收到此错误:

syntax error line 59 at position 25 unexpected '::'. (line 59)
。我查了一下,确实是 LIMIT 语句有问题。但我不明白什么。当我删除它时,一切正常。如果使用像 10 这样的硬编码值,它也可以正常工作。但不适用于争论。我做错了什么?

sql stored-procedures snowflake-cloud-data-platform arguments limit
1个回答
0
投票

这个简化的存储过程会产生相同的错误:

CREATE OR REPLACE PROCEDURE TEST_LIMIT(PAGE_SIZE INTEGER)
RETURNS TABLE (N INTEGER)
LANGUAGE SQL
AS '
DECLARE
    res RESULTSET DEFAULT (
        SELECT 1
        LIMIT :PAGE_SIZE
    );
BEGIN
    RETURN TABLE(res);
END;
';

call test_limit(3);

-- syntax error line 2 at position 24 unexpected '::'. (line 2837)

一个可能的解决方案是用查询来组成字符串:

CREATE OR REPLACE PROCEDURE TEST_LIMIT(PAGE_SIZE INTEGER)
RETURNS TABLE (N INTEGER)
LANGUAGE SQL
AS 
DECLARE
    stmt VARCHAR DEFAULT 'SELECT 1
        LIMIT ' || :PAGE_SIZE;
    res RESULTSET DEFAULT (EXECUTE IMMEDIATE :stmt);
BEGIN
    RETURN TABLE(res);
END;
;

call test_limit(3);
© www.soinside.com 2019 - 2024. All rights reserved.