vb.net 在 sys.servers 错误中找不到服务器“DATABASE_NAME”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20807492/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Could not find server ‘DATABASE_NAME′ in sys.servers error
提问by User1
The code below is embedded in a .sql file in a vb.net project. It gives me an error:
下面的代码嵌入在 vb.net 项目中的 .sql 文件中。它给了我一个错误:
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.
在 sys.servers 中找不到服务器“DATABASE_NAME”。验证是否指定了正确的服务器名称。如有必要,请执行存储过程 sp_addlinkedserver 将服务器添加到 sys.servers。
I checked in sys.server and the LinkedDatabaseName is returned if i do a select * from sys.servers
我检查了 sys.server 并且如果我从sys.servers 中选择 *则返回 LinkedDatabaseName
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;
回答by competent_tech
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
问题是 $$DATABASE_NAME$$ 替换字符串的用法不一致。在大多数情况下,它用于引用特定的数据库
$$DATABASE_NAME$$..txmr_TABLE1
but in the exec string:
但在 exec 字符串中:
EXEC $$DATABASE_NAME$$..dbo.txmr_ExtractPOCIS
there is one too many periods, which means that it is now referencing a different server and not a database.
有一个太多的句点,这意味着它现在正在引用不同的服务器而不是数据库。
Changing this statement to:
将此语句更改为:
EXEC $$DATABASE_NAME$$.dbo.txmr_ExtractPOCIS
should resolve the problem.
应该可以解决问题。

