SQL 服务器:更新连接表中的多条记录
SQL Server : update multiple records from joined tables
我在测试环境中使用 SQL Server 2008 R2 Express,在生产环境中使用完整版。
我写了一个 select 语句来查找我要更新的所有记录。其中大约有 1200 个。
它连接多个表并且 selection 基于多个字段。
我想要做的是把它变成一个更新语句,其中所有匹配的记录都更新了相同的文件,即 dFinalised 设置为 '2015-01-14 00:00:00.000'
我 运行 我的更新查询
时收到以下错误
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
我想我将不得不以某种方式嵌套它,但我找不到足够简单的示例来遵循。
提前致谢
大卫
select 语句相当简单
SELECT lMeetingRegisterID
,sPlanNumber
,sName
,sDescription
,dMeeting
,sMeetingTime
,bManaged
FROM [Strata].[dbo].[MeetingRegister]
inner Join MeetingType on MeetingRegister.lMeetingTypeID = meetingtype.lMeetingTypeID
inner Join OwnersCorporation on MeetingRegister.lOwnersCorporationID = OwnersCorporation.lOwnersCorporationID
inner Join tbluser on OwnersCorporation.lUserID = tblUser.lUserID
WHERE dFinalised = '1900-01-01 00:00:00.000'
AND dMeeting < '2014-07-01 00:00:00.000'
AND bManaged != 'N'
这是我对更新查询的尝试。
Update dbo.MeetingRegister
set dFinalised = '2015-01-14 00:00:00.000'
from dbo.MeetingRegister
inner Join MeetingType on MeetingRegister.lMeetingTypeID = meetingtype.lMeetingTypeID
inner Join OwnersCorporation on MeetingRegister.lOwnersCorporationID = OwnersCorporation.lOwnersCorporationID
inner Join tbluser on OwnersCorporation.lUserID = tblUser.lUserID
Where dFinalised = '1900-01-01 00:00:00.000'
AND dMeeting < '2014-07-01 00:00:00.000'
AND bManaged = 'N'
尝试table表达式
WITH C AS (
SELECT lMeetingRegisterID
,sPlanNumber
,sName
,sDescription
,dMeeting
,sMeetingTime
,bManaged
,dFinalised
FROM [Strata].[dbo].[MeetingRegister]
inner Join MeetingType on MeetingRegister.lMeetingTypeID = meetingtype.lMeetingTypeID
inner Join OwnersCorporation on MeetingRegister.lOwnersCorporationID = OwnersCorporation.lOwnersCorporationID
inner Join tbluser on OwnersCorporation.lUserID = tblUser.lUserID
WHERE dFinalised = '1900-01-01 00:00:00.000'
AND dMeeting < '2014-07-01 00:00:00.000'
AND bManaged != 'N'
)
Update C
set dFinalised = '2015-01-14 00:00:00.000'
我在测试环境中使用 SQL Server 2008 R2 Express,在生产环境中使用完整版。 我写了一个 select 语句来查找我要更新的所有记录。其中大约有 1200 个。 它连接多个表并且 selection 基于多个字段。
我想要做的是把它变成一个更新语句,其中所有匹配的记录都更新了相同的文件,即 dFinalised 设置为 '2015-01-14 00:00:00.000'
我 运行 我的更新查询
时收到以下错误Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
我想我将不得不以某种方式嵌套它,但我找不到足够简单的示例来遵循。
提前致谢 大卫
select 语句相当简单
SELECT lMeetingRegisterID
,sPlanNumber
,sName
,sDescription
,dMeeting
,sMeetingTime
,bManaged
FROM [Strata].[dbo].[MeetingRegister]
inner Join MeetingType on MeetingRegister.lMeetingTypeID = meetingtype.lMeetingTypeID
inner Join OwnersCorporation on MeetingRegister.lOwnersCorporationID = OwnersCorporation.lOwnersCorporationID
inner Join tbluser on OwnersCorporation.lUserID = tblUser.lUserID
WHERE dFinalised = '1900-01-01 00:00:00.000'
AND dMeeting < '2014-07-01 00:00:00.000'
AND bManaged != 'N'
这是我对更新查询的尝试。
Update dbo.MeetingRegister
set dFinalised = '2015-01-14 00:00:00.000'
from dbo.MeetingRegister
inner Join MeetingType on MeetingRegister.lMeetingTypeID = meetingtype.lMeetingTypeID
inner Join OwnersCorporation on MeetingRegister.lOwnersCorporationID = OwnersCorporation.lOwnersCorporationID
inner Join tbluser on OwnersCorporation.lUserID = tblUser.lUserID
Where dFinalised = '1900-01-01 00:00:00.000'
AND dMeeting < '2014-07-01 00:00:00.000'
AND bManaged = 'N'
尝试table表达式
WITH C AS (
SELECT lMeetingRegisterID
,sPlanNumber
,sName
,sDescription
,dMeeting
,sMeetingTime
,bManaged
,dFinalised
FROM [Strata].[dbo].[MeetingRegister]
inner Join MeetingType on MeetingRegister.lMeetingTypeID = meetingtype.lMeetingTypeID
inner Join OwnersCorporation on MeetingRegister.lOwnersCorporationID = OwnersCorporation.lOwnersCorporationID
inner Join tbluser on OwnersCorporation.lUserID = tblUser.lUserID
WHERE dFinalised = '1900-01-01 00:00:00.000'
AND dMeeting < '2014-07-01 00:00:00.000'
AND bManaged != 'N'
)
Update C
set dFinalised = '2015-01-14 00:00:00.000'