Link to home
Start Free TrialLog in
Avatar of sqdperu
sqdperuFlag for United States of America

asked on

Need help with sp_addlinkedserver

I'm on SQL Server 2008 R2.  I am the admin for both servers (Prod and Dev).  In SSMS, I have both servers connected (as sa) and can do whatever I want with either of them.
I got a stored procedure off the internet to compare row counts from tables on Prod server to same database\tables on the Dev server.

When I try to "CREATE PROCEDURE" on the Prod server I get the error:
"Could not find server 'DEV' 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.

So I try to run the sp_addlinkedserver the site said I need to do before running the "CREATE PROCEDURE":

EXEC sp_addlinkedserver    
    @server='DEV',             -- Remote Computer Name
    @srvproduct='',                 -- Not Needed
    @catalog='MyDB',             -- Database
    @provider='SQLNCLI',            -- SQL Server Driver
    @datasrc='DEV'                       -- SQL Server name

It gives me the error: "The server 'DEV' already exists."

So what give?   One says it cannot find the server and the other is complaining because it does!

Any ideas?

Thanks

If it matters, here is the Stored Procedure I'm trying to create:

-- Use this script to compare the row counts of all the tables in a database on a server with the same on another server.
-- Script came from http://sqln.blogspot.com/2008/10/tsql-code-compare-tables-of-databases.html?_sm_au_=iQHNFskH8qqFMDpq



--Change the database name to the appropriate database
USE [MyDB];
GO

CREATE PROCEDURE sp_CompareTables_Different_RowCount
 AS
 
 CREATE TABLE #tblNew
( tblName varchar(50), CountRows int )

INSERT INTO #tblNew
( tblName, CountRows)
SELECT o.name AS "Table Name", i.rowcnt AS "Row Count"
FROM sysobjects o, sysindexes i
WHERE i.id = o.id
AND indid IN(0,1)
AND xtype = 'u'
AND o.name <> 'sysdiagrams'
ORDER BY o.name

CREATE TABLE #tblOLD
( tblName varchar(50), CountRows int )

INSERT INTO #tblOLD
( tblName, CountRows)
SELECT lo.name AS "Table Name", li.rowcnt AS "Row Count"

FROM [Prod].[MyDB].[dbo].[sysobjects] lo,  
    [DEV].[MyDB].[dbo].[sysindexes] li
WHERE li.id = lo.id
AND indid IN(0,1)
AND xtype = 'u'
AND lo.name <> 'sysdiagrams'
ORDER BY lo.name

CREATE TABLE #tblDiff
( OldTable varchar(50), OldRowCount int, NewRowCount int, NewTableName varchar(50))

INSERT INTO #tblDiff
( OldTable, OldRowCount, NewRowCount, NewTableName )
SELECT ol.tblName, ol.CountRows, nw.CountRows, nw.tblName
From    #tblNew nw
JOIN #tblOLD ol
ON (ol.tblName = nw.tblName AND ol.CountRows <> nw.CountRows)

-- Use line below instead of line above if you to see all tables and compare row count (even if rowcount is equal)
--ON (ol.tblName = nw.tblName AND ol.CountRows = nw.CountRows)
         
SELECT * FROM #tblDiff
         
DROP TABLE #tblNEW
DROP TABLE #tblOLD
DROP TABLE #tblDiff
ASKER CERTIFIED SOLUTION
Avatar of Kyle Abrahams, PMP
Kyle Abrahams, PMP
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sqdperu

ASKER

Awesome!

I didn't know @server was an alias and didn't know how to enter login info.   Got it working now.   Thanks for the quick response!
Avatar of sqdperu

ASKER

Exactly what I needed.  Thanks!