all groups > sql server programming > june 2004 >
You're in the

sql server programming

group:

Testing Table Existence - DB name as variable ?


RE: Testing Table Existence - DB name as variable ? Nigel Rivett
6/27/2004 9:23:01 AM
sql server programming:
if object_id(['+ @SalespersonDB + '].[dbo].[tblCustPrClChanlSlsREP])) is not null
BEGIN
Print 'Do nothing'
END
ELSE
BEGIN
Print 'Do something'
End

For your dynamic sql version you would have to use sp_executesql to set a varriable and use that in the if statement.
see
http://www.nigelrivett.net/sp_executeSQL.html

[quoted text, click to view]
Testing Table Existence - DB name as variable ? rob
6/27/2004 9:29:33 AM
I am doing something wrong in the code below...

This section fails within the cursor... I am attempting to check for
existence of a table in the database names I am looping through...

As is, Error is Incorrect syntax near keyword 'ELSE'
If I comment out from BEGIN to the END, Error is Incorrect syntax near ')'

Thanks !

DECLARE @sql as varchar(8000)
DECLARE @SalespersonDB as varchar(8000)

set @SalespersonDB = 'EJ'
set @sql = ' if exists (select * from dbo.sysobjects where id = object_id(['
+ @SalespersonDB + '].[dbo].[tblCustPrClChanlSlsREP]))'
print @sql
Exec(@sql)
BEGIN
Print 'Do nothing'
END
ELSE
BEGIN
Print 'Do something'
End

Re: Testing Table Existence - DB name as variable ? Nigel Rivett
6/27/2004 12:05:23 PM
If you aren't worried about the object type then

if object_id(@SalespersonDB + '..tblCustPrClChanlSlsREP') is not null

For your query better to do an exists rather than a count - will stop if
it fnds one rather than scanning the whole table.



DECLARE @sql as nvarchar(4000)
DECLARE @SalespersonDB as varchar(8000)
DECLARE @cnt as varchar(10)

select @SalespersonDB = 'Ej', @cnt = 'notexists'

set @sql = 'Select @cnt = ''exists'' where exists (select * FROM ' +
@SalespersonDB + '.dbo.sysobjects WHERE (xtype = ''U'') and name =
''tblCustPrClChanlSlsREP'')'
print @sql
Exec sp_executesql @sql, N'@cnt varchar(10) OUTPUT', @cnt OUTPUT
print @cnt

Nigel Rivett
www.nigelrivett.net

*** Sent via Devdex http://www.devdex.com ***
Re: Testing Table Existence - DB name as variable ? Dan Guzman
6/27/2004 1:00:49 PM
4000 is the max length for nvarchar. Should be:

DECLARE @sql as nvarchar(4000)

--
Hope this helps.

Dan Guzman
SQL Server MVP

[quoted text, click to view]

Re: Testing Table Existence - DB name as variable ? rob
6/27/2004 1:10:36 PM
Hi Roji,

Changed it to the following in order to store the results to a variable...

DECLARE @sql as varchar(8000)
DECLARE @SalespersonDB as varchar(8000)
DECLARE @cnt as nvarchar(10)

set @SalespersonDB = 'EJ'

set @sql = 'Select @cnt = cast(Count(*) as nvarchar(10)) FROM ' +
@SalespersonDB + '.dbo.sysobjects WHERE (xtype = ''U'') and name =
''tblCustPrClChanlSlsREP'''
print @sql
Exec sp_executesql @sql, N'@cnt nvarchar(10) OUTPUT', @cnt OUTPUT
print @cnt

Resulted in ERROR....
Server: Msg 214, Level 16, State 2, Procedure sp_executesql, Line 10
Procedure expects parameter '@statement' of type 'ntext/nchar/nvarchar'.



[quoted text, click to view]

Re: Testing Table Existence - DB name as variable ? Imran Koradia
6/27/2004 1:31:11 PM
thats what sql server expects the type of @sql. declare ur @sql variable as:
DECLARE @sql as nvarchar(8000)

check out http://www.sommarskog.se/dynamic_sql.html for more info.

hope this helps..

[quoted text, click to view]

Re: Testing Table Existence - DB name as variable ? Roji. P. Thomas
6/27/2004 8:11:02 PM
Oh Rob,

You are trying to mix dynamic SQL and Static SQL.
You cannot do that. Your Else condition doesnt have a
corresponding IF condition. thats why you are getting the error.

To learn more abt dynamic SQL read

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


--
Roji. P. Thomas
SQL Server Programmer
[quoted text, click to view]

Re: Testing Table Existence - DB name as variable ? David Portas
6/27/2004 11:38:27 PM
It appears that you have a separate DB for each sales person. Why? The most
logical design would seem to be to add a salesperson column to your table(s)
in a single database. That would avoid the need for all this messy dynamic
SQL.

--
David Portas
SQL Server MVP
--

Re: Testing Table Existence - DB name as variable ? rob
6/28/2004 8:47:36 AM
Thanks David,

I have about 20 different salespeople all using the same client tool to
access their data via ODBC. They ABOLUTELY cannot view each others data.
They will have permissions to their database only. The client tool they
will be using will be the same for each salesperson, just different
connction info, table names etc. remain the same for each client tool.
Obviously, there is a salesperson column in the primary database.



[quoted text, click to view]

Re: Testing Table Existence - DB name as variable ? rob
6/28/2004 10:58:59 AM
Thanks again for the response David...

[quoted text, click to view]
to
just the data they own.

Would it then require separate sp's for each saleperson ? Or would you use
a stored parameter in the MSAccess client tool ? Problem is that if a
parameter were ever changed, or a client tool set-up error occurred by
mistake, and data was viewed by the wrong salesperson, it would cause a
complete meltdown.

[quoted text, click to view]
creates a management headache, makes development
needlessly complex (your question in this thread for example) and makes it
cumbersome to write Decision Support queries (find the top selling
salesperson for example).

Basically, there is a primary database that feeds each of the salesperson
databases, each salesperson updates their MS Access client tool to review
data and make forecast changes... the query to find the Top selling
saleperson would be easy becasue it would be run against the Primary
(feeder) database.

Please respond back if you still think this is a bad route....




[quoted text, click to view]

Re: Testing Table Existence - DB name as variable ? David Portas
6/28/2004 3:17:47 PM
Then allow them to view the database only through SPs that restrict them to
just the data they own. Segmenting databases in the way you have is
unnecessary and it just creates a management headache, makes development
needlessly complex (your question in this thread for example) and makes it
cumbersome to write Decision Support queries (find the top selling
salesperson for example).

--
David Portas
SQL Server MVP
--

Re: Testing Table Existence - DB name as variable ? rob
6/29/2004 9:47:17 AM

David,

I have been thinking alot about your point of view... is there any other way
to provide "row level" permissions ? Other than a client tool parameter,
or separate sp's... I figured that by setting up one database per
saleperson, 2 mistakes must occur for a salesperson to see another
salespersons data 1) the wrong connection parameters would have to be used
when setting up the MSAccess client tool, and 2) they would have to
incorrectly assign permissions to the wrong database.

Thanks




[quoted text, click to view]

Re: Testing Table Existence - DB name as variable ? Hugo Kornelis
6/29/2004 10:32:32 PM
[quoted text, click to view]

Hi Rob,

A common way to provide row level permissions is the use of views. From
BOL, subject "Using Views as Security Mechanisms":

--> start quote
By defining different views and granting permissions selectively on them,
users, groups, or roles can be restricted to different subsets of data.
For example:

Access can be restricted to a subset of the rows of a base table. For
example, define a view that contains only rows for business and psychology
books and keep information about other types of books hidden from users.
<-- end quote


Best, Hugo
--

AddThis Social Bookmark Button