Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Data Management

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

    Search

    XML Feeds

    Google Ads

    « Getting started with Execution PlansDatabase Projects - Installing the Database Project Template »
    comments

    I once did some work for a company and noticed that they were running as sysadmin. When I asked why, their answer was that the stored procedures would not work otherwise. This is very bad practice, in general I create a user, and then give execute permissions to the procedures. In some instances the user might also get reader and writer permissions.

    Yesterday I got a request from a person who wanted to be able to change two stored procedures on the staging server. There is no need to make this person a db_owner, you can just give the ALTER proc permissions. There are two flavors of the syntax

    1. GRANT ALTER  ON OBJECT::ProcName TO UserName
    2. GRANT ALTER  ON  ProcName TO UserName

    Let's take a look at this by writing some code. First create a new database

    1. CREATE DATABASE Test
    2. GO

    Create a user in that database

    1. USE [master]
    2. GO
    3. CREATE LOGIN [TestUser] WITH PASSWORD=N'Test', DEFAULT_DATABASE=[test], CHECK_EXPIRATION=OFF, CHECK_POLICY=OFF
    4. GO
    5. USE [test]
    6. GO
    7. CREATE USER [TestUser] FOR LOGIN [TestUser]
    8. GO

    Now create this very simple stored procedure

    1. CREATE PROC prTest
    2. AS
    3. SELECT GETDATE()

    Open up another connection, login as TestUser with the password Test. If you try to execuce the stored procedure you will get an error

    1. EXEC prTest

    Msg 229, Level 14, State 5, Procedure prTest, Line 1
    The EXECUTE permission was denied on the object 'prTest', database 'test', schema 'dbo'.

    In the window where you have all the permissions, give execute permissions to this stored procedure to the TestUser. You can use GRANT EXECUTE ON ProcName TO UserName to accomplish this, there is no need to give additional privileges like db_owner or sysadmin

    1. GRANT EXECUTE  ON prTest  TO TestUser

    Now you will see that TestUser can execute the stored procedure.

    If TestUser wants to modify the stored procedure you can give TestUser ALTER StoredProc permissions. First let's see what happens if TestUser tries to modify the stored procedure before we gave the permissions

    1. ALTER PROC prTest
    2. AS
    3. SELECT GETDATE()

    And here is the error

    Msg 3701, Level 14, State 20, Procedure prTest, Line 3
    Cannot alter the procedure 'prTest', because it does not exist or you do not have permission.

    Execute the following in the window where you have all the permissions

    1. GRANT ALTER ON prTest TO TestUser

    Now run this again in the window where you are logged in as TestUser

    1. ALTER PROC prTest
    2. AS
    3. SELECT GETDATE()

    As you now saw, this succeeded.

    You can also take away privileges, execute the following in the window where you have all the permissions.

    1. REVOKE ALTER ON prTest  TO TestUser

    Now when you try to alter the stored procedure again it will fail.

    Here is the other way to give permissions, I like the other syntax better, it is shorter and there are no double colon characters

    1. GRANT ALTER  ON OBJECT::prTest TO TestUser

    Now trying to modify the stored procedure will work again.

    There you have it, it is simple to give users access to modify only the stored procedures that you want, no need to give elevated privileges that might bite you in the butt down the road

    About the Author

    User bio imageDenis has been working with SQL Server since version 6.5. Although he worked as an ASP/JSP/ColdFusion developer before the dot com bust, he has been working exclusively as a database developer/architect since 2002. In addition to English, Denis is also fluent in Croatian and Dutch, but he can curse in many other languages and dialects (just ask the SQL optimizer) He lives in Princeton, NJ with his wife and three kids.
    Social SitingsTwitterFacebookLinkedInHomePageFlickrLTD RSS Feed
    2864 views
    InstapaperVote on HN

    2 comments

    Comment from: Keith Howlette [Visitor] · http://www.rckeith.co.uk
    Keith Howlette Good information, but I have several developers that change many stored procs so it could be a bit time consuming this way. Is there a better way. I agree don't like giving db_owner but its the only way I found so far.

    Keith
    01/09/13 @ 07:07
    Comment from: SQLDenis [Member] Email
    SQLDenis Yes, there is another way, you can also give the user ALTER SCHEMA permissions. Check out my schema post to learn more about schemas



    Basically the command will look like this

    GRANT ALTER ON SCHEMA::SchemaName TO UserName

    In this case it would be

    GRANT ALTER ON SCHEMA::dbo TO TestUser

    Now the user can alter any object in that database, keep that in mind.

    Another option is making the user part of the db_ddladmin fixed database role, of course now you can do a lot of other things as well
    01/09/13 @ 07:20

    Leave a comment


    Your email address will not be revealed on this site.

    To mislead the spambots.

    Your URL will be displayed.
    (Line breaks become <br />)
    (Name, email & website)
    (Allow users to contact you through a message form (your email will not be revealed.)