GRANT EXECUTE on a Scalar-Valued Function Syntax

Bobby P 231 Reputation points
2023-02-01T16:55:08.31+00:00

What is the syntax to GRANT EXECUTE ON FUNCTION for a Scalar-valued Function?

Thanks in advance for your help.

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
14,492 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Nandan Hegde 34,436 Reputation points MVP
    2023-02-01T18:02:02.2833333+00:00

    Hey, Can you please try Grant execute on function <> to <> where the initial bracket is the function name and the next one is for user name

    2 people found this answer helpful.
    0 comments No comments

  2. Erland Sommarskog 119.9K Reputation points MVP
    2023-02-01T22:49:24.35+00:00
    GRANT EXECUTE ON MyFun TO MyUser
    

    Or exactly the same as for a stored procedure.

    1 person found this answer helpful.
    0 comments No comments

  3. LiHongMSFT-4306 31,311 Reputation points
    2023-02-02T02:17:21.3333333+00:00

    Hi @Bobby P

    What is the syntax to GRANT EXECUTE ON FUNCTION for a Scalar-valued Function?

    You are almost here. As experts answered above, the syntax is simply like this:

    GRANT EXECUTE ON [ufnXXXX] TO [user]
    

    Also, if you want to quickly grant a user permission to all procedures or functions. Then you could try something like this:

    SELECT 'GRANT ' + CASE(ROUTINE_TYPE) 
                      WHEN 'PROCEDURE' THEN 'EXECUTE '
                      WHEN 'FUNCTION' THEN CASE(DATA_TYPE)
                                           WHEN 'TABLE' THEN 'SELECT '
                                           ELSE 'EXECUTE '
                                           END
                      END
                    + 'ON [' + ROUTINE_SCHEMA + '].[' + ROUTINE_NAME + '] TO [user];'
    FROM INFORMATION_SCHEMA.ROUTINES
    WHERE OBJECTPROPERTY(OBJECT_ID(ROUTINE_NAME),'IsMSShipped') = 0;
    

    Note: The SELECT permission must be granted on this object rather than EXECUTE as for scalar functions.

    Best regards,

    Cosmog Hong


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.