Solved

Compile-time conditions in stored proc?

Posted on 2013-06-05
5
191 Views
Last Modified: 2013-06-07
I have some stored procs that reference a few linked servers in a distributed transaction.  This works OK in live, but in my DR location I have fewer actual machines, and they contain databases that reside on separate machines in live.
Under the circumstances, i would have to use loop-back linked server names in DR, which would kill the distributed trans, or I would have to have two versions of the code - for DR and Live, that reference actual machine names - which pretty much defeats the purpose of disaster recovery.
I have tried solving the issue by adding conditions like
IF @@SERVERNAME = 'SQLPROD1' EXEC SQLPROD2.MyDB.dbo.mySP
ELSE EXEC SQLDR2.MyDB.dbo.mySP

Open in new window

Problem with that is - if either SQLPROD2 or SQLDR2 is offline - the SP fails, because referential integrity is validated at "prepare", and an inaccessible server throws an error, even though it would not be actually referenced at all at execution.
Is there a way of adding conditional statements that would be evaluated at "prepare" stage?
Or perhaps someone could suggest a different way of achieving what I am after?
0
Comment
Question by:COANetwork
  • 2
  • 2
5 Comments
 
LVL 23

Assisted Solution

by:Racim BOUDJAKDJI
Racim BOUDJAKDJI earned 150 total points
ID: 39222812
The fundamental problem you are facing here is that you are trying to solve applicatively an architecture problem.  As a dirty hack you can eventually want to try something like this to fool the compiler (in pseudo code).

if exists( linked server (SQLPROD2) or  linked server (SQLDR2))
drop linked server SQLPROD2
drop linked server SQLDR2

IF @@SERVERNAME = 'SQLPROD1'
begin 
create linked server SQLPROD2...
EXEC SQLPROD2.MyDB.dbo.mySP
drop linked server SQLPROD2...
end
ELSE 
begin
create linked server SQLDR2...
EXEC SQLDR2.MyDB.dbo.mySP
drop linked server SQLDR2...
end

Open in new window


For creating linked servers applicatively, you can look at the following link:

http://msdn.microsoft.com/en-us/library/ms190479.aspx

Hope this helps.
0
 
LVL 23

Accepted Solution

by:
nemws1 earned 350 total points
ID: 39223931
I'm guessing you can get around this by using dynamic SQL:

DECLARE @SQL VARCHAR(max) = NULL;
IF (@@SERVERNAME = 'SQLPROD1')
BEGIN
    SET @SQL='EXEC SQLPROD2.MyDB.dbo.mySP';
    EXEC @SQL;
END
ELSE
BEGIN
    SET @SQL='EXEC SQLDR2.MyDB.dbo.mySP'
    EXEC @SQL;
END                     

Open in new window

0
 
LVL 23

Expert Comment

by:Racim BOUDJAKDJI
ID: 39224123
<<I'm guessing you can get around this by using dynamic SQL:>>
Definitely a more elegant solution.  You just me realize I have a bias onto not mixing up dynamic SQL and multi server distributed transactions for security purposes and error feed backing reasons.  Thanks (I do mean it) :)
0
 
LVL 23

Expert Comment

by:nemws1
ID: 39224515
Glad I had a good idea.  Usually you school me. ;-)
0
 
LVL 9

Author Comment

by:COANetwork
ID: 39229920
Both viable ideas, guys.  thanks...  I have to say that dropping and recreating linked servers sounds a bit clumsy to me, though, and dynamic SQL introduces its own complications because my stored procs have output parameters.  Besides, isn't there a performance hit with dynamic SQL because it has to re-generate the execution plan?  Or is that a moot point, because SPs (and their execution plans) are atomic, anyway, even when sub-called?
Actually, there is yet another complication to this - I also have a QA environment where server names are yet again different, but there I also have the added complication of database names being different (MyDB_QA1, MyDB_QA2, MyDB_Dev, etc)  I do not think that problem can be resolved by architectural changes, as originally pointed out - I have to have these multiple copies of the databases.  I guess I will have to go the dynamic SQL route...  Thanks again.
0

Featured Post

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

746 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now