我遇到問題,使用子查詢返回更新語句所需的值。我正在這裏使用CRM數據庫。我已經使用工作來導入一些字段,但是現在我需要使用更新語句,因爲導入器中並非所有字段都可用。子查詢返回的值超過1,無論標識爲Update語句
當我導入操作時,我使用了一個Actionname字段作爲ID字段的組合,以便在需要更新數據的事件中爲每個操作創建標識。
我需要使用特定於每個操作(2016會議或2017年會議)的RequestorComments字段來更新「主題」字段,但是我的子查詢沒有識別出string, SecurityID (FK), ContactID (FK) and an identity
作爲唯一值的組合。
我也嘗試使用select TOP 1作爲子查詢,但是這會爲每行返回「2017 Conference」。
DECLARE @Actions TABLE (ContactID nvarchar(256), PersonID int, CompletorComments nvarchar(256), RequestorComments nvarchar(256), SecurityListingID int, SubstatusCode int, CompletedDate datetime, Number int not null identity (1,1))
INSERT INTO @Actions VALUES ('34733','211','Corporation1','2017 Conference','2648','10014','2017-01-23 00:00:00')
INSERT INTO @Actions VALUES ('34733','211','Corporation2','2016 Conference','9103','10014','2016-01-23 00:00:00')
UPDATE dbo.Action
SET Subject = (select RequestorComments
from @Actions a
join dbo.action act (nolock)
on act.name = 'TRST-1289' + cast(SecurityListingID as nvarchar) + ContactID + cast(Number as nvarchar)
where act.CreatedDate between '2017-07-27 15:00:09.540' and '2017-07-27 15:05:09.540')
,UpdatedBy = 9999
,UpdatedDate = getdate()
--select *
from @Actions a
join dbo.action act (nolock)
on act.name = 'TRST-1289' + cast(SecurityListingID as nvarchar) + ContactID +
cast(Number as nvarchar)
where act.CreatedDate between '2017-07-27 15:00:09.540' and '2017-07-27
15:05:09.540'
您能否提供操作字段的示例數據?而且,Actions和@Actions之間的連接在性能方面非常糟糕。您可能希望已將其作爲臨時表中的字段連接以便於匹配 – Eli
如果'act.name'在傳遞的'act.CreatedDate'之間的'action'表中存在重複項,則會出現此錯誤 –
@eli謝謝爲備註re:串聯 - 我會確保更新。插入語句下方的2個插入語句是2行樣本數據。公司1參加了2017年的評論,並需要填入「主題」字段。 Corporation2參加了2016年,並需要進入主題字段 –