Solved

Explicit datasource for User Defined Function

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Azure Functions is a solution for easily running small pieces of code, or "functions," in the cloud. This article shows how to create one of these functions to write directly to Azure Table Storage.
This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
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…

717 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