473,473 Members | 1,900 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

Jul 23 '05 #1
10 5692
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

Jul 23 '05 #2
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.

Jul 23 '05 #3
(bs******@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, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #4
>> 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.

Jul 23 '05 #5
--CELKO-- (jc*******@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, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #6
>> .. 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.

Jul 23 '05 #7

"--CELKO--" <jc*******@earthlink.net> wrote in message
news:11*********************@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.

Jul 23 '05 #8
>> 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.

Jul 23 '05 #9
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.

Jul 23 '05 #10
--CELKO-- (jc*******@earthlink.net) writes:
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.
If you first hit the limits in Access, and believe you have hit limits
in SQL Server, then I can only congratulate to a very expanding business.
I don't know where Access bites the dust, but would not consider it for
a 1GB database. Neither do I know when SQL Server caves in, but since
people run terabyte databases in SQL Server, so the margin to Access to
SQL Server.

Of course, with poor indexing you can make any engine go awfully slow.

The major reason for partitioning is probably so much query speed,
bu manageability. If the billings for last months never changes,
you don't really want to back them up each time. And if you routinely
purge them after, say, 12 months, then you want to do that quickly.
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,.


Of course. Most database products on the market are propritary. The
exception are the open-source products, but my impression is that
MySQL are not really ripe for the terabyte market yet.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Andrew Dodgshun | last post by:
I have 2 grids - one shows a list of table names in a database and when you click on a table name the other grid dynamically populates the grid with the table contents. My problem is that I cannot...
2
by: Wayne Wengert | last post by:
I want to write a Windows application to go through all the email addresses in an SQL Server table and to report which ones are invalid. From Googling and perusing NGs it is my understanding that...
3
by: akadelski | last post by:
I need a way to verify a file exists before I attempt to open it. Right now we are storing PDFs server-side and I need a way to either 1. Check if the physical file exists or 2. check if the URL...
0
by: jpr | last post by:
Hello, I need some help. I have a form named MASTER based on a table also called MASTER. A control of my form in names SSN which stores the client SSN. On the same form I have placed a subform...
3
by: jpr | last post by:
Hello, I have a form on which I have a cmdbutton to copy a couple of fields into another table (MASTER) using the SSN on the active form as criteria. In the active form (based on a tables...
2
by: SM | last post by:
Hello, I've created this 'wonderful' function the embeds a youtube video in a specified div section using the Javascript DOM. Everything works OK... until I realize how bad the logical programming...
4
by: m.wanstall | last post by:
I have a crosstab query that compiles data for Months of the year. I have a stacked select query on top of that crosstab query that uses the latest 2 months data and exports it to a fixed length...
29
by: Quarco | last post by:
This one is driving me nuts.... var tbl = document.createElement("table"); var tbody = document.createElement("tbody"); for(var i=0; i<10; i++) { var row =...
7
by: sasimca007 | last post by:
Hello friends, What my doubt is, a page is already designed in that page a table exists, and i am doing a script like a radio button exixts and when click that radio button...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.