all groups > sql server programming > november 2006 >
You're in the

sql server programming

group:

Parameterized Views and passing DB Name


Re: Parameterized Views and passing DB Name Stu
11/14/2006 5:12:31 PM
sql server programming:
As far as I know, you cannot use a view for this; you'll have to use
some sort of dynamic SQL in a stored procedure (assuming that your SQL
statement is short enough). Your only other option is to create a
UNION ALL view that contains the SQL statement for each database, and
include the source database as a varchar value, eg:

CREATE VIEW masterdata AS

SELECT columnlist, 'database1' as SrcDB
FROM database1.dbo.table
UNION ALL
SELECT columnlist, 'database2' as SrcDB
FROM database2.dbo.table


You could then pass your database name as a criteria to limit the
results. Of course, you'll have to figure out some form of maintenance
protocol whenever you add a new client, and performance will probably
be less than optimal.

Stu

[quoted text, click to view]
Parameterized Views and passing DB Name masmith via SQLMonster.com
11/14/2006 10:08:09 PM
I need to write some parameterized views(function that returns a table) for
our analytics department. problem is the views will be written against third
party software which creates a new database for each new client. We also can
not store any sql we design in their database. I will be creating these views
in a seperate database but would like to pass the database name to the view.
Or is their any easier way then passing the db name as a parameter to make
the view work for any db with the same data structure.

--
Message posted via http://www.sqlmonster.com
Re: Parameterized Views and passing DB Name masmith via SQLMonster.com
11/15/2006 12:00:00 AM
[quoted text, click to view]
When I say view I mean a function that returns a table. Is their any other
way to do this without using dynamic sql

--
Message posted via http://www.sqlmonster.com
AddThis Social Bookmark Button