Re: Linked Server: How to check if server exists?



I adapted this from the object explorer scripting feature.
The root problem is described in the Books Online topic Batches.

Rules for Using Batches
The following rules apply to using batches:
- CREATE DEFAULT, CREATE FUNCTION, CREATE PROCEDURE, CREATE RULE, CREATE SCHEMA, CREATE TRIGGER, and CREATE VIEW statements cannot be combined with other statements in a batch.

I don't have a way around that.
--
Rick Byham (MSFT), SQL Server Books Online
This posting is provided "AS IS" with no warranties, and confers no rights.

"Axel Dahmen" <keentoknow@xxxxxxxxxxxxxxxx> wrote in message news:u3EUxdXVJHA.3908@xxxxxxxxxxxxxxxxxxxxxxx
Thanks, Rick, for trying to help.

Unfortunately putting the Stored Procedure in a string will lose all IntelliSense features and colour highlighting, plus all strings will have to be escaped.

Do you perhaps have some other solution at hand?

TIA,
www.axeldahmen.de
Axel Dahmen




----------------------
"Rick Byham, (MSFT)" <rickbyh@xxxxxxxxxxxxxxxxxxxxxxxxxx> schrieb im Newsbeitrag news:4A81D37A-14CD-47BB-AA01-16FB9F22FCF0@xxxxxxxxxxxxxxxx
This should get you started:

IF EXISTS (SELECT name FROM sys.servers WHERE name = N'linkedserver')
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE dbo.ExportToSMS
AS
SELECT * FROM [linkedserver].[db].[schema].[table]'
END
ELSE
PRINT 'No such linked server.'
--
Rick Byham (MSFT), SQL Server Books Online
This posting is provided "AS IS" with no warranties, and confers no rights.

"Axel Dahmen" <keentoknow@xxxxxxxxxxxxxxxx> wrote in message
news:Oxio$AVVJHA.2928@xxxxxxxxxxxxxxxxxxxxxxx
Hi,

in our development project not all of use have a linked server entry at our
local SQL Server installations.

At compile time we're running a couple of SQL scripts to update our local
databases.

Unfortunately the Stored Procedures containing statements addressing a
linked server don't run on those development machines not having that linked
server entry yet.

Now I'd like to know if it is possible to conditionally have the CREATE
PROCEDURE statement run, depending on a condition.


The following doesn't work:

-----------------------------------------------
IF EXISTS (SELECT NULL FROM sys.servers WHERE name = N'linkedserver')
BEGIN
CREATE PROCEDURE dbo.ExportToSMS
AS
SELECT * FROM [linkedserver].[db].[schema].[table]
END
-----------------------------------------------


even the following doesn't work:

-----------------------------------------------
CREATE PROCEDURE dbo.ExportToSMS
AS
IF EXISTS (SELECT NULL FROM sys.servers WHERE name = N'linkedserver')
BEGIN
SELECT * FROM [linkedserver].[db].[schema].[table]
END
-----------------------------------------------

Can someone please enlighten me on how to achieve a conditional CREATE
PROCEDURE?

TIA,
www.axeldahmen.de
Axel Dahmen


.



Relevant Pages

  • Re: How to import excel data to tempdb
    ... Create a Linked server to the Excel spreadsheat. ... Books Online. ... See sp_addlinkedserver and sp_addlinkedsrvlogin in SQL Server ...
    (microsoft.public.sqlserver.server)
  • Re: Trigger to populate table or database
    ... I've got a linked server to an Access database, ... just replace "mastertbl" with LINKEDSERVER...mastertbl. ... Erland Sommarskog, SQL Server MVP, esquel@xxxxxxxxxxxxx ... Books Online for SQL Server 2005 at ...
    (comp.databases.ms-sqlserver)
  • Re: ODBC in Stored Procedure
    ... I don't want to use linked server. ... OLE object that you could call from a stored procedure, ... Erland Sommarskog, SQL Server MVP, esquel@xxxxxxxxxxxxx ... Books Online for SQL Server 2005 at ...
    (comp.databases.ms-sqlserver)
  • Re: Need Help Importing Data from Excel to My SQL Database
    ... The OLE DB provider "Microsoft.Jet.OLEDB.4.0" for linked server ... To me it sounds like SQL Server cannot find Excel at all. ... Books Online for SQL Server 2005 at ...
    (comp.databases.ms-sqlserver)
  • Re: Query training -- Complex queries
    ... > Update PositionsEOM ... Here you don't have a derived table, but a correlated subquery. ... UPDATE in Books Online. ... is not written for SQL Server. ...
    (microsoft.public.sqlserver.server)