Welcome to dbFreaks.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

grant in a stored proc

 
   Database Help (Home) -> Sybase RSS
Next:  changevalue not triggering with table lookup  
Author Message
Larry.Martell

External


Since: Aug 22, 2008
Posts: 2



(Msg. 1) Posted: Fri Aug 22, 2008 8:54 am
Post subject: grant in a stored proc
Archived from groups: comp>databases>sybase (more info?)

I have a stored proc that create a table, renames it, then does a
grant on it. It runs without errors, but the table does not get the
permissions granted. Here is a small test that shows what I am talking
about:

1> create procedure lmTest as
2> begin
3> exec sp_rename 'lmTestTable2','lmTestTable3'
4> select * into lmTestTable1 from lmproc_Books
5> exec sp_rename 'lmTestTable1','lmTestTable2'
6> grant select on lmTestTable2 to public
7> end
8> go
1> lmTest
2> go
Object name has been changed.
Object name has been changed.
(return status = 0)
1> sp_helprotect lmTestTable2
2> go
grantor grantee type action object column grantable
------- ------- ---- ------ ------ ------ ---------

(1 row affected)
(return status = 0)

Why doesn't the grant work?

TIA!
-larry

 >> Stay informed about: grant in a stored proc 
Back to top
Login to vote
Bret_Halford

External


Since: Jul 14, 2008
Posts: 10



(Msg. 2) Posted: Fri Aug 22, 2008 10:51 am
Post subject: Re: grant in a stored proc [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Aug 22, 9:54 am, " "
wrote:
> I have a stored proc that create a table, renames it, then does a
> grant on it. It runs without errors, but the table does not get the
> permissions granted. Here is a small test that shows what I am talking
> about:
>
> 1> create procedure lmTest as
> 2> begin
> 3> exec sp_rename 'lmTestTable2','lmTestTable3'
> 4> select * into lmTestTable1 from lmproc_Books
> 5> exec sp_rename 'lmTestTable1','lmTestTable2'
> 6> grant select on lmTestTable2 to public
> 7> end
> 8> go
> 1> lmTest
> 2> go
> Object name has been changed.
> Object name has been changed.
> (return status = 0)
> 1> sp_helprotect lmTestTable2
> 2> go
>  grantor grantee type action object column grantable
>  ------- ------- ---- ------ ------ ------ ---------
>
> (1 row affected)
> (return status = 0)
>
> Why doesn't the grant work?
>
> TIA!
> -larry

This is a general problem that is often a gotcha when using sp_rename.

The query tree for stored procedures references objects by object id.
When you rename an object, the object id doesn't change, and the query
tree continues to refer to the original object. There is a warning
about
this in the Ref Manual entry for sp_rename that basically says (though
perhaps not as clearly) - "rebuild any compiled object that references
an object you have renamed before executing it again".

If the original object is dropped, the procedure should be able to re-
resolve
the query tree to pick up the new object with the same name. But as
long
as the original object id is around, the procedure will continue to
reference it.

You can somewhat work around that here by issuing the GRANT in
a dynamic sql call, as dynamic sql isn't parsed and compiled until it
is called,
and at that point the new t1 exists.

Try this:

create procedure lmTest as
begin
exec sp_rename 'lmTestTable2','lmTestTable3'
select * into lmTestTable1 from lmproc_Books
exec sp_rename 'lmTestTable1','lmTestTable2'
execute ("grant select on lmTestTable2 to public")
end
go
lmTest
go
sp_helprotect lmTestTable2
go

Note, however, that any other procedures you have out there that
reference lmTestTable2 should be rebuilt before they are executed
again.

 >> Stay informed about: grant in a stored proc 
Back to top
Login to vote
Larry.Martell

External


Since: Aug 22, 2008
Posts: 2



(Msg. 3) Posted: Fri Aug 22, 2008 12:44 pm
Post subject: Re: grant in a stored proc [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Aug 22, 11:51 am, Bret_Halford wrote:
> On Aug 22, 9:54 am, " "
>
>
>
> wrote:
> > I have a stored proc that create a table, renames it, then does a
> > grant on it. It runs without errors, but the table does not get the
> > permissions granted. Here is a small test that shows what I am talking
> > about:
>
> > 1> create procedure lmTest as
> > 2> begin
> > 3> exec sp_rename 'lmTestTable2','lmTestTable3'
> > 4> select * into lmTestTable1 from lmproc_Books
> > 5> exec sp_rename 'lmTestTable1','lmTestTable2'
> > 6> grant select on lmTestTable2 to public
> > 7> end
> > 8> go
> > 1> lmTest
> > 2> go
> > Object name has been changed.
> > Object name has been changed.
> > (return status = 0)
> > 1> sp_helprotect lmTestTable2
> > 2> go
> > grantor grantee type action object column grantable
> > ------- ------- ---- ------ ------ ------ ---------
>
> > (1 row affected)
> > (return status = 0)
>
> > Why doesn't the grant work?
>
> > TIA!
> > -larry
>
> This is a general problem that is often a gotcha when using sp_rename.
>
> The query tree for stored procedures references objects by object id.
> When you rename an object, the object id doesn't change, and the query
> tree continues to refer to the original object. There is a warning
> about
> this in the Ref Manual entry for sp_rename that basically says (though
> perhaps not as clearly) - "rebuild any compiled object that references
> an object you have renamed before executing it again".
>
> If the original object is dropped, the procedure should be able to re-
> resolve
> the query tree to pick up the new object with the same name. But as
> long
> as the original object id is around, the procedure will continue to
> reference it.
>
> You can somewhat work around that here by issuing the GRANT in
> a dynamic sql call, as dynamic sql isn't parsed and compiled until it
> is called,
> and at that point the new t1 exists.
>
> Try this:
>
> create procedure lmTest as
> begin
> exec sp_rename 'lmTestTable2','lmTestTable3'
> select * into lmTestTable1 from lmproc_Books
> exec sp_rename 'lmTestTable1','lmTestTable2'
> execute ("grant select on lmTestTable2 to public")
> end
> go
> lmTest
> go
> sp_helprotect lmTestTable2
> go
>
> Note, however, that any other procedures you have out there that
> reference lmTestTable2 should be rebuilt before they are executed
> again.

Works like a charm! Thanks much Bret!
 >> Stay informed about: grant in a stored proc 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
logsegment blown while running stored proc - i just inherited this automated process that is filling the transaction log.... the system, creates a bcp file, bcp's the file into table A, then calls stored proc Y to copy values in A (and add some columns and modifications) into table B. since ther...

Tuning stored procedures - Hi Everyone; We are working on tuning a compact framework mobile application which uses the Sybase iAnywhere 9.0 as the database. Performance of the stored procedures have been really bad. But we are at our wits end trying to find out the Stored..

How to get around a stored procedure returning a datasset .. - Given the following sql:- create procedure sp2 (in @param integer) begin declare @sql varchar(100); if @param = 1 then set @sql = 'select 1 as a ' else set @sql = 'select 2 as b, 3 as c ' //ATTEMPT 1 //ATTEMPT 2 set @sql = 'select 4 a...

Example of how to call a stored procedure from C using dbl.. - Hello, I desperately need a small example that shows how to call a simple stored procedure that returns an integer. I need a solution in C that uses dblib. I am in an environment that is using an ancient mix of technologies. We have an old version of..

Facing problem in Executing Stored Procedure for Sybase12.0 - Dear All, I am facing a Problem in executing a SP fr Sybase12.0 Use Test1 go create table Data1(ID int,Names varchar(30),Names1 varchar(30),Primary key(ID)) go create procedure SP_Insert AS BEGIN Declare @Input int ,@Count int Set @Input = 100000 Set...
   Database Help (Home) -> Sybase All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]