Solved

How to reference multiple db's in T-sql

Posted on 2011-02-17
4
216 Views
Last Modified: 2012-05-11
I use one db for storing data, it is built from a restore of another db.  We'll call this db REPORT.  Another database I put my actual stored procedures and functions on. I'll call this one SPS

Typically this isn't an issue, we simply reference the appropriate db in the FROM clause, but I want to know how I can make it so I can have T-SQl pointed to look in specific places for functions.  For isntance, I don't want to have to type 'SPS.DBO.FUNCTIONNAME()' everytime I want to use one of those functions.  How can I make it so I just have to type the function name?
0
Comment
Question by:UnderSeven
  • 2
4 Comments
 
LVL 13

Expert Comment

by:devlab2012
ID: 34918082
you can use as three part name as DBName.Schema.Objectname. Objectname is your table or sp etc. You can simply ignore schema

e.g.

select * from MyDB..MyTable
0
 

Author Comment

by:UnderSeven
ID: 34918159
Is there anyway to include it like you would in a c language? So I can avoid writing anything but the function?
0
 
LVL 40

Accepted Solution

by:
Sharath earned 250 total points
ID: 34918162
In your code, you can use USE keyword. For functions, you should use 2-part naming conventions as dbo.functionName().

Use DBName
... select * from dbo.your_function()
exec your_sp
etc....
etc....

Open in new window

0
 

Author Closing Comment

by:UnderSeven
ID: 34933396
Thanks.
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Many companies are looking to get out of the datacenter business and to services like Microsoft Azure to provide Infrastructure as a Service (IaaS) solutions for legacy client server workloads, rather than continuing to make capital investments in h…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question