How to extract dynamically data from another database in function with DBName as parameter?
1
vote
3
answers
2909
views
I have one main database (
MAIN
) and several client DBs (CLIENTDB
). I need to start query in MAIN
and to get data from CLIENTDB
. The client DB can be found also on linked server. My problem is that I need to do this in a function with EXEC
, so I can get SELECT * FROM SRV.CLIENTDB
, but this in not allowed by SQL Server. Is there another way to do that?
I need to have code like inside the function:
DECLARE @sSRV AS VARCHAR(128) = 'SRV';
DECLARE @sDB AS VARCHAR(128) = 'CLIENTDB1';
EXEC('SELECT * FROM ' + sSRV + '.' + @sDB + '.MyTable');
I need a function, because I need to make joins with it inside my engine.
Asked by Bogdan Bogdanov
(1163 rep)
Dec 14, 2016, 09:57 AM
Last activity: Dec 14, 2016, 04:42 PM
Last activity: Dec 14, 2016, 04:42 PM