在不久前对我最后一个问题的非常友好的回答之后,我现在想清理我刚刚将记录转移到我刚刚转移的那些记录的历史数据表的表。
我知道 DELETE FROM 将删除显然我不想这样做的所有记录,因此我需要使用 where 语句对其进行条件处理。
我迄今为止的情况如下:
CREATE PROCEDURE [HistoricalData].[ClearAllLandingInformation]
-- Add the parameters for the stored procedure here
@cutoffdate date
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
DELETE FROM Landings.LandingDetails
WHERE INNER JOIN Landings.LandingHeaders
ON Landings.LandingHeaders.LandingId = Landings.LandingDetails.LandingId
WHERE Landings.LandingHeaders.LandingDate1 <= @cutoffdate
DELETE FROM Landings.LandingHeaders
WHERE LandingDate1 <= @cutoffdate
GO
当我尝试执行 sql 并因此创建过程时的错误消息告诉我单词 INNER 附近的语法错误(带或不带括号)。这仅仅是我的语法结构不佳的情况,还是您不能根据主表中的条件从详细信息表中删除记录?
最后,目前假设我的 sql 语法不好,创建一个首先将记录从当前表传输到历史表然后继续删除那些被传输的记录的过程有什么根本错误吗?从当前表。我的最终目标是生成一个存储过程,该存储过程可以导入实体模型并由最终用户作为单个函数执行。
谢谢
这是我尝试组合 spoc 来移动然后删除满足特定条件的记录。它成功地将它们从原始表(最后一部分)中删除,但没有将它们转移到原始部分中。
ALTER PROCEDURE [HistoricalData].[MoveAndClearLandingInformation] -- Add the parameters for the stored procedure here
@cutoffdate date
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
--First collect and move the required information
INSERT INTO HistoricalData.HistoricalLandingHeaders
SELECT
LandingId,
VesselId,
TicketNumber,
LandingDate1,
PortOfLanding,
CountryOfLanding,
Logbook1,
Loogbook2,
Logbook3,
LandingDecNumber1,
LandingDecNumber2,
LandingDecNumber3,
DateOfPurchase,
Posted,
AllocatedErsId,
LandingDate2,
LandingDate3,
VesselName,
VesselOwner,
VesselPLN,
ModifiedDate
FROM Landings.LandingHeaders
WHERE Landings.LandingHeaders.LandingDate1 <= @cutoffdate
INSERT INTO HistoricalData.HistoricalLandingDetails
SELECT
LandingDetails.LandingId,
ProductId,
PresentationCode,
PresentationState,
FreshnessGrade,
ProductSize,
FishingArea,
IcesZone,
Quantity,
UnitPrice,
LandingDetailVatRate,
SpeciesCode,
Currency,
ProductLandedUnderQuota,
LandingDetails.ModifiedDate
FROM Landings.LandingDetails
INNER JOIN Landings.LandingHeaders
ON Landings.LandingHeaders.LandingId = Landings.LandingDetails.LandingId
WHERE Landings.LandingHeaders.LandingDate1 <= @cutoffdate
-- Now remove that information from the current tables
DELETE FROM ld
FROM Landings.LandingDetails ld
JOIN Landings.LandingHeaders lh
ON ld.LandingId = lh.LandingId
WHERE lh.LandingDate1 <= @cutoffdate
DELETE FROM Landings.LandingHeaders
WHERE LandingDate1 <= @cutoffdate
END