Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Deploying Stored Procedures in Visual Studio - GRANT EXECUTE

Deploying Stored Procedures in Visual Studio - GRANT EXECUTE

Scheduled Pinned Locked Moved Database
databasesysadmincsharpsql-serverdotnet
10 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mike Ellison
    wrote on last edited by
    #1

    Hi all. Is there a way to automatically grant execute permissions on CLR stored procedures upon deployment from a Visual Studio SqlServer project? I have a solution with a project for all my Sql Server stored procs, and a second project for a web site that tests them. When I rebuild/run the web site, the stored procedure assembly is rebuilt and re-deployed also (which is good) but because the stored procedures are recreated, the GRANT permissions previously on them are lost (which is not so good). I'd like to have the GRANT EXECUTE permissions added as an automatic part of the deployment process, if that is possible. Help?

    W 1 Reply Last reply
    0
    • M Mike Ellison

      Hi all. Is there a way to automatically grant execute permissions on CLR stored procedures upon deployment from a Visual Studio SqlServer project? I have a solution with a project for all my Sql Server stored procs, and a second project for a web site that tests them. When I rebuild/run the web site, the stored procedure assembly is rebuilt and re-deployed also (which is good) but because the stored procedures are recreated, the GRANT permissions previously on them are lost (which is not so good). I'd like to have the GRANT EXECUTE permissions added as an automatic part of the deployment process, if that is possible. Help?

      W Offline
      W Offline
      Wendelius
      wrote on last edited by
      #2

      Instead of rebuilding (dropping and creating) the procedures, could you use ALTER PROCEDURE syntax? If that's not an option, perhaps you could include a plain script to the deployment (depending how you do the deployment).

      The need to optimize rises from a bad design.My articles[^]

      M 1 Reply Last reply
      0
      • W Wendelius

        Instead of rebuilding (dropping and creating) the procedures, could you use ALTER PROCEDURE syntax? If that's not an option, perhaps you could include a plain script to the deployment (depending how you do the deployment).

        The need to optimize rises from a bad design.My articles[^]

        M Offline
        M Offline
        Mike Ellison
        wrote on last edited by
        #3

        Mika Wendelius wrote:

        Instead of rebuilding (dropping and creating) the procedures, could you use ALTER PROCEDURE syntax? If that's not an option, perhaps you could include a plain script to the deployment (depending how you do the deployment).

        If I were writing the deployment script, then sure, but I was hoping to stay within Visual Studio - it's just a lot more convenient to click the "run" button, have it go through the build and deployment, and run the web app. I was hoping that I've missed an option along the way that supports the execution of custom SQL scripts upon deployment... or some other means of addressing the stored procedure permissions. I haven't found anything like that in VS. You know, something like "Deployment Events..." similar to "Build Events..." would be exactly the thing.

        W 1 Reply Last reply
        0
        • M Mike Ellison

          Mika Wendelius wrote:

          Instead of rebuilding (dropping and creating) the procedures, could you use ALTER PROCEDURE syntax? If that's not an option, perhaps you could include a plain script to the deployment (depending how you do the deployment).

          If I were writing the deployment script, then sure, but I was hoping to stay within Visual Studio - it's just a lot more convenient to click the "run" button, have it go through the build and deployment, and run the web app. I was hoping that I've missed an option along the way that supports the execution of custom SQL scripts upon deployment... or some other means of addressing the stored procedure permissions. I haven't found anything like that in VS. You know, something like "Deployment Events..." similar to "Build Events..." would be exactly the thing.

          W Offline
          W Offline
          Wendelius
          wrote on last edited by
          #4

          Have you tried post build events? I'm not sure, but I think it's run as the last operation in building sequence. I did a little test and it seems that the post build command line (or actually I used a script) was run after the full build was done, but I think I may have a different situation since the target directory was actually the web folder.

          The need to optimize rises from a bad design.My articles[^]

          M 1 Reply Last reply
          0
          • W Wendelius

            Have you tried post build events? I'm not sure, but I think it's run as the last operation in building sequence. I did a little test and it seems that the post build command line (or actually I used a script) was run after the full build was done, but I think I may have a different situation since the target directory was actually the web folder.

            The need to optimize rises from a bad design.My articles[^]

            M Offline
            M Offline
            Mike Ellison
            wrote on last edited by
            #5

            Mika Wendelius wrote:

            Have you tried post build events? I'm not sure, but I think it's run as the last operation in building sequence. I did a little test and it seems that the post build command line (or actually I used a script) was run after the full build was done, but I think I may have a different situation since the target directory was actually the web folder.

            Hi Mika. Thanks for the replies. I guess that might work - a post-build event on the web project that issues the GRANT EXECUTE statements to the server... I can try that. It just seems like there should be a way to do this when compiling and deploying the stored procedures themselves, but the above might be a workaround. Thanks again.

            W 1 Reply Last reply
            0
            • M Mike Ellison

              Mika Wendelius wrote:

              Have you tried post build events? I'm not sure, but I think it's run as the last operation in building sequence. I did a little test and it seems that the post build command line (or actually I used a script) was run after the full build was done, but I think I may have a different situation since the target directory was actually the web folder.

              Hi Mika. Thanks for the replies. I guess that might work - a post-build event on the web project that issues the GRANT EXECUTE statements to the server... I can try that. It just seems like there should be a way to do this when compiling and deploying the stored procedures themselves, but the above might be a workaround. Thanks again.

              W Offline
              W Offline
              Wendelius
              wrote on last edited by
              #6

              Mike Ellison wrote:

              the above might be a workaround.

              If you have time, let us know if it worked. I think that there could be other workarounds also, but nothing comes in mind that would be any better than post event build. One thing I was thinking is that if the grant statements are fixed, could you actually do this in the database. What I mean is that if you would create a DDL trigger using CREATE_PROCEDURE event type, you could catch the procedure creation at the database and then execute the necessary grants...

              Mike Ellison wrote:

              Thanks again

              You're welcome :)

              The need to optimize rises from a bad design.My articles[^]

              M 1 Reply Last reply
              0
              • W Wendelius

                Mike Ellison wrote:

                the above might be a workaround.

                If you have time, let us know if it worked. I think that there could be other workarounds also, but nothing comes in mind that would be any better than post event build. One thing I was thinking is that if the grant statements are fixed, could you actually do this in the database. What I mean is that if you would create a DDL trigger using CREATE_PROCEDURE event type, you could catch the procedure creation at the database and then execute the necessary grants...

                Mike Ellison wrote:

                Thanks again

                You're welcome :)

                The need to optimize rises from a bad design.My articles[^]

                M Offline
                M Offline
                Mike Ellison
                wrote on last edited by
                #7

                Hi Mika. I ended up using a DDL trigger like the following:

                CREATE TRIGGER GrantExecuteTrigger on DATABASE
                FOR CREATE_PROCEDURE
                AS
                declare @user varchar(64)
                set @user = 'whateverUserWeWant'

                declare @data xml
                set @data = EVENTDATA()

                declare @sql varchar(max)
                set @sql = 'GRANT EXECUTE ON '
                + @data.value('(/EVENT_INSTANCE/ObjectName)[1]', 'varchar(256)')
                + ' TO ' + @user

                execute(@sql)

                GO

                This did work... every time the assembly/stored procs are deployed from Visual Studio, the trigger is applying the GRANT EXECUTE permission accordingly. I will just have to remember to adjust permissions on any stored procs that I would work with outside the VS context. Much of what I read regarding this issue indicated there was a Script.PostDeployment.sql file that could be modified for this purpose, but I wonder if that is limited to the Database edition of Team Studio... we have the Architecture edition, and it doesn't seem like the same facility exists there (unless I'm completely missing it).

                W 1 Reply Last reply
                0
                • M Mike Ellison

                  Hi Mika. I ended up using a DDL trigger like the following:

                  CREATE TRIGGER GrantExecuteTrigger on DATABASE
                  FOR CREATE_PROCEDURE
                  AS
                  declare @user varchar(64)
                  set @user = 'whateverUserWeWant'

                  declare @data xml
                  set @data = EVENTDATA()

                  declare @sql varchar(max)
                  set @sql = 'GRANT EXECUTE ON '
                  + @data.value('(/EVENT_INSTANCE/ObjectName)[1]', 'varchar(256)')
                  + ' TO ' + @user

                  execute(@sql)

                  GO

                  This did work... every time the assembly/stored procs are deployed from Visual Studio, the trigger is applying the GRANT EXECUTE permission accordingly. I will just have to remember to adjust permissions on any stored procs that I would work with outside the VS context. Much of what I read regarding this issue indicated there was a Script.PostDeployment.sql file that could be modified for this purpose, but I wonder if that is limited to the Database edition of Team Studio... we have the Architecture edition, and it doesn't seem like the same facility exists there (unless I'm completely missing it).

                  W Offline
                  W Offline
                  Wendelius
                  wrote on last edited by
                  #8

                  Mike Ellison wrote:

                  I wonder if that is limited to the Database edition of Team Studio

                  Seems that i's only in Database Edition, http://msdn.microsoft.com/en-us/library/aa833410.aspx[^]

                  Mike Ellison wrote:

                  I will just have to remember to adjust permissions on any stored procs that I would work with outside the VS context.

                  If you want to put some time to this, one way could be that you: - create a table in your db where you will store the privileges that you want to grant to procedures (also grantees) - include a file containing the data for the grants to your VS project - using pre build events, first delete the grants from the table in the database - add new rows to the table based on the previous file (you could use sqlcmd or osql for this) Now when the procedure is created, your trigger could read the grant "profiles" from that table so you could control what privileges are granted using the file in VS project.

                  The need to optimize rises from a bad design.My articles[^]

                  M 1 Reply Last reply
                  0
                  • W Wendelius

                    Mike Ellison wrote:

                    I wonder if that is limited to the Database edition of Team Studio

                    Seems that i's only in Database Edition, http://msdn.microsoft.com/en-us/library/aa833410.aspx[^]

                    Mike Ellison wrote:

                    I will just have to remember to adjust permissions on any stored procs that I would work with outside the VS context.

                    If you want to put some time to this, one way could be that you: - create a table in your db where you will store the privileges that you want to grant to procedures (also grantees) - include a file containing the data for the grants to your VS project - using pre build events, first delete the grants from the table in the database - add new rows to the table based on the previous file (you could use sqlcmd or osql for this) Now when the procedure is created, your trigger could read the grant "profiles" from that table so you could control what privileges are granted using the file in VS project.

                    The need to optimize rises from a bad design.My articles[^]

                    M Offline
                    M Offline
                    Mike Ellison
                    wrote on last edited by
                    #9

                    Thanks for the suggestion, Mika, and I appreciate the conversation you and I have been having on this. It was very helpful.

                    W 1 Reply Last reply
                    0
                    • M Mike Ellison

                      Thanks for the suggestion, Mika, and I appreciate the conversation you and I have been having on this. It was very helpful.

                      W Offline
                      W Offline
                      Wendelius
                      wrote on last edited by
                      #10

                      Mike Ellison wrote:

                      It was very helpful.

                      Glad to hear that :) You're welcome.

                      The need to optimize rises from a bad design.My articles[^]

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups