I need to write a stored procedure to verify that a table exists and
also that the user executing the stored procedure has access to the
specified table.
Any user can call this publicly available procedure and pass a database
name, an owner name and a table name as parameters. The procedure
returns success if the table exists and the user has access to it, or
fails if he doesn't. Here's a simplified version of what I have, but
I'm wondering if there's a better way. Thanks.
create procedure dumb as
begin
declare @myError int,
@mytable varchar(128),
@myquery varchar(128)
select @mytable = '[Northwind].[dbo].[sysobjects2]'
select @myquery = 'DECLARE @x int SELECT @x = count(1) from ' +
@mytable + ' where 1 = 2'
exec (@myquery)
select @myError = @@ERROR
if @myError != 0
BEGIN
RAISERROR ('ERROR: The specified table %s cannot be accessed.', 10, 1,
@mytable)
RETURN 1
end
end
go
							
						
					also that the user executing the stored procedure has access to the
specified table.
Any user can call this publicly available procedure and pass a database
name, an owner name and a table name as parameters. The procedure
returns success if the table exists and the user has access to it, or
fails if he doesn't. Here's a simplified version of what I have, but
I'm wondering if there's a better way. Thanks.
create procedure dumb as
begin
declare @myError int,
@mytable varchar(128),
@myquery varchar(128)
select @mytable = '[Northwind].[dbo].[sysobjects2]'
select @myquery = 'DECLARE @x int SELECT @x = count(1) from ' +
@mytable + ' where 1 = 2'
exec (@myquery)
select @myError = @@ERROR
if @myError != 0
BEGIN
RAISERROR ('ERROR: The specified table %s cannot be accessed.', 10, 1,
@mytable)
RETURN 1
end
end
go
	
	
Comment