Advertisement

06.10.2008 at 10:31AM PDT, ID: 23473192 | Points: 500
[x]
Attachment Details

How to run stored procedures in database A while logged into database B?

Asked by dqmq in SQL Server 2005, MS SQL Server

HI,
I need some help setting up an SQL Server environment. I have a database that is chock full of my favorite utility procedures and functions that I use all the time at different client sites.  As it stands, in order to run those utilities, I install them in the client database and all is fine.  However, that creates some administrative/logistic headaches for me:
  --I install the procs/functions is with a CREATE PROC script, which means I need to maintain that every time I add a new procedure or one of them changes.
  --some clients understandably resist the intrusion of my scripts into their databases.  Frankly, I think it's an obnoxious practice myself.
  --purging the objects when I leave is a hassle

It would be much easier if I could just attach my database to the client's server and run the scripts from there:  1. Simple installation/de-installation  2. No introduction of my objects into the client database.

But, I can't get it work.  Suppose my utilities are in database U, which is attached to the client's server S with client database C.  I want to run my utilities in database U against the tables in database C.   I want to do this without explicitly naming database C in my utilities, since I want them to be portable.  

To simplify by example, see the code snippet.

When that runs, I want it to see the tables from C, but it shows the tables from U, instead.  In other words, my database context is the database where the proc is stored, not the one I am connected to or running against.

Specifically, my questions are:
1.  Is there something simple that I am overlooking?  Can I get my example to show the results from the context of the database C (without hardcoding the database name)?

2.  Is there a better way to accompish my goal of a easily porting a package of utilities? What's the best practice?





 

 


 



Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
use u
go
create proc dbo.ShowTables as select name from sys.tables
go
create table dbo.Utable (i int)
go
 
use c
go
create table dbo.Ctable (i int)
go
 
exec u.dbo.showtables
go
 
 
Name
-----------------------------------------
UTable
 
 
[+][-]06.10.2008 at 11:20AM PDT, ID: 21753610

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.10.2008 at 11:49AM PDT, ID: 21753875

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.11.2008 at 04:05PM PDT, ID: 21764861

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.12.2008 at 08:14AM PDT, ID: 21770392

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.13.2008 at 09:05PM PDT, ID: 21784031

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628