从Master- Details表中删除记录

时间:2013-06-12 09:43:49

标签: sql sql-server

不久前我对上一个问题的答复非常友好,我现在想清理我刚刚将记录转移到我刚刚转移的记录的历史数据表中的表格。

我知道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

3 个答案:

答案 0 :(得分:3)

将您的第一个删除声明更改为:

DELETE FROM Landings.LandingDetails
WHERE LandingId IN 
             ( 
                  SELECT Landings.LandingId
                  FROM Landings.LandingHeaders
                  WHERE Landings.LandingHeaders.LandingDate1 <= @cutoffdate
             )

答案 1 :(得分:2)

试试这个 -

CREATE PROCEDURE [HistoricalData].[ClearAllLandingInformation]

@cutoffdate DATE

AS BEGIN

    SET NOCOUNT ON;

    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

更新(如果您有FK):

CREATE TABLE Landings.LandingHeaders
(
      LandingId  INT PRIMARY KEY NOT NULL
    , LandingDate1 DATETIME
)
CREATE TABLE Landings.LandingDetails(
    ID INT PRIMARY KEY NOT NULL,
    LandingId INT NOT NULL
)
GO

ALTER TABLE Landings.LandingDetails WITH CHECK 
ADD CONSTRAINT FK_LandingId FOREIGN KEY(LandingId)
REFERENCES Landings.LandingHeaders (LandingId)
ON DELETE CASCADE
GO

ALTER TABLE Landings.LandingDetails CHECK CONSTRAINT FK_LandingId
GO

CREATE PROCEDURE [HistoricalData].[ClearAllLandingInformation]

    @cutoffdate DATE

AS BEGIN

    SET NOCOUNT ON;

    DELETE FROM Landings.LandingHeaders
    WHERE LandingDate1 <= @cutoffdate

END

答案 2 :(得分:0)

这在SQL Server中不正确:

WHERE INNER JOIN