Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Grant permissions to execute Stored Procedure

Posted on 2004-08-04
16
Medium Priority
?
4,628 Views
Last Modified: 2008-01-09
I have a stored procedure that looks somewhat like this:

CREATE PROCEDURE Build_Test (@Client varchar(30) = " ")
AS
begin

 set nocount on
 
 declare @sql varchar(8000)

 set @sql = 'CREATE TABLE dbo.[' + @Client + '_Test] (' +
     '[Column1] [char] (8) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
      [Column2] [char] (3) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL
) ON [PRIMARY] '
 
 execute (@sql)
 set nocount off
end

Set @sql='GRANT  REFERENCES ,  SELECT ,  UPDATE ,  INSERT ,  DELETE  ON [' + @Client + '_Test] TO [nsmith]'  
Exec(@sql)

Set @sql='GRANT  REFERENCES ,  SELECT ,  UPDATE ,  INSERT ,  DELETE  ON [' + @Client + '_Test] TO [public]'
Exec(@sql)

Set @sql='GRANT  REFERENCES ,  SELECT ,  UPDATE ,  INSERT ,  DELETE  ON [' + @Client + '_Test] TO [ttoms]'
Exec(@sql)

Set @sql='GRANT  REFERENCES ,  SELECT ,  UPDATE ,  INSERT ,  DELETE  ON [' + @Client + '_Test] TO [ISTService]'
Exec(@sql)
GO



I also need to grant execute privileges to run the sp, since I will be calling it from an ASP page:

GRANT  EXECUTE  ON Build_Car TO [nsmith]

GRANT  EXECUTE  ON Build_Car TO [public]

GRANT  EXECUTE  ON Build_Car TO [ttoms]

GRANT  EXECUTE  ON Build_Car TO [ISTService]

I need to run the GRANT statements inside the procedure.  However, they are not triggering when I run the sp.  I had them placed right after this:

CREATE PROCEDURE Build_Car (@Client varchar(30) = " ")
AS
begin

Not working :).  What to do?

Thanks,

IPT


0
Comment
Question by:iptrader
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
  • 4
16 Comments
 
LVL 10

Expert Comment

by:ibost
ID: 11721447
I don't think you could do this unless the person calling the SP has permission to GRANT.  

Try logging in as an admin and see if it works (i suspect it would).

Why not just deploy the SP and then manually grant permissions to execute it?
0
 

Author Comment

by:iptrader
ID: 11721475
Well, the point is not to grant the permissions manually, since I want the user to execute the SP through ASP page.

I have permissions to GRANT.  If I execute the GRANT statements independently, they work, just not in the SP.
0
 
LVL 10

Expert Comment

by:ibost
ID: 11721503
are you logged in as the same user in both cases?  I ran a little test logged in as admin and it works.  Logged in as Joe User it raises an error (GRANTOR does not have permission to GRANT permission)

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:iptrader
ID: 11721517
can U show me the code U ran?
0
 
LVL 10

Expert Comment

by:ibost
ID: 11721554
It was something like this:

-- this is the procedure that i want to grant execute permissions to joeuser
create procedure blah
as
select current_timestamp
GO

create procedure grantperm
as
grant execute on blah to joeuser
GO

grant execute on grantperm to joeuser
GO

So, JoeUser logs in via QA, and execs grantperm, which failed:
Server: Msg 4613, Level 16, State 1, Procedure grantperm, Line 4
Grantor does not have GRANT permission.

Then Admin logged in via QA and execs grantperm:
The command(s) completed successfully.

0
 
LVL 10

Expert Comment

by:ibost
ID: 11721586
ahhh hold on - I just raised complexity by using dynamic sql like you and now I think I see same problem.  The SP appears to execute but permission is not, in fact, granted.
0
 
LVL 9

Accepted Solution

by:
crescendo earned 1200 total points
ID: 11721837
You can't grant permiissions to run the procedure you are creating until after it has been created, so do it separately.

Also, CREATE PROCEDURE must be run as a batch by itself, you will have to run a command to create the procedure, then run another command to grant permissions to access it.
0
 
LVL 9

Expert Comment

by:crescendo
ID: 11721841
If you are doing it in T-SQL, simply stick a GO after the end of the SP create script, then do the GRANTs.
0
 
LVL 10

Expert Comment

by:ibost
ID: 11721863
iptrader's procedure is not granting permission to itself.  

The procedure is called Build_Test

Inside the procedure, permission is being granted to table "[@Client]_Test" and procedure "Build_Car"

When I tried this out, I was able to get the non-dynamic-sql grants to work.  However the dynamic sql grants did not work (any errors seem to have been consumed because the test sp returned no errors and appeared to have worked - only problem being that permission was not, in fact, granted).

-Ian
0
 
LVL 9

Expert Comment

by:crescendo
ID: 11721919
OK, but looking at the original code, either there needs to be a GO after the "end", or the "end" needs to come after the GRANTs.
0
 
LVL 10

Expert Comment

by:ibost
ID: 11721926
ACK - nevermind.  I had set up the dynamic sql SP wrong.

Having set it up correctly, it works for an admin.  Still won't work for JoeUser (expected) because JoeUser does not have permission to GRANT.

IPTrader - does it give you any sort of error at all?
0
 
LVL 9

Expert Comment

by:crescendo
ID: 11721929
As it stands, the SP creates the table but doesn't grant access to it.
0
 
LVL 10

Expert Comment

by:ibost
ID: 11721993
oooh wait a minute - maybe I'm confused by a typo:

IPTrader, in the beginning of your post, you call the procedure "Build_Test"

Near the bottom, you seem to be referring to the same procedure as "Build_Car"

If that is the case, crescendo is correct - you can't grant the permission to a procedure before it is created.
0
 

Author Comment

by:iptrader
ID: 11722459
Sorry, name of the procedure is Build_Test.  Build_Car is something completely different.  My apologies for the confusion.  It has been a really long day :)
0
 
LVL 9

Expert Comment

by:crescendo
ID: 11724162
iptrader:

I'm confused. When you say, "I need to run the GRANT statements inside the procedure", do you mean the statements granting access to the newly-created table, or the statements granting access to the stored procedure?

If it's the latter, you have to do it separately, after you have created the procedure.

What error messages are you getting?
0
 

Author Comment

by:iptrader
ID: 11728321
Thank you all for the help.  Crescendo is right.  It was a matter of where I placed the code.

Best,

IPT
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question