雪花存储程序 CTE

时间:2021-07-06 07:40:11

标签: sql stored-procedures snowflake-cloud-data-platform common-table-expression

我想把Adventurework2017中的uspGetBillOfMaterials存储过程从sql迁移到snowflake .在 SQL 中创建 uspGetBillOfMaterials 的代码

CREATE PROCEDURE [dbo].[uspGetBillOfMaterials]
    @StartProductID [int],
    @CheckDate [datetime]
AS
BEGIN
    SET NOCOUNT ON;

    -- Use recursive query to generate a multi-level Bill of Material (i.e. all level 1 
    -- components of a level 0 assembly, all level 2 components of a level 1 assembly)
    -- The CheckDate eliminates any components that are no longer used in the product on this date.
    WITH [BOM_cte]([ProductAssemblyID], [ComponentID], [ComponentDesc], [PerAssemblyQty], [StandardCost], [ListPrice], [BOMLevel], [RecursionLevel]) -- CTE name and columns
    AS (
        SELECT b.[ProductAssemblyID], b.[ComponentID], p.[Name], b.[PerAssemblyQty], p.[StandardCost], p.[ListPrice], b.[BOMLevel], 0 -- Get the initial list of components for the bike assembly
        FROM [Production].[BillOfMaterials] b
            INNER JOIN [Production].[Product] p 
            ON b.[ComponentID] = p.[ProductID] 
        WHERE b.[ProductAssemblyID] = @StartProductID 
            AND @CheckDate >= b.[StartDate] 
            AND @CheckDate <= ISNULL(b.[EndDate], @CheckDate)
        UNION ALL
        SELECT b.[ProductAssemblyID], b.[ComponentID], p.[Name], b.[PerAssemblyQty], p.[StandardCost], p.[ListPrice], b.[BOMLevel], [RecursionLevel] + 1 -- Join recursive member to anchor
        FROM [BOM_cte] cte
            INNER JOIN [Production].[BillOfMaterials] b 
            ON b.[ProductAssemblyID] = cte.[ComponentID]
            INNER JOIN [Production].[Product] p 
            ON b.[ComponentID] = p.[ProductID] 
        WHERE @CheckDate >= b.[StartDate] 
            AND @CheckDate <= ISNULL(b.[EndDate], @CheckDate)
        )
    -- Outer select from the CTE
    SELECT b.[ProductAssemblyID], b.[ComponentID], b.[ComponentDesc], SUM(b.[PerAssemblyQty]) AS [TotalQuantity] , b.[StandardCost], b.[ListPrice], b.[BOMLevel], b.[RecursionLevel]
    FROM [BOM_cte] b
    GROUP BY b.[ComponentID], b.[ComponentDesc], b.[ProductAssemblyID], b.[BOMLevel], b.[RecursionLevel], b.[StandardCost], b.[ListPrice]
    ORDER BY b.[BOMLevel], b.[ProductAssemblyID], b.[ComponentID]
    OPTION (MAXRECURSION 25) 
END;

我已经将所有表从 SQL 迁移到雪花 我尝试用代码在雪花中做到这一点

CREATE OR REPLACE PROCEDURE proc2(StartProductID float , CheckDate datetime) -- set default startproduct id = 1 and checkdate = '2008-04-30 00:00:00.000'
RETURNS VARIANT
LANGUAGE JAVASCRIPT
EXECUTE AS CALLER
AS $$
retrieve_queries_sql = 'WITH BOM_CTE ("ProductAssemblyID", "ComponentID", "ComponentDesc", "PerAssemblyQty", "StandardCost", "ListPrice", "BOMLevel", "RecursionLevel")
    AS (
        SELECT b."ProductAssemblyID", b."ComponentID", p."Name", b."PerAssemblyQty", p."StandardCost", p."ListPrice", b."BOMLevel", 0 
        FROM "ADVENTUREWORKS2017"."PRODUCTION"."BillOfMaterials" b
            INNER JOIN "ADVENTUREWORKS2017"."PRODUCTION"."Product" p 
            ON b."ComponentID" = p."ProductID" 
        WHERE b."ProductAssemblyID" = 1
            AND '2008-04-30 00:00:00.000' >= b."StartDate" 
            AND '2008-04-30 00:00:00.000' <= IFNULL(b."EndDate", '2008-04-30 00:00:00.000')
        UNION ALL
        SELECT b."ProductAssemblyID", b."ComponentID", p."Name", b."PerAssemblyQty", p."StandardCost", p."ListPrice", b."BOMLevel", "RecursionLevel" + 1 -- Join recursive member to anchor
        FROM "BOM_CTE" cte
            INNER JOIN "PRODUCTION"."BillOfMaterials" b 
            ON b."ProductAssemblyID" = cte."ComponentID"
            INNER JOIN "PRODUCTION"."Product" p 
            ON b."ComponentID" = p."ProductID" 
        WHERE '2008-04-30 00:00:00.000' >= b."StartDate" 
            AND '2008-04-30 00:00:00.000' <= IFNULL(b."EndDate", '2008-04-30 00:00:00.000')
        )
    SELECT b."ProductAssemblyID", b."ComponentID", b."ComponentDesc", SUM(b."PerAssemblyQty") AS "TotalQuantity" , b."StandardCost", b."ListPrice", b."BOMLevel", b."RecursionLevel"
    FROM "BOM_CTE" b
    GROUP BY b."ComponentID", b."ComponentDesc", b."ProductAssemblyID", b."BOMLevel", b."RecursionLevel", b."StandardCost", b."ListPrice"
    ORDER BY b."BOMLevel", b."ProductAssemblyID", b."ComponentID"
';
retrieve_queries_result_set = snowflake.execute({sqlText: retrieve_queries_sql });

query_to_run = retrieve_queries_result_set.next().getColumnValue(1);

rs = snowflake.execute({sqlText: query_to_run})

var return_value = "";
if (rs.next())  {
      return_value += rs.getColumnValue(1);
      return_value += ", " + rs.getColumnValue(2);
      }
while (rs.next())  {
      return_value += "\n";
      return_value += rs.getColumnValue(1);
      return_value += ", " + rs.getColumnValue(2);
      }
  }
return return_value;
$$

但是 sql 语句 var 出错了,你们能帮帮我吗?谢谢

1 个答案:

答案 0 :(得分:1)

尝试使用反引号 ` 来包装 SQL 语句而不是单引号 '