Unix Technical Forum

Verify dynamically specified table exists

This is a discussion on Verify dynamically specified table exists within the SQL Server forums, part of the Microsoft SQL Server category; --> I need to write a stored procedure to verify that a table exists and also that the user executing ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-29-2008, 07:09 AM
bsandell@gmail.com
 
Posts: n/a
Default Verify dynamically specified table exists

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

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-29-2008, 07:09 AM
Simon Hayes
 
Posts: n/a
Default Re: Verify dynamically specified table exists

You can use the PERMISSIONS() function (see Books Online) to see if a
user has permissions on a certain object.

Although you don't say what your goal is, I would be wary of your
approach - if you don't know which table a user needs to access
runtime, then you will probably have to use dynamic SQL heavily, which
is usually a bad idea:

http://www.sommarskog.se/dynamic_sql.html

Depending on what you're trying to do, there may be better options
available, such as using stored procs, a reporting tool, etc. If you
can give more details of what you need to accomplish, someone may have
a suggestion.

Simon

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-29-2008, 07:10 AM
bsandell@gmail.com
 
Posts: n/a
Default Re: Verify dynamically specified table exists

Hi Simon,

Thanks for the info and the link to the dynamic SQL article. The
article was excellent. I will attempt to explain what the goal of the
procedure is. We have an application that stores metadata about
certain tables that our application has processed. Users would like to
be able to duplicate this metadata for tables that have the same
structure as one for which we already store metadata. For example, if
there is already metadata stored for a table called BILLING_CURRENT,
and there is a monthly process that renames the BILLING_CURRENT table
to BILLING_MMYY (month and year), and creates a new empty table called
BILLING_CURRENT, I would like to be able to replicate all of the
relevant metadata for the table BILLING_CURRENT table to the
BILLING_MMYY table. This is a pretty generic example, the table could
also get replicated in another database for reporting, etc. The stored
procedure which I have written to do this takes 6 parameters, the
database, owner and tablename of the original table ( which form a
unique key to the metadata table), and the database, owner and
tablename of the new table. Before I actually replicate the metadata,
I would like to be sure that the requesting user actually has
sufficient access to the table they are requesting to have the metadata
copied for. If they aren't able to select from the table, then they
shouldn't be able to replicate metadata for that table.

Your suggestion to use the persissions() function is a great idea, but
won't it only apply to the current database? It seems like I'll still
need some dynamic sql to get what I'm after. FYI this stored procedure
is not something that will be run frequently. Once a day would
probably be pretty heavy usage.

I hope this is somewhat clear, and thanks again for the help.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-29-2008, 07:11 AM
Erland Sommarskog
 
Posts: n/a
Default Re: Verify dynamically specified table exists

(bsandell@gmail.com) writes:
> We have an application that stores metadata about
> certain tables that our application has processed. Users would like to
> be able to duplicate this metadata for tables that have the same
> structure as one for which we already store metadata. For example, if
> there is already metadata stored for a table called BILLING_CURRENT,
> and there is a monthly process that renames the BILLING_CURRENT table
> to BILLING_MMYY (month and year), and creates a new empty table called
> BILLING_CURRENT, I would like to be able to replicate all of the
> relevant metadata for the table BILLING_CURRENT table to the
> BILLING_MMYY table. This is a pretty generic example, the table could
> also get replicated in another database for reporting, etc. The stored
> procedure which I have written to do this takes 6 parameters, the
> database, owner and tablename of the original table ( which form a
> unique key to the metadata table), and the database, owner and
> tablename of the new table. Before I actually replicate the metadata,
> I would like to be sure that the requesting user actually has
> sufficient access to the table they are requesting to have the metadata
> copied for. If they aren't able to select from the table, then they
> shouldn't be able to replicate metadata for that table.


The idea is that the schema in a relational database is supposed to be
static. It could be changed with new versions of the application being
installed, but adding new tables during run-time goes against the
spirit.

Not knowing why all this copying take place, it's a little difficult
to suggest alternatives. But in the one example you give with a billing
table, the normal thing to do would simply be to add MMYY as a column
in the table, and have one table for all billings.

> Your suggestion to use the persissions() function is a great idea, but
> won't it only apply to the current database? It seems like I'll still
> need some dynamic sql to get what I'm after.


Yes, it seems that you would have some dynamic SQL which performs
a USE on the source database, and then assigns the result of permissions()
into an output parameter. You would use sp_executesql for this.



--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-29-2008, 07:11 AM
--CELKO--
 
Posts: n/a
Default Re: Verify dynamically specified table exists

>> We have an application that stores metadata about certain tables
that our application has processed. <<

Mixing data and meta-data in a schema is always a fundamental design
mistake.

>> there is already metadata stored for a table called BILLING_CURRENT,

and there is a monthly process that renames the BILLING_CURRENT table
to BILLING_MMYY (month and year), .. <<

You have re-discovered a version of the old IBM magnetic tape label
convention (yyddd) and put it into an RDMS over 50 years later. "Those
who cannot remember the past are condemned to repeat it." --George
Santayana.

You have missed the whole point of relational models. This is a
version of attribute splitting; you have taken a temporal attribute and
made it's values into tables. You need to start over after you have
gotten some data modeling training.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 02-29-2008, 07:11 AM
Erland Sommarskog
 
Posts: n/a
Default Re: Verify dynamically specified table exists

--CELKO-- (jcelko212@earthlink.net) writes:
> You have missed the whole point of relational models. This is a
> version of attribute splitting; you have taken a temporal attribute and
> made it's values into tables. You need to start over after you have
> gotten some data modeling training.


That's the thoery. Real life is apparently somewhat different. Or else
SQL Server would not have partitioned views, and in SQL 2005 also add
partitioned tables, so actually permit you to have billing_0501,
billing_0502 (or whatever), although you can still view it as one big
table as well.

> You have re-discovered a version of the old IBM magnetic tape label
> convention (yyddd) and put it into an RDMS over 50 years later. "Those
> who cannot remember the past are condemned to repeat it." --George
> Santayana.


The underlying problem is really the same, it is just that relational
databases and most of all the hardware development that have pushed
the limits where you need to resort to this sort of thing.



--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 02-29-2008, 07:12 AM
--CELKO--
 
Posts: n/a
Default Re: Verify dynamically specified table exists

>> .. partitioned views, and in SQL 2005 also add partitioned tables,
so actually permit you to have billing_0501, billing_0502 (or
whatever), although you can still view it as one big table as well. <<

Red Brick went even further with kind of STORAGE design, since it was
one of the first products built for OLAP. I don't care how the data is
PHYSICALLY stored as long as I see it has a normalized schema with
correct data. I do not want to have to manage it myself -- I am not as
smart as a good storage optimizer (see Teradata and its hashing
algorithm).

>> The underlying problem is really the same, it is just that

relational databases and most of all the hardware development that have
pushed the limits where you need to resort to this sort of thing. <<

We have the hardware for VLDB apps and it is cheap. The real problem
is the choice of software and programming. People start off with a
small app in ACCESS, then find that it is a pain to port to SQL Server
because the languages are so different. Then they find that SQL
Server also hits a limit.

Rather than make the step up to a new platform, they kludge for awhile
with tricks like this, and try to get speed from highly proprietary
code,. This makes the code even harder than before to move to a larger
RDBMS. So they have to start over when it gets critical.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 02-29-2008, 07:12 AM
Greg D. Moore \(Strider\)
 
Posts: n/a
Default Re: Verify dynamically specified table exists


"--CELKO--" <jcelko212@earthlink.net> wrote in message
news:1114384868.392936.62120@z14g2000cwz.googlegro ups.com...
>
> We have the hardware for VLDB apps and it is cheap. The real problem
> is the choice of software and programming. People start off with a
> small app in ACCESS, then find that it is a pain to port to SQL Server
> because the languages are so different. Then they find that SQL
> Server also hits a limit.


One thing that I'm hoping will help is that more people will use SQL Express
instead of Access. Makes a lot of these porting/upgrading questions kind of
moot.


>
> Rather than make the step up to a new platform, they kludge for awhile
> with tricks like this, and try to get speed from highly proprietary
> code,. This makes the code even harder than before to move to a larger
> RDBMS. So they have to start over when it gets critical.
>



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 02-29-2008, 07:12 AM
--CELKO--
 
Posts: n/a
Default Re: Verify dynamically specified table exists

>> I'm hoping will help is that more people will use SQL Express
instead of Access. <<

I hope ACCESS dies. I was COMDEX when they presented it to the world
and the Trade Press for the first time. It sorted dates alphabetically
by month and gave a Blue Screen of Death when Gates demoed it. On the
other hand, Foxpro and "Dr. Dave" had a flawless demo on the same
stageand equipment. This piece of crap has never been close to
Standard SQL and the engine is so bad that it probably can not ever be
made to perform correctly. I was also on retainer to the ACCESS group
for a year.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 02-29-2008, 07:12 AM
bsandell@gmail.com
 
Posts: n/a
Default Re: Verify dynamically specified table exists

Thanks everyone for all your input on this topic. Just to clarify a
few points -

1. The metadata is in a separate schema from the user's data.
2. I have nothing to do with the existing application design or
implementation. I just have to provide a tool that's flexible enough
to work in this existing environment, among others, regardless of what
I think of any exisitng implementation decisions.

Again, thanks for all the input. I think that the sp_executesql
suggestion looks like what I'm after.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 08:32 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com