我有这样的更新声明:
UPDATE CompaniesHouse SET
LastOccurrence =(select top 1 Id from Dataset ORDER BY RowNum DESC),
CompanyName =QCompaniesHouseUpdate.CompanyName,
FirmId =QCompaniesHouseUpdate.FirmId,
...
PreviousCompanyDate =QCompaniesHouseUpdate.PreviousCompanyDate,
PreviousCompanyName =QCompaniesHouseUpdate.PreviousCompanyName
FROM QCompaniesHouseUpdate Join CompaniesHouse
ON (QCompaniesHouseUpdate.FirmId=CompaniesHouse.FirmId)
GO
我尝试将嵌套查询保存到变量中并使用它而不是这样但它没有工作:
declare @dataset int
select top 1 @dataset = Id from Dataset ORDER BY RowNum DESC
当我尝试使用变量代替嵌套查询时,我收到了消息:
Msg 137, Level 15, State 2, Line 135
Must declare the scalar variable "@dataset".
它并不重要,但还有其他地方也很有用。我还有点担心,每次更新都要对每次插入进行评估,然后再对代码进行评估。 如果我需要更改它,将声明置于代码顶部也是有用的。
答案 0 :(得分:0)
- 试试这个,我只是改变了连接的顺序,并将Alias设置为表CompaniesHouse(Rest似乎对我好)
declare @dataset int
select top 1 @dataset = Id from Dataset ORDER BY RowNum DESC
UPDATE C SET
LastOccurrence =@dataset,
CompanyName =QCompaniesHouseUpdate.CompanyName,
FirmId =QCompaniesHouseUpdate.FirmId,
...
PreviousCompanyDate =QCompaniesHouseUpdate.PreviousCompanyDate,
PreviousCompanyName =QCompaniesHouseUpdate.PreviousCompanyName
FROM CompaniesHouse C JOIN QCompaniesHouseUpdate
ON (QCompaniesHouseUpdate.FirmId=CompaniesHouse.FirmId)
答案 1 :(得分:0)
更多信息:http://technet.microsoft.com/en-us/library/ms188037.aspx