对雪花中的所有存储过程授予执行权限

GRANT EXECUTE permission to ALL STORED PROCEDURES in snowflake

对雪花中的所有存储过程授予执行权限。

我已经在 snowflake 数据库中创建了一个存储过程,但在尝试执行该存储过程时出现错误。

create or replace procedure get_column_scale(column_index float)
    returns float not null
    language javascript
    as
    $$
    var stmt = snowflake.createStatement(
        {sqlText: "select EmployeeKey, EmployeeCode from stproc_test_employees;"}
        );
    stmt.execute();  // ignore the result set; we just want the scale.
    return stmt.getColumnScale(COLUMN_INDEX); // Get by column index (1-based)
    $$
    ;

我正在执行如下

CALL get_column_scale(1);

尝试使用 Snowflake 执行存储过程时出现此错误

Error [100183] [P0000]: Execution error in stored procedure GET_COLUMN_SCALE:
compilation error:
'SYEMPLOYEES' does not exist or not authorized.
Statement.execute, line 5 position 9

我认为这是我需要添加的执行权限,但我不知道在 Snowflake 中需要在哪里配置存储过程权限。

有没有人知道要授予存储 procedure/table 权限?

一些可能对您有帮助的事情。

  1. 我建议在 SELECT 语句中完全限定 table 名称,这样每当调用存储过程时, "context"用户的会话无关紧要,只要会话的当前角色可以访问 table 和模式,你就应该很好。
    完全限定的 table 具有以下形式:database_name.schema_name.object_name

    示例:hr_prod_db.hr_schema.employees

您可以在此 link 阅读有关对象名称解析的更多信息:https://docs.snowflake.net/manuals/sql-reference/name-resolution.html

  1. 我建议您花一点时间阅读有关 "Session State" 的文章 link,因为这个 link 讨论 "Caller's rights" 与 "Owner's rights" 存储过程。如果您的过程只会从具有存储过程所有者角色的会话中调用,这应该无关紧要,但如果您将过程上的 USAGE 授予另一个角色,理解这一点并正确设置它是非常重要的. https://docs.snowflake.net/manuals/sql-reference/stored-procedures-usage.html#session-state

  2. 如果您的过程将被一个会话调用,该会话将其当前角色设置为与“所有者角色”不同的角色,您需要确保对该过程进行适当的授权(和模式 + 数据库)到将要执行该过程的角色。本文档中的所有内容都在此处进行了非常详尽的概述,请特别注意这一点,因为在您的示例代码中,您有一个 table 或视图名称与您的错误消息报告的内容不同,因此 stproc_test_employees 可能是 SYEMPLOYEES 之上的视图: https://docs.snowflake.net/manuals/sql-reference/stored-procedures-usage.html#access-control-privileges 注意:When/if 您将此过程的使用权限授予另一个角色,您将需要包括参数的数据类型,例如:

    在database_name.schema_name.get_column_scale(浮动)上授予角色 other_role_name_here;

希望对你有帮助...丰富

对于 2022 年阅读此答案的人,授予执行过程权限的正确语法如下:

GRANT USAGE ON PROCEDURE
    get_column_scale(float) 
    TO ROLE other_role_name_here;