In order to make single interface for app getting data from different datasources I created one stored procedure where I try to keep all data access/handling routines. Current situation:
- Sql Server sp receives fixed number of parameters and should return recordsets taken from remote servers;
- remote servers represented as Linked Servers based on Sql Sqrver Express both sides;
- some Linked Servers are just connection points lead to local OLE DB file datasources (others have db on Sql Server itself);
- remote data structures vary a lot and for each Linked Server should be kept special list of queries with variables handling;
Example of query:
select * from OPENQUERY(linkedA,
'select f1 as id,f2 as name
from openrowset(''VFPOLEDB'',''c:\program\db\Table.FPT'';'''';
'''',''SELECT * FROM Table where f1=1'')')
When source is Sql Server based:
select * from OPENQUERY(linkedB,
'select f3 as id,f4 as name from [db].[dbo].[Table] where f3=1')
As I need to handle not only connection properties (linked server name) but query parameters (db, scheme) and variables (declared and existing in local sp runtime) I have some problems:
- Making default queries is a mess - parameters and variables parsing makes code unreadable;
- Because of handling variables I possibly need to turn to
sp_executesql
to simplify the process; - Not sure about the way of keeping query codes - should it be in
config_table
as xml or smth.
<Config id="1" name="linkedB" db="dblinkedB" scheme="dbo" version="1.0.1">
<Actions>
<Action id="0" name="Query 1">select * from OPENQUERY(linkedB,'select f3 as id,f4 as name from [db].[dbo].[Table] where f3=1')')</Action>
</Actions>
</Config>
Question:
Is there any bestpractices available?
According to subject of this topic I am looking for best way to organize query templates just passing them some parameters with possibility to use default or special code.