December 1, 2009 at 1:48 pm
I have dozens of stored procedures pointing to another server and at times they need to point to another server. Reporting, ETLs, etc.
All my procedures are straight up TSQL. How would I best encapsulate data sources so I could change one value and be pointing at another server?
After the fact (months of development based on another dbas work), I realize that if it was done in dynamic TSQL, we could store the servername in the database and change it whenever we need.
I'm using SSIS for some ETLs and love the datasource in that application, but nothing obvious stands out in SQL Server. I'm not sure if I just got painted into a corner through bad practices or if I just haven't discovered the proper door yet.
My ad hoc solution at this point would be to create a linked server and have all the TSQL use this reference and change where it points where I need. Are there drawbacks with doing this?
December 1, 2009 at 1:55 pm
One method to be able to vary any item in a select statement is to use a string method to create the statement and then execute the string .. e.g. .. DECLARE @sSQL as Varchar(8000): SET @sSQL = "SELECT ... ":
By using a parameter for the Stored Procedure, you can then use an IF statement to select what source to bolt onto the string. e.g. ...
If @SourceID = 1 .... SET @sSQL = "FROM myserver1.db1.dbo.tbl1"
If @SourceID = 2 .... SET @sSQL = "FROM myserver2.db2.dbo.tbl2"
Then use the EXEC (@sSQL) to run the statement.
Always more than one method though....
December 1, 2009 at 2:34 pm
This may help you:
December 1, 2009 at 2:36 pm
No, dynamic SQL definitely is a good solution. given this case a year in the past, thats the route I would have gone... or likely through SSIS for the case of the import, not the reports.
At this point debugging a 9 hour ETL (in one case) is a good argument for not rewriting everything. Could take weeks to debug if I introduce errors.
-- Thank you for that information on the SYNONYM. Great solution for the reporting where you can pass in an indication of which server, use dynamic SQL to create the SYNONYM, then normal TSQL to report off of it.
December 1, 2009 at 3:08 pm
That is exactly what I was going to suggest.. Synonyms are a great way to "hide" exactly what is on the other end and this is a perfect example of why they work so well.
I have one caution, if the other end is SQL 2000 you may have intermittent issues.
Please see:
MS Connect Article on the problem related to SQL 2000 endpoint
CEWII
December 1, 2009 at 5:49 pm
Agreed... SYNONYMs are great. If you can't use them for some reason, a "pass through" view will do the same wonderful thing except instead of ALTER SYNONYM, you would use ALTER VIEW instead.
--Jeff Moden
Change is inevitable... Change for the better is not.
December 2, 2009 at 8:12 am
I moved from pass-thru views to SYNONYMs on a recent project. Love it..
CEWII
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply