Click here to Skip to main content
13,549,306 members
Click here to Skip to main content
Add your own
alternative version


40 bookmarked
Posted 19 Sep 2004

Tool to script Linked Servers in SQL Server 2000

, 19 Sep 2004
Rate this:
Please Sign up or sign in to vote.
Tool to generate script for Linked Servers in SQL Server 2000, using C# and SQLDMO.


SQL Server presently does not provide option to generate script for linked servers on an existing instance it provides for other objects which we see as "Generate SQL Scripts" on context menus. The tool provided in this article creates script for linked servers and gives an option to either save the script to a file or run it immediately on another server. The script generated uses undocumented SQL Server extended stored procedures to access registry and can only be used by a sysadmin. There are number of ways to create this script. I found SQLDMO very useful because of the ease with which we can traverse through the SQLDMO objects and convert the properties into SQL statements. The same effort with any other way, like using ADO or stored procedure calls, would have taken more lines of code and complicated the programming logic. One problem with SQLDMO is that it does not have the "Disallow Adhoc Access" provider option in the SQLDMO_SRVOPTION_TYPE enumeration (I do not know why!). To overcome this limitation, we can use a xp_instance_regread or xp_instance_regenumvalues call to get the value and generate the script.

Using the tool

Screen shot below shows the tool in action:

Tool to script linked servers on SQL Server

To run the program:

  • Give the SQL Server instance name in the combo box, or press "Get Servers List" button to populate the list to select one.
  • Give the user name and password, or check "Trusted Connection".
  • Press "Script Linked Servers" to generate and put the script in the text box. Note: A password will be missing in sp_addlinkedsrvlogin statements generated, it should be provided for the SQL scripts to run properly.
  • You may want to:
    • copy the SQL statements to the clipboard
    • save them to a file
    • run them against a SQL Server directly from the tool itself by pressing "Create Linked Servers from Script"

Code Details

A reference to Microsoft SQLDMO Object Library under COM libraries is added to the project references, and a using SQLDMO; statement takes care of using the namespace in the code. Most of the scripting statements are inside the OnScript() event handler which uses the helper functions GetProviderOptionScript(), GetOnOffValue(), and GetSQLStringValue() in the code. Now, let us take a closer look at the OnScript() method. The following code generates the sp_addlinkedserver SQL statement. Since linked servers are server level objects, we iterate through the LinkedServers collection:

foreach (LinkedServer2 ls in sqlserver.LinkedServers)
    scriptText += "sp_addlinkedserver '" + ls.Name + "','" + ls.ProductName<BR>               + "'";

    // script Provider Parameters if the product is not "SQL Server"
    if (ls.ProductName != "SQL Server")
        scriptText += ",'" + ls.ProviderName +
                      "','" + ls.DataSource +
                      "','" + ls.Location + "','"
                      + ls.ProviderString + "','" + ls.Catalog + "'";

    scriptText += "\ngo\n";

Next, the logins used by the linked servers are scripted by the following code segment. We use a helper function GetSQLStringValue() which will return null or the string value itself. In SQLDMO, get property is not supported on passwords! Hence, we substitute the passwords in the script with PASSWORD_STRING (defined in my program as 'enter password here'):

// script the linked server logins
foreach (LinkedServerLogin login in ls.LinkedServerLogins)
    scriptText += ("sp_addlinkedsrvlogin '" + ls.Name + "','" +
                  ((login.Impersonate)? "true" : "false") + "',"
                  + GetSQLStringValue(login.LocalLogin)
                  + "," + GetSQLStringValue(login.RemoteUser) +
                  "," + PASSWORD_STRING + "\ngo\n");

After this, the linker server options are scripted by the code shown below. It is interesting to note the difference between the Connection Timeout and Query Timeout values passed to the sp_serveroption stored procedure. The stored procedure accepts numeric value for Connection Timeout and string value for Query Timeout! For all other options, which are represented as bits inside SQL Server (except Collation Name), the GetOnOffValue() helper function is used to return a "on" or "off" value after checking if the bit is set or not.

// script the following Server Options -
// Collation Compatible, Data Access, RPC, RPC Out,
//        Use Remote Collation, Collation Name,
//        Connection Timeout, Query Timeout

scriptText += ("sp_serveroption '" + ls.Name + "','collation compatible',"
           + GetOnOffValue(ls.Options & 
           + "\ngo\n");

scriptText += ("sp_serveroption '" + ls.Name + "','data access',"
           + GetOnOffValue(ls.Options & 
           + "\ngo\n");

scriptText += ("sp_serveroption '" + ls.Name + "','rpc',"
          + GetOnOffValue(ls.Options & SQLDMO_SRVOPTION_TYPE.SQLDMOSrvOpt_RPC)
           + "\ngo\n");

scriptText += ("sp_serveroption '" + ls.Name + "','rpc out',"
      + GetOnOffValue(ls.Options & SQLDMO_SRVOPTION_TYPE.SQLDMOSrvOpt_RPC_out)
           + "\ngo\n");
scriptText += ("sp_serveroption '" + ls.Name + "','use remote collation',"
           + GetOnOffValue(ls.Options & 
           + "\ngo\n");
scriptText += ("sp_serveroption '" + ls.Name + "','collation name',"
           + GetSQLStringValue(ls.CollationName) + "\ngo\n");
scriptText += ("sp_serveroption '" + ls.Name + "','connect timeout'," 
           + ls.ConnectTimeout + "\ngo\n");
scriptText += ("sp_serveroption '" + ls.Name + "','query timeout','" 
           + ls.QueryTimeout + "'\ngo\n");

At the end of the method, the Provider Options are scripted. It should be noted that these options are at the provider level and not at the linked server level. I.e., we may be using the same provider for a number of linked servers. That is why it is enough if we script the options only once for every provider. The providersListForOptions string is used to keep track of the providers for which the script is already generated, to avoid duplicate scripting. I think it's better if SQLDMO can provide these options under a separate collection at server level instead of attaching them to a LinkedServer object.

It should be noted that these provider options are stored by SQL Server in the registry under HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSSQLServer\Providers\PROVIDER key as values. The GetProviderOptionScript() helper method generates the code to write into or delete these values from the registry. We use undocumented extended stored procedures called xp_regdeletevalue and xp_regwrite inside the helper function as given below.

One point to be noted - I do not know why SQLDMO does not have the "Disallow Adhoc Access" option in the SQLDMO_SRVOPTION_TYPE enumeration. You can find it on any SQL Server by opening the Add Linked Server dialog box. If anybody has any idea, please share it here.

Also, to overcome this limitation, we can use a xp_instance_regread or xp_instance_regenumvalues call to get the value and generate the script.

    private string GetProviderOptionScript(SQLDMO_SRVOPTION_TYPE options,
                                                SQLDMO_SRVOPTION_TYPE check,
                                                string value_name,
                                                string provider)
        string optionString = "exec master.." 
           + (((options|check) == 0)?"xp_regdeletevalue ":"xp_regwrite ")
           + @"'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\MSSQLServer\Providers\"
           + provider + "','" + value_name + "'"
           + (((options|check) == 0)? "" : ",'REG_DWORD',1");

         return optionString + "\ngo\n";

    private void OnScript(object sender, System.EventArgs e)
        // script the Provider Options if the product is not "SQL Server"
        // send them into registry directly
        if (providersListForOptions.IndexOf(ls.ProviderName, 0) == -1)
            scriptText += "\n-- Options for provider : " + ls.ProviderName<BR>                       + "\n";
            // add the provider to the list
            providersListForOptions += (ls.ProviderName + ",");
            scriptText += (
                    "DynamicParameters", ls.ProviderName) +
                    "NestedQueries", ls.ProviderName) +
                    "LevelZeroOnly", ls.ProviderName) +
                    "AllowInProcess", ls.ProviderName) +
                    "NonTransactedUpdates", ls.ProviderName) +
                    "IndexAsAccessPath", ls.ProviderName));
    catch(Exception ex)

The rest of the code is self explanatory.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Sriram Chitturi
United States United States
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionMessage Closed Pin
9-Mar-12 21:53
membersangram3609-Mar-12 21:53 
GeneralAwsome Pin
Member 373600518-Feb-10 5:59
memberMember 373600518-Feb-10 5:59 
GeneralGood Tool! Pin
JffThChf7-Sep-06 5:30
memberJffThChf7-Sep-06 5:30 
GeneralIncorrect Provider Options created via script Pin
DoyleB14-Feb-06 12:16
memberDoyleB14-Feb-06 12:16 
Generalwith error, Install Pin
99chips12-Aug-05 7:03
member99chips12-Aug-05 7:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.180515.1 | Last Updated 19 Sep 2004
Article Copyright 2004 by Sriram Chitturi
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid