Solved

Explicit datasource for User Defined Function

Posted on 2009-06-30
4
244 Views
Last Modified: 2013-11-16
Im trying to use a function with two different data sources. For example I have a user defined function called GetNewCustomer. Lets say for example sake one data source is our New York customers and one is for California Customers. I would expect that I could explicitly declare one function with the NY data source and call the other one with the California data source like this:

newyork.CustomerDB.GetNewCustomer
california.CustomerDB.GetNewCustomer

Yet when run in a query or spreadsheet I just return whichever one is the active data source its like the declaration on the data source is ignored. Any suggestions?
0
Comment
Question by:keymarkIT
4 Comments
 
LVL 31

Expert Comment

by:RiteshShah
ID: 24744810
I don't get your 100% but I guess you can create two schema (owner) and create two function with each schema and call it in the way you want.
0
 
LVL 17

Accepted Solution

by:
pssandhu earned 500 total points
ID: 24744890
I think you will have to pass the datasourcename as a parameter to the function and use dynamic SQL to change datasource names. Something like this maybe:
Select * From CustomerDB.dbo.GetNewCustomer('california')
And in the Fucntion you can have something like this:
ALTER FUNCTION GetNewCustomer(@DS varchar(50))
   RETURNS Table
AS
DECLARE @SQL varchar(250)
SET @SQL = 'Select * From '+ @DS +'.CustomerDB.dbo.TableName'
RETURN EXEC (@SQW)
I am assuming Data Source name is another SQL Server
P.
0
 

Author Comment

by:keymarkIT
ID: 24924215
Pssandhu, Im going to give this a try. This seems to be the logical approach. I will get back to you after testing.

Thanks.
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

786 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