尝试在雪花中创建存储过程但无法执行该过程

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

我创建了两个表

Sales_Data
Vendor_type
以及一个视图
Sales_Summary
和一个存储过程。

调用查询时,我收到此错误:

错误:第 3 行位置 4 处出现“STATMENT_ERROR”:SQL 编译错误:位置 8 处出现错误第 2 行(第 94 行)
标识符“SALES_ID”无效

以下是用于创建表、视图、插入数据和存储过程的 SQL 查询。

CREATE TABLE sales_data 
(
    sales_id INT PRIMARY KEY,
    transaction_date DATE,
    transaction_time TIME,
    daily_id INT,
    first_name STRING,
    last_name STRING,
    email STRING,
    vendor_type STRING,
    cost DECIMAL(10, 2),
    cost_code STRING,
    cost_description STRING,
    sales_rep STRING,
    sale_state STRING,
    state_tax DECIMAL(5, 2)
);

CREATE TABLE vendor_info 
(
    vendor_id INT PRIMARY KEY,
    vendor_name STRING,
    vendor_location STRING
);
CREATE VIEW sales_summary AS
SELECT 
    sd.sales_id,
    sd.transaction_date,
    sd.first_name,
    sd.last_name,
    sd.cost,
    sd.cost_description,
    vi.vendor_name
FROM 
    sales_data sd
JOIN 
    vendor_info vi ON sd.vendor_type = vi.vendor_name;
CREATE OR REPLACE PROCEDURE insert_sales_record
    (sales_id INT,
     transaction_date DATE,
     transaction_time TIME,
     daily_id INT,
     first_name STRING,
     last_name STRING,
     email STRING,
     vendor_type STRING,
     cost DECIMAL(10, 2),
     cost_code STRING,
     cost_description STRING,
     sales_rep STRING,
     sale_state STRING,
     state_tax DECIMAL(5, 2))
RETURNS STRING
LANGUAGE SQL
AS
$$
BEGIN
    INSERT INTO sales_data VALUES (
        sales_id,
        transaction_date,
        transaction_time,
        daily_id,
        first_name,
        last_name,
        email,
        vendor_type,
        cost,
        cost_code,
        cost_description,
        sales_rep,
        sale_state,
        state_tax
    );

    RETURN 'Record inserted successfully';
END;
$$;

CALL insert_sales_record(3, '2024-01-03', '11:00:00', 103, 'Mark', 'Smith', '[email protected]', 'Retail', 200.00, 'C003', 'Home Goods', 'Charlie Johnson', 'NY', 10.00);
sql stored-procedures snowflake-schema
1个回答
0
投票

您必须根据其在查询中用作变量或关键字的用法,在 Snowflake 中基于 SQL 的存储过程中将参数作为 identifier 传递。

CREATE OR REPLACE PROCEDURE <db_nm>.<sc_nm>.test_proc
    (id string)
RETURNS STRING
LANGUAGE SQL
AS
$$
BEGIN
    INSERT INTO  <db_nm>.<sc_nm>.test_abc
    (id)
    VALUES 
    (
    -----------------------------------
    :id
    -----------------------------------
    );

    RETURN 'Record inserted successfully';
END;
$$;

call <db_nm>.<sc_nm>.test_proc('123');

在示例中您可以看到我在插入查询中使用 :(冒号) 作为标识符。

© www.soinside.com 2019 - 2024. All rights reserved.