Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

referencing function in another database

Posted on 2002-06-17
2
Medium Priority
?
316 Views
Last Modified: 2008-03-06
I have a user defined function in one database.  Within query analyer, I would call the function with this syntax dbo.myfunction(). my function is in the first database.  I'm writing a stored procedure and want to call this function within an instance of another database.  I can't seem to reference my function outside of the first database.

Thanks
0
Comment
Question by:tinman1412
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 6

Accepted Solution

by:
curtis591 earned 200 total points
ID: 7083983
select database.dbo.function(parameters) should do it for you.
0
 

Expert Comment

by:AdiCohn
ID: 7084024
 You need to specify the database's name.  DatabaseName.FunctionOwner.FunctionName.  Here is a small example:

USE pubs
GO

--Creating a small function in pubs database
create function MyTest() returns int
as
begin
return (1)
end

GO

USE northwind
GO

--Creating a stored procedure in the northwind database
create procedure MyStored
as
declare @t int
--Notice the way that I specify the location of the
--function.
set @t = pubs.dbo.MyTest()
print @t
return

GO

Exec MyStored
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
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.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

636 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