The code below is embedded in a .sql file in a vb.net project. It gives me an error:
Could not find server ‘DATABASE_NAME′ 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 checked in sys.server and the LinkedDatabaseName is returned if i do a select * from sys.servers
Here is the code where I get the error….
-----------------------------------
-- Obs Set Obs Set Obs Item Xref
-----------------------------------
-- STEP 1: txmr_TABLE1 to exp_TABLE1
DELETE FROM LinkedDatabaseName...exp_TABLE1
Select distinct newid() as GUID,
b.ObsSetCode as 'parObsSetCode',
c.ObsSetCode as 'chObsSetCode',
d.ObsItemCode as 'chObsItemCode'
Into #tmp_exp_TABLE1
From $$DATABASE_NAME$$..txmr_TABLE1 a
Inner Join $$DATABASE_NAME$$..txmr_obs_set b On a.parObsSetGUID = b.GUID
Left Join $$DATABASE_NAME$$..txmr_obs_set c On a.chObsSetGUID = c.GUID
Left Join $$DATABASE_NAME$$..txmr_obs_item d On a.chObsItemGUID = d.GUID
Order By b.ObsSetCode
Select * From #tmp_exp_TABLE1
EXEC $$DATABASE_NAME$$..dbo.txmr_ExtractPOCIS '#tmp_exp_TABLE1',
'$$DATABASE_NAME$$.dbo.txmr_TABLE1_chg',
@FromVersion,
@ToVersion;
INSERT INTO LinkedDatabaseName...exp_TABLE1 (GUID, parObsSetCode, chObsSetCode, chObsItemCode)
SELECT GUID, parObsSetCode, chObsSetCode, chObsItemCode
FROM #tmp_exp_TABLE1
ORDER BY parObsSetCode, chObsSetCode;
DROP TABLE #tmp_exp_TABLE1;
SELECT COUNT(*), 'exp_TABLE1' FROM LinkedDatabaseName...exp_TABLE1;
-- STEP 2: txmr_TABLE1_chg to exp_TABLE1_chg
DELETE FROM LinkedDatabaseName...exp_TABLE1_chg;
INSERT INTO LinkedDatabaseName...exp_TABLE1_chg (ChangeID, DateModified, parObsSetCode, chObsSetCode, chObsItemCode, RationaleLink, RationaleFreeText)
SELECT a.GUID as 'ChangeID',
a.DateModified as 'DateModified',
b.ObsSetCode as 'parObsSetCode',
c.ObsSetCode as 'chObsSetCode',
d.ObsItemCode as 'chObsItemCode',
r.RationaleID as 'RationaleLink',
a.RationaleFreeText as 'RationaleFreeText'
FROM $$DATABASE_NAME$$..txmr_TABLE1_chg a
INNER JOIN $$DATABASE_NAME$$..txmr_obs_set b ON a.parObsSetGUID = b.GUID
LEFT JOIN $$DATABASE_NAME$$..txmr_obs_set c ON a.chObsSetGUID = c.GUID
LEFT JOIN $$DATABASE_NAME$$..txmr_obs_item d ON a.chObsItemGUID = d.GUID
LEFT JOIN $$DATABASE_NAME$$..txmr_rationale r ON a.RationaleGUID = r.GUID
WHERE @StartDate <= a.DateModified AND @EndDate > a.DateModified
ORDER BY a.DateModified, b.ObsSetCode;
SELECT COUNT(*), 'exp_TABLE1_chg'
FROM LinkedDatabaseName...exp_TABLE1_chg;
Best Answer
The problem is that there are inconsistent usages of the $$DATABASE_NAME$$ substitution string. In most cases it is used to reference a specific database
but in the exec string:
there is one too many periods, which means that it is now referencing a different server and not a database.
Changing this statement to:
should resolve the problem.