Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/68.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 从临时表中删除记录时删除的所有记录_Sql_Sql Server - Fatal编程技术网

Sql 从临时表中删除记录时删除的所有记录

Sql 从临时表中删除记录时删除的所有记录,sql,sql-server,Sql,Sql Server,我有一个存储过程,我正在向它传递一个documentID字符串,它应该删除documentID,其中它的roleID=@roleID,但是它正在删除基于roleID的所有记录,我要做的就是根据roleID从表中删除documentID 我的sp是 ALTER PROCEDURE sp_RemoveDocumentIDsFromRole ( @roleID int, @group_name varchar(50), @DocumentIDString varchar(500), @group_di

我有一个存储过程,我正在向它传递一个documentID字符串,它应该删除documentID,其中它的roleID=@roleID,但是它正在删除基于roleID的所有记录,我要做的就是根据roleID从表中删除documentID

我的sp是

ALTER PROCEDURE sp_RemoveDocumentIDsFromRole
(
@roleID int,
@group_name varchar(50),
@DocumentIDString varchar(500),
@group_display_name varchar(50)
)
as
UPDATE [master_groups] 
set group_name = @group_name, @group_display_name = @group_display_name
where roleID = @roleID

-- Creating Variables
DECLARE @numberLength int
DECLARE @numberCount int
DECLARE @TheDocumentIDs VarChar(500)

DECLARE @sTemp VarChar(100) -- to hold single characters

    -- Creating a temp table
DECLARE @T TABLE 
(
    TheDocumentIDs VarChar(500)
)
--Initializing Variables for counting 
SET @numberLength = LEN (@DocumentIDString)
SET @numberCount = 1
SET @TheDocumentIDs = ''

--Start looping through the keyword ids
WHILE (@numberCount <= @numberLength)

BEGIN
SET @sTemp = SUBSTRING (@DocumentIDString, @numberCount, 1)
    IF (@sTemp = ',')
        BEGIN
            INSERT @T(TheDocumentIDs) VALUES (@TheDocumentIDs)
            SET @TheDocumentIDs = ''
        END
    IF (@sTemp <> ',')
        BEGIN
            SET @TheDocumentIDs = @TheDocumentIDs + @sTemp
        END
        SET @numberCount = @numberCount + 1
END 

declare @rLevel int
set @rLevel = 0

delete from [master_group_document_relations] where exists(select documentID = @TheDocumentIDs from @T) and roleID = @roleID
ALTER PROCEDURE sp_RemoveDocumentIDsFromRole
(
@罗莱德国际酒店,
@组名varchar(50),
@DocumentIDString varchar(500),
@组显示名称varchar(50)
)
作为
更新[主控组]
设置组名=@group\u name,@group\u display\u name=@group\u display\u name
其中roleID=@roleID
--创建变量
声明@numberLength int
声明@numberCount int
声明@TheDocumentIDs VarChar(500)
声明@sTemp VarChar(100)--保存单个字符
--创建临时表
声明@T表
(
文档ID VarChar(500)
)
--初始化用于计数的变量
设置@numberLength=LEN(@DocumentIDString)
设置@numberCount=1
设置@TheDocumentId=“”
--开始循环关键字ID

虽然(@numberCount不确定您在文档ID中得到了什么,但我认为它应该适合您

首先,正如@Chris所提到的,您应该检查put condition to where子句,因为documentId是一个id列表,它应该在条件中与一起使用,而不是相等,这就是为什么您需要或使用sp_executesql或将id填充到临时表中的原因

         EXECUTE sp_executesql 
          N'delete from [master_group_document_relations] 
            where documentID IN (@TheDocumentIDs) 
               and roleID = @roleID',
          N'@TheDocumentIDs varchar(500), @roleID int',
          @DocumentIDString,
          @roleID;
或者试试这个

            delete from [master_group_document_relations] 
            where documentID IN (SELECT TheDocumentIDs FROM @T) 
               and roleID = @roleID

如果要删除
master\u group\u document\u relations
documentID
所在的
master\u group\u document\u relations的记录,请尝试以下操作:

DELETE T1
FROM   [master_group_document_relations] AS T1
WHERE  EXISTS(SELECT 1
              FROM   @T AS T2
              WHERE  T1.documentID = T2.TheDocumentIDs)
       AND roleID = @roleID 

@EmmadCareem,我非常确定它是正确的,它从临时表中选择ID并应该删除它们。我使用的方法与将ID临时表插入到表中的方法相同一个问题和一个建议。Q:您能否提供关于输入文档字符串以及处理后关于表T内容的示例?建议:避免使用“sp_”作为存储过程的前缀。“sp_”也被Microsoft SQL Server内置存储过程用作前缀,这是不允许的(如果有兴趣,可以通过在线搜索书籍来确认)@Eduardata,是的,我对使用sp_前缀感到内疚,我已经知道这是一个“否”,但在我准备清理代码之前它是用于测试的..DocumentIDString看起来像..123234321等..为无效列名T1抛出了一个错误。documentIDsorry编辑了答案。我在两个表中使用了相同的别名。我尝试了它,结果返回该ID没有被删除。它应该已经工作,或者我没有正确地感知到它。请检查此sql FIDLE以获取上述查询:使用数据的图片更新我也尝试了此操作,但应已删除的ID仍然有效there@Chris第二个查询呢?很抱歉,第二个查询不起作用,我不知道如何运行第一个查询query@Chris就这么说吧在sp中而不是在delete语句中。@Chris在delete之前T和DOCUMENTID的值是多少?