【问题标题】:How to execute Snowflake Stored Procedure from Python?如何从 Python 执行雪花存储过程?
【发布时间】:2021-07-12 13:04:09
【问题描述】:

我已经在雪花中创建了存储过程,该存储过程在雪花 UI 中以及使用 snowsql 从服务器中执行得很好。现在我想从 python 程序执行程序,我尝试从 python 执行,以下是我遵循的步骤:

  1. 建立与雪花的连接(成功连接。)

cs = ctx.cursor()

  1. 使用了适当的角色、仓库、数据库和架构。
  2. 试图执行这样的程序:

cs.execute("call test_proc('value1', 'value2')")   
x = cs.fetchall() 
print(x)

但出现错误:

snowflake.connector.errors.ProgrammingError: 002140 (42601): SQL 编译错误:未知函数test_proc

你能帮我解决这个问题吗?

谢谢,

【问题讨论】:

    标签: python-3.x stored-procedures snowflake-cloud-data-platform


    【解决方案1】:

    当使用 Python connector 连接到 Snowflake 时,您可以定义 DATABASE/SCHEMA

    conn = snowflake.connector.connect(
                    user=USER,
                    password=PASSWORD,
                    account=ACCOUNT,
                    warehouse=WAREHOUSE,
                    database=DATABASE,
                    schema=SCHEMA
                    );
    

    设置完成后,您可以在不使用完全限定名称的情况下调用您的存储过程:

    cs.execute("call test_proc('value1', 'value2')");
    

    另一种方法是:

    Using the Database, Schema, and Warehouse

    指定要在其中创建表的数据库和架构。还要指定为执行 DML 语句和查询提供资源的仓库。

    例如要使用数据库testdb、模式testschema和仓库tiny_warehouse(之前创建的):

    conn.cursor().execute("USE WAREHOUSE tiny_warehouse_mg")
    conn.cursor().execute("USE DATABASE testdb_mg")
    conn.cursor().execute("USE SCHEMA testdb_mg.testschema_mg")
    

    【讨论】:

      【解决方案2】:

      其实我得有这样的命令

      cs.execute("调用 yourdbname.schemaname.test_proc('value1', 'value2')")

      它正在按预期工作。

      谢谢

      【讨论】:

      • 如果您在连接字符串中连接到数据库和架构,那么您的第一次尝试也会成功。
      猜你喜欢
      • 2021-10-01
      • 2021-08-25
      • 1970-01-01
      • 2020-12-10
      • 1970-01-01
      • 2020-05-23
      • 1970-01-01
      • 2021-06-22
      • 2021-10-17
      相关资源
      最近更新 更多