Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

On demand linked server creation in SP Expand / Collapse
Author
Message
Posted Thursday, May 8, 2014 7:50 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, June 5, 2014 1:45 PM
Points: 6, Visits: 6
I have a stored procedure that creates a linked server as one of the first lines of the procedure and then proceeds to use it for some calculations.

It worked great in SQL 2005, but now that I've upgraded to SQL 2012 it will not allow me to execute, telling me that my linked server does not exist.

Is there a way around this in 2012?
Post #1568894
Posted Thursday, May 8, 2014 8:13 AM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Today @ 9:43 AM
Points: 4,324, Visits: 10,589
The creation of the linked server fails for some reason.
You didn't post the actual error message, so it's nearly impossible to give sensible advice.


--
Gianluca Sartori

Get your two-cent-answer quickly
spaghettidba.com
@spaghettidba
Post #1568913
Posted Thursday, May 8, 2014 8:19 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, June 5, 2014 1:45 PM
Points: 6, Visits: 6
The creation of the linked server does not fail. I copied the exact line of code and ran it separately on a SQL 2012 machine to create the linked server for a test and that succeeded. It just doesn't like it in the procedure in 2012. The same procedure has been running in SQL 2005 for years.

The error I get is:

Msg 7202, Level 11, State 2, Procedure ,<my procedure>, Line 49
Could not find server 'MyLinkedServer' in sys.servers. Verify that the correct server name was specified. If necessary, execute the stored procedure sp_addlinkedserver to add the server to sys.servers.

I'll add that if I create the linked server first and execute the procedure it does allow me to execute and I get the expected error that I explicitly raise telling me that the linked server already exists.
Post #1568918
Posted Thursday, May 8, 2014 8:37 AM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Today @ 9:43 AM
Points: 4,324, Visits: 10,589
Weird. Can you post the code?

--
Gianluca Sartori

Get your two-cent-answer quickly
spaghettidba.com
@spaghettidba
Post #1568928
Posted Thursday, May 8, 2014 8:41 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, June 5, 2014 1:45 PM
Points: 6, Visits: 6
I have figured something out.

If I create the linked server manually and then run the procedure I get an explicitly raised error that I wrote in to tell me that the server already exists.

If I manually drop the linked server and then execute the procedure it works as expected. I ran it several times with success.

I then ran <dbcc freeproccache>, attemped the procedure again and the <Could not find server 'MyLinkedServer' in sys.servers> error returned.

So the question becomes, how can I force this procedure to compile without the linked server present at the time of compilation?
Post #1568936
Posted Thursday, May 8, 2014 9:05 AM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Today @ 9:43 AM
Points: 4,324, Visits: 10,589
Use dynamic sql.

--
Gianluca Sartori

Get your two-cent-answer quickly
spaghettidba.com
@spaghettidba
Post #1568955
Posted Thursday, May 8, 2014 9:08 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, June 5, 2014 1:45 PM
Points: 6, Visits: 6
Sounds good. I don't know why I didn't think of that.

Thanks!
Post #1568961
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse