Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 如果可以先删除其他记录,如何删除记录?_Sql Server_Transactions - Fatal编程技术网

Sql server 如果可以先删除其他记录,如何删除记录?

Sql server 如果可以先删除其他记录,如何删除记录?,sql-server,transactions,Sql Server,Transactions,我有以下资料: 一个文件可以包含另一个文件一个视频文件包含音频和字幕,通常内容文件不是单独保存的,只是作为主文件的一部分出现。但是在某些特殊情况下,我会将一些内容文件分开保存 我也有存储区,所以如果主文件是一个文件,那么存储区中就有一个文件,如果我有一个辅助文件也保存在主文件之外的话 我有两张桌子: 文件文件,。。。; filecontainsfilesidcontainerfile,IDContentFile; 商店…商店。。。 StoresHaveFilesIDStore,IDFile 如果

我有以下资料:

一个文件可以包含另一个文件一个视频文件包含音频和字幕,通常内容文件不是单独保存的,只是作为主文件的一部分出现。但是在某些特殊情况下,我会将一些内容文件分开保存

我也有存储区,所以如果主文件是一个文件,那么存储区中就有一个文件,如果我有一个辅助文件也保存在主文件之外的话

我有两张桌子:

文件文件,。。。; filecontainsfilesidcontainerfile,IDContentFile; 商店…商店。。。 StoresHaveFilesIDStore,IDFile 如果我想删除一个主文件,我只想从一个特定的sotre中删除,因此如果该文件在其他存储中,我不想删除该文件。如果该文件不在其他存储中,我想删除该文件,因为在数据库中包含此信息没有意义

此外,我希望删除所有内容文件,前提是内容文件不单独存储在主文件之外的另一个存储中

为了确保我不能删除另一个存储中的文件,我不会在级联中删除存储文件上的关系,因此这样我会得到一个键错误

所以我想按照以下步骤来做:

尝试删除主文件。如果我能做到,请尝试删除内容文件。 我想我需要一个交易来做这件事,但我不知道怎么做

我不知道是否可以声明一个包含我要删除的文件的列表,如何迭代此列表以及如何尝试删除内容文件,如果某些内容文件由于存储在其他位置而无法删除,请尝试删除下一个内容文件

也许还有其他办法来解决这个问题


谢谢。

有几种不同的方法可以做到这一点。关于事务,了解语法和用法的最简单方法是在MSDN上阅读关于

对于删除,我的方法是:

    -- Assumed inputs
    DECLARE @StoreId int;
    DECLARE @FileIdToDelete int;

    -- Query
    SET XACT_ABORT ON;  -- Forces whole transaction to roll back if 
                        --  there was an error at any point

    BEGIN TRAN;

    DECLARE @NumberOfStoresContainingFile int;

    -- Find out how many stores have this file
    SELECT  @NumberOfStoresContainingFile = COUNT(*)
    FROM    StoresHaveFiles shf
    WHERE   IDFile = @FileIdToDelete

    -- If it is more than one we know we dont want to delete so signal caller
    IF (@NumberOfStoresContainingFile > 1)
        RAISERROR(<addYourDetailsHere>);

    -- Remove any contained files of the main file we want to remove
    DELETE  f
    FROM    Files f
    WHERE   EXISTS (SELECT * 
                    FROM FilesContainsFiles fcf 
                    WHERE fcf.IDContainerFile = @FileIdToDelete AND fcf.IDContentFile = f.IDFile);

    -- Remove the main file entry itself
    DELETE
    FROM    Files
    WHERE   IDFile = @FileIdToDelete;

    -- Remove the JOIN table record between store and file
    DELETE  StoresHaveFiles
    WHERE   IDStore = @StoreId
        AND IDFile = @FileIdToDelete;

    -- This is an optimistic approach (it assumes the file belongs to the store you asked about
    --  if the file is only in one store).  This check is to ensure that the JOIN record was
    --  deleted.  If it is not, this means the file did not belong to the store and an error
    --  should be thrown.  To do this pessimistically, simply do this check before the deletes
    --  and after the count check.
    IF (@@rowcount = 0)
        RAISERROR(<addYourDetailsHere>);

    COMMIT;

有几种不同的方法可以做到这一点。关于事务,了解语法和用法的最简单方法是在MSDN上阅读关于

对于删除,我的方法是:

    -- Assumed inputs
    DECLARE @StoreId int;
    DECLARE @FileIdToDelete int;

    -- Query
    SET XACT_ABORT ON;  -- Forces whole transaction to roll back if 
                        --  there was an error at any point

    BEGIN TRAN;

    DECLARE @NumberOfStoresContainingFile int;

    -- Find out how many stores have this file
    SELECT  @NumberOfStoresContainingFile = COUNT(*)
    FROM    StoresHaveFiles shf
    WHERE   IDFile = @FileIdToDelete

    -- If it is more than one we know we dont want to delete so signal caller
    IF (@NumberOfStoresContainingFile > 1)
        RAISERROR(<addYourDetailsHere>);

    -- Remove any contained files of the main file we want to remove
    DELETE  f
    FROM    Files f
    WHERE   EXISTS (SELECT * 
                    FROM FilesContainsFiles fcf 
                    WHERE fcf.IDContainerFile = @FileIdToDelete AND fcf.IDContentFile = f.IDFile);

    -- Remove the main file entry itself
    DELETE
    FROM    Files
    WHERE   IDFile = @FileIdToDelete;

    -- Remove the JOIN table record between store and file
    DELETE  StoresHaveFiles
    WHERE   IDStore = @StoreId
        AND IDFile = @FileIdToDelete;

    -- This is an optimistic approach (it assumes the file belongs to the store you asked about
    --  if the file is only in one store).  This check is to ensure that the JOIN record was
    --  deleted.  If it is not, this means the file did not belong to the store and an error
    --  should be thrown.  To do this pessimistically, simply do this check before the deletes
    --  and after the count check.
    IF (@@rowcount = 0)
        RAISERROR(<addYourDetailsHere>);

    COMMIT;

有几种不同的方法可以做到这一点。关于事务,了解语法和用法的最简单方法是在MSDN上阅读关于

对于删除,我的方法是:

    -- Assumed inputs
    DECLARE @StoreId int;
    DECLARE @FileIdToDelete int;

    -- Query
    SET XACT_ABORT ON;  -- Forces whole transaction to roll back if 
                        --  there was an error at any point

    BEGIN TRAN;

    DECLARE @NumberOfStoresContainingFile int;

    -- Find out how many stores have this file
    SELECT  @NumberOfStoresContainingFile = COUNT(*)
    FROM    StoresHaveFiles shf
    WHERE   IDFile = @FileIdToDelete

    -- If it is more than one we know we dont want to delete so signal caller
    IF (@NumberOfStoresContainingFile > 1)
        RAISERROR(<addYourDetailsHere>);

    -- Remove any contained files of the main file we want to remove
    DELETE  f
    FROM    Files f
    WHERE   EXISTS (SELECT * 
                    FROM FilesContainsFiles fcf 
                    WHERE fcf.IDContainerFile = @FileIdToDelete AND fcf.IDContentFile = f.IDFile);

    -- Remove the main file entry itself
    DELETE
    FROM    Files
    WHERE   IDFile = @FileIdToDelete;

    -- Remove the JOIN table record between store and file
    DELETE  StoresHaveFiles
    WHERE   IDStore = @StoreId
        AND IDFile = @FileIdToDelete;

    -- This is an optimistic approach (it assumes the file belongs to the store you asked about
    --  if the file is only in one store).  This check is to ensure that the JOIN record was
    --  deleted.  If it is not, this means the file did not belong to the store and an error
    --  should be thrown.  To do this pessimistically, simply do this check before the deletes
    --  and after the count check.
    IF (@@rowcount = 0)
        RAISERROR(<addYourDetailsHere>);

    COMMIT;

有几种不同的方法可以做到这一点。关于事务,了解语法和用法的最简单方法是在MSDN上阅读关于

对于删除,我的方法是:

    -- Assumed inputs
    DECLARE @StoreId int;
    DECLARE @FileIdToDelete int;

    -- Query
    SET XACT_ABORT ON;  -- Forces whole transaction to roll back if 
                        --  there was an error at any point

    BEGIN TRAN;

    DECLARE @NumberOfStoresContainingFile int;

    -- Find out how many stores have this file
    SELECT  @NumberOfStoresContainingFile = COUNT(*)
    FROM    StoresHaveFiles shf
    WHERE   IDFile = @FileIdToDelete

    -- If it is more than one we know we dont want to delete so signal caller
    IF (@NumberOfStoresContainingFile > 1)
        RAISERROR(<addYourDetailsHere>);

    -- Remove any contained files of the main file we want to remove
    DELETE  f
    FROM    Files f
    WHERE   EXISTS (SELECT * 
                    FROM FilesContainsFiles fcf 
                    WHERE fcf.IDContainerFile = @FileIdToDelete AND fcf.IDContentFile = f.IDFile);

    -- Remove the main file entry itself
    DELETE
    FROM    Files
    WHERE   IDFile = @FileIdToDelete;

    -- Remove the JOIN table record between store and file
    DELETE  StoresHaveFiles
    WHERE   IDStore = @StoreId
        AND IDFile = @FileIdToDelete;

    -- This is an optimistic approach (it assumes the file belongs to the store you asked about
    --  if the file is only in one store).  This check is to ensure that the JOIN record was
    --  deleted.  If it is not, this means the file did not belong to the store and an error
    --  should be thrown.  To do this pessimistically, simply do this check before the deletes
    --  and after the count check.
    IF (@@rowcount = 0)
        RAISERROR(<addYourDetailsHere>);

    COMMIT;

我会通过一系列的检查来处理这个问题

如果文件存在于指定的存储中。。否则什么也不做。 如果文件存在于其他存储中。。。否则只需从一个存储中删除

例:


我会通过一系列的检查来处理这个问题

如果文件存在于指定的存储中。。否则什么也不做。 如果文件存在于其他存储中。。。否则只需从一个存储中删除

例:


我会通过一系列的检查来处理这个问题

如果文件存在于指定的存储中。。否则什么也不做。 如果文件存在于其他存储中。。。否则只需从一个存储中删除

例:


我会通过一系列的检查来处理这个问题

如果文件存在于指定的存储中。。否则什么也不做。 如果文件存在于其他存储中。。。否则只需从一个存储中删除

例: