Solved

Explicit datasource for User Defined Function

Posted on 2009-06-30
4
237 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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Analysis of table use 7 30
SQL server 2008 SP4 29 33
MySQL ERROR 1045 (28000) 2 40
Auditing in Azure SQL Database 3 28
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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…
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…

757 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

19 Experts available now in Live!

Get 1:1 Help Now