Solved

Explicit datasource for User Defined Function

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL SELECT query help 7 56
is there any function in ms sql that can format date this way: yymmdd hhmmss 2 18
SQL DATEADD 10 69
PolyServe for SQL server 12 30
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
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.
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…

792 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