Solved

How to reference multiple db's in T-sql

Posted on 2011-02-17
4
189 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
Shadow IT is coming out of the shadows as more businesses are choosing cloud-based applications. It is now a multi-cloud world for most organizations. Simultaneously, most businesses have yet to consolidate with one cloud provider or define an offic…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now