我试图将sql server update语句转换为delete语句,但是在创建delete语句时,我收到有关别名的错误。我的原始更新加入了两个查询,比较了两列'值和更新哪里为真。我的删除类似,但会删除任何上述列比较为false的行。这是我的更新声明:
UPDATE drdTable
SET DeratingPartNumberID = new.DeratingPartNumberID
FROM [ReliabilityData].[dbo].[DeratingRefDes] drdTable, ((SELECT drd.[DeratingPartNumberID], [DeratingPartNumber].[Parameter], [DeratingPartNumber].[Units], drd.[DeratingRefDesID]
FROM [ReliabilityData].[dbo].[DeratingRefDes] drd
INNER JOIN [ReliabilityData].[dbo].[ReferenceDesignator]
ON [ReferenceDesignator].[ReferenceDesignatorID] = drd.[ReferenceDesignatorID]
INNER JOIN [ReliabilityData].[dbo].[DeratingPartNumber]
ON [DeratingPartNumber].[DeratingPartNumberID] = drd.[DeratingPartNumberID]
INNER JOIN [ReliabilityData].[dbo].[PartNumber]
ON [PartNumber].[PartNumberID] = [ReferenceDesignator].[PartNumberID]
INNER JOIN [ReliabilityData].[dbo].[BoardRevision]
ON [BoardRevision].[BoardRevisionID] = [ReferenceDesignator].[BoardRevisionID]
WHERE [PartNumber] = '2000465-203' AND [ReferenceDesignator].[BoardRevisionID] = 335 AND [ReferenceDesignator] IN ('C1','C2','C3','C4')) AS old
JOIN (SELECT [DeratingPartNumberID], [Parameter], [Units]
FROM [ReliabilityData].[dbo].[DeratingPartNumber]
WHERE [PartNumberID] = 82) AS new ON old.[Parameter] = new.[Parameter] AND old.[Units] = new.[Units])
WHERE drdTable.DeratingRefDesId = old.DeratingRefDesIDenter code here
以下是我的删除声明:
DELETE
FROM drdTable
FROM [ReliabilityData].[dbo].[DeratingRefDes] AS drdTable
WHERE DeratingPartNumberID IN (
(
SELECT drd.[DeratingPartNumberID],
[DeratingPartNumber].[Parameter],
[DeratingPartNumber].[Units],
drd.[DeratingRefDesID]
FROM [ReliabilityData].[dbo].[DeratingRefDes] drd
INNER JOIN [ReliabilityData].[dbo].[ReferenceDesignator]
ON [ReferenceDesignator].[ReferenceDesignatorID] = drd.[ReferenceDesignatorID]
INNER JOIN [ReliabilityData].[dbo].[DeratingPartNumber]
ON [DeratingPartNumber].[DeratingPartNumberID] = drd.[DeratingPartNumberID]
INNER JOIN [ReliabilityData].[dbo].[PartNumber]
ON [PartNumber].[PartNumberID] = [ReferenceDesignator].[PartNumberID]
INNER JOIN [ReliabilityData].[dbo].[BoardRevision]
ON [BoardRevision].[BoardRevisionID] = [ReferenceDesignator].[BoardRevisionID]
WHERE [PartNumber] = '2000465-203'
AND [ReferenceDesignator].[BoardRevisionID] = 335
AND [ReferenceDesignator] IN ('C1','C2','C3','C4')
) AS old
JOIN (
SELECT [DeratingPartNumberID],
[Parameter],
[Units]
FROM [ReliabilityData].[dbo].[DeratingPartNumber]
WHERE [PartNumberID] = 82
) AS new
ON old.[Parameter] <> new.[Parameter]
or old.[Units] <> new.[Units]
)
WHERE drdTable.DeratingRefDesId = old.DeratingRefDesID
我收到错误消息,抱怨我在select语句中使用的别名。
Msg 156, Level 15, State 1, Line 13
Incorrect syntax near the keyword 'AS'.
Msg 156, Level 15, State 1, Line 16
Incorrect syntax near the keyword 'AS'.
请注意,它所引用的别名是选择查询的别名(即旧的&#39;以及新的&#39;引用。有关如何重写查询的任何建议?
答案 0 :(得分:2)
你有一个额外的from
。
从别名表中删除时,语法为delete alias from tbl as alias ...
不要使用旧式连接。 Bad habits to kick : using old-style JOINs
据我所知,这是一个更简洁的查询版本:
代表update
:
use ReliabilityData;
go
/* update old with new Id where new Parameter and Units are the same */
update drd
set DeratingPartNumberID = dpn_new.DeratingPartNumberID
from dbo.DeratingRefDes as drd
inner join dbo.DeratingPartNumber as dpn_old /* old */
on dpn_old.DeratingPartNumberID = drd.DeratingPartNumberID
inner join dbo.ReferenceDesignator as rd
on rd.ReferenceDesignatorID = drd.ReferenceDesignatorID
inner join dbo.PartNumber as pn
on pn.PartNumberID = rd.PartNumberID
-- BoardRevision is not referenced in the where clause, so not needed */
/* inner join dbo.BoardRevision as br
on br.BoardRevisionID = rd.BoardRevisionID */
/* new */
inner join dbo.DeratingPartNumber as dpn_new
on dpn_old.Parameter = dpn_new.Parameter
and dpn_old.Units = dpn_new.Units
where pn.PartNumber = '2000465-203'
and rd.BoardRevisionID = 335
and rd.ReferenceDesignator in ('C1', 'C2', 'C3', 'C4')
and dpn_new.PartNumberID = 82
和delete
:
use ReliabilityData;
go
/* delete old where new Parameter and Units are different */
delete drd
from dbo.DeratingRefDes as drd
inner join dbo.DeratingPartNumber as dpn_old /* old */
on dpn_old.DeratingPartNumberID = drd.DeratingPartNumberID
inner join dbo.ReferenceDesignator as rd
on rd.ReferenceDesignatorID = drd.ReferenceDesignatorID
inner join dbo.PartNumber as pn
on pn.PartNumberID = rd.PartNumberID
-- BoardRevision is not referenced in the where clause, so not needed */
/* inner join dbo.BoardRevision as br
on br.BoardRevisionID = rd.BoardRevisionID */
/* new */
inner join dbo.DeratingPartNumber as dpn_new
on dpn_old.Parameter != dpn_new.Parameter
and dpn_old.Units != dpn_new.Units
where pn.PartNumber = '2000465-203'
and rd.BoardRevisionID = 335
and rd.ReferenceDesignator in ('C1', 'C2', 'C3', 'C4')
and dpn_new.PartNumberID = 82