Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/74.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/21.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_Tsql - Fatal编程技术网

Sql 插入到不存在的“从内部联接选择”中

Sql 插入到不存在的“从内部联接选择”中,sql,sql-server,tsql,Sql,Sql Server,Tsql,我有这个SQL Server代码。除了复制具有相同RightsId和UserId的行之外,所有操作都正常。where not exists子句不起作用。 感谢您的帮助 INSERT INTO dbo.UserAccessRights (Id, UserId, RightType, RightsId, CreatedOn, CreatedBy) SELECT DISTINCT NEWID(), @changedUserId, N'Proce

我有这个SQL Server代码。除了复制具有相同RightsId和UserId的行之外,所有操作都正常。where not exists子句不起作用。 感谢您的帮助

INSERT INTO dbo.UserAccessRights (Id, UserId, RightType, RightsId, CreatedOn, CreatedBy)
    SELECT DISTINCT 
        NEWID(),
        @changedUserId,
        N'Process ' + @rightsTypeSuffix,
        ptm.ProcessInstance_id,
        getdate(),
        @loggedInUserId
    FROM 
        dbo.ProcessTeamMembers ptm WITH (NOLOCK)
    INNER JOIN 
        dbo.Users u WITH (NOLOCK) ON ptm.TeamMemberProfile_id = u.ProfileID
                                  AND u.Id = @changedUserId
                                  AND ptm.TenantId = @tenantId
    INNER JOIN 
        dbo.ProcessInstances p_i WITH (NOLOCK) ON p_i.Id = ptm.ProcessInstance_id
                                               AND p_i.DeletedOn IS NULL
    WHERE 
        NOT EXISTS (SELECT * 
                    FROM UserAccessRights uar WITH (NOLOCK) 
                    WHERE uar.UserId = @changedUserId 
                      AND uar.RightsId = ptm.ProcessInstance_id)

请尝试左键,而不是WHERE NOT EXISTS子句

INSERT INTO dbo.UserAccessRights (Id, UserId, RightType, RightsId, CreatedOn, CreatedBy)
    SELECT DISTINCT 
        NEWID(),
        @changedUserId,
        N'Process ' + @rightsTypeSuffix,
        ptm.ProcessInstance_id,
        getdate(),
        @loggedInUserId
    FROM 
        dbo.ProcessTeamMembers ptm 
    INNER JOIN 
        dbo.Users u ON ptm.TeamMemberProfile_id = u.ProfileID
                                  AND u.Id = @changedUserId
                                  AND ptm.TenantId = @tenantId
    INNER JOIN 
        dbo.ProcessInstances p_i ON p_i.Id = ptm.ProcessInstance_id
                                               AND p_i.DeletedOn IS NULL
    LEFT JOIN UserAccessRights uar
        ON uar.UserId = u.Id
            AND uar.RightsId = ptm.ProcessInstance_id
    WHERE 
        uar.Id IS NULL -- Replace with the actual pkey on UserAccessRights if not Id

重复项可能来自查询内部,而不是表中的现有行。问题可能是select distinct没有做任何事情,因为newid总是唯一的

我的建议是将id列更改为默认值newid。然后您不需要插入它,select distinct将起作用。如果没有,您可以修复查询:

INSERT INTO dbo.UserAccessRights (Id, UserId, RightType, RightsId, CreatedOn, CreatedBy)
    SELECT DISTINCT NEWID(), @changedUserId, N'Process ' + @rightsTypeSuffix,
           ProcessInstance_id, getdate(), @loggedInUserId
    FROM (SELECT DISTINCT ptm.ProcessInstance_id
          FROM dbo.ProcessTeamMembers ptm WITH (NOLOCK) INNER JOIN 
               dbo.Users u WITH (NOLOCK)
               ON ptm.TeamMemberProfile_id = u.ProfileID AND
                  u.Id = @changedUserId AND
                ptm.TenantId = @tenantId INNER JOIN 
               dbo.ProcessInstances p_i WITH (NOLOCK)
               ON p_i.Id = ptm.ProcessInstance_id AND
                  p_i.DeletedOn IS NULL
          WHERE NOT EXISTS (SELECT 1
                            FROM UserAccessRights uar WITH (NOLOCK) 
                            WHERE uar.UserId = @changedUserId AND   
                                  uar.RightsId = ptm.ProcessInstance_id
                           )
    ) t;

嗯,当我想到这一点时,也许你应该使用TOP1而不是选择DISTINCT。我不确定是哪些列导致了distinct上的问题,但您在许多列中插入了相同的值,因此多行可能会导致问题。

Set-不建议在任何地方都使用此选项-恰恰相反!因此,重复项的问题是NOLOCK?我们可能需要查看产生问题的模式和示例数据,以便更好地诊断多行问题。