在更改一列时防止多次执行将记录复制回源 table 的重复

Prevent duplicates across multiple executions of copying records back into source table while changing one column

我想从 tblOrder select CustomerIDOrderTypeIDLoanNumber 插入新的 OrderTypeID 但相同CustomerIDLoanNumber。 我的查询会这样做,但它会重复值,每次执行查询时都会重复行。

insert into tblOrder (CustomerID, OrderTypeID, LoanNumber)
Select o.CustomerID,3, o.LoanNumber
from tblOrder as o
where o.OrderTypeID = 1;

这是查询的作用:

OrderID 9 - 12 重复或 OrderID 5 - 8。查询执行了 2 次,因此记录重复。

这是您要找的吗?确保

Create table #temp(OrderID int, CustomerID int,  OrderTypeID int, LoanNumber int)
INsert into #temp values(1  , 1  , 1  , 45584565)
INsert into #temp values(2 ,  1 ,  1 ,  45566856)
INsert into #temp values(3  , 1  , 1 ,  45565584)
INsert into #temp values(4  , 1  , 1  , 45588545)

select * from #temp



UPDATE TE
SET  CustomerID=tab.CustomerID,OrderTypeID=3,LoanNumber=tab.LoanNumber
FROM #temp TE inner join
(Select o.OrderID,o.CustomerID,3 tt, o.LoanNumber
from #temp as o
where o.OrderTypeID = 1)tab

ON TE.OrderID = tab.OrderID

select * from #temp
drop table #temp

这是你想要的吗?

CREATE TABLE tblOrder(
    OrderID     INT IDENTITY(1, 1),
    CustomerID  INT,
    OrderTypeID INT,
    LoanNumber  INT
)
INSERT INTO tblOrder VALUES
(1, 1, 45584565),
(1, 1, 45566856),
(1, 1, 45565584),
(1, 1, 45588545)

INSERT INTO tblOrder( CustomerID, OrderTypeID, LoanNumber)
SELECT
    o.CustomerID,
    3,
    o.LoanNumber
FROM tblOrder o
WHERE NOT EXISTS(
    SELECT 1
    FROM tblOrder
    WHERE
        CustomerID = o.CustomerID
        AND OrderTypeID = 3
        AND LoanNumber = o.LoanNumber
)

这可能对你有帮助

我在这里检查存在的值(在变量中)是否存在于 table 中,如果存在则离开 else insert

 Declare @claimid int, @subscriber int, @qualifyinginformation int
set @claimid = '1000008'
set @subscriber = '1'
set @qualifyinginformation = '1'

If Exists (Select * from test1 where claimid = @claimid and subscriber=@subscriber and qualifyinginformation=@qualifyinginformation )
begin
     print ('The Value already Exist')
 end
else
 begin
   Insert into test1 (claimid,subscriber,qualifyinginformation) values (@claimid,@subscriber,@qualifyinginformation)
select * from test1
end

这里第一个也是最重要的问题是不是您的查询插入重复;是您的 table 允许 它首先发生。因此,您首先需要在这 3 个字段上创建一个 UNIQUE INDEX 以禁止重复。

第二个问题是如何处理操作试图插入重复的情况。您有两个主要选择:

  1. 您可以先检查记录是否存在,如果找到则跳过 INSERT,或者

  2. 您可以将 UNIQUE INDEX 设置为 "ignore" 重复项,在这种情况下,您不需要先检查,因为操作会悄无声息地失败,只是警告说重复项不是已插入。


如果您选择选项#1(先检查),那么:

CREATE UNIQUE NONCLUSTERED INDEX [UIX_tblOrder_CustomerID_OrderTypeID_LoanNumber]
    ON [tblOrder]
    ( [CustomerID] ASC, [OrderTypeID] ASC, [LoanNumber] ASC );

然后:

INSERT INTO tblOrder (CustomerID, OrderTypeID, LoanNumber)
  SELECT o.CustomerID, 3, o.LoanNumber
  FROM   tblOrder as o
  WHERE  o.OrderTypeID = 1
  AND    NOT EXISTS (SELECT *
                     FROM tblOrder tmp
                     WHERE tmp.CustomerID = o.CustomerID
                     AND   tmp.OrderTypeID = 3
                     AND   tmp.LoanNumber = o.LoanNumber);

如果您选择选项#2(不勾选),那么:

CREATE UNIQUE NONCLUSTERED INDEX [UIX_tblOrder_CustomerID_OrderTypeID_LoanNumber]
    ON [tblOrder]
    ( [CustomerID] ASC, [OrderTypeID] ASC, [LoanNumber] ASC )
    WITH (IGNORE_DUP_KEY = ON);

然后:

INSERT INTO tblOrder (CustomerID, OrderTypeID, LoanNumber)
  SELECT o.CustomerID, 3, o.LoanNumber
  FROM   tblOrder as o
  WHERE  o.OrderTypeID = 1;

IGNORE_DUP_KEY 行为示例:

CREATE TABLE #IgnoreDuplicateTest (Col1 INT);
CREATE UNIQUE NONCLUSTERED INDEX [UIX_#IgnoreDuplicateTest_Col1]
    ON #IgnoreDuplicateTest
    ( [Col1] ASC )
    WITH (IGNORE_DUP_KEY = ON);

INSERT INTO #IgnoreDuplicateTest (Col1) VALUES (1);
-- (1 row(s) affected)

INSERT INTO #IgnoreDuplicateTest (Col1) VALUES (1);
-- Duplicate key was ignored.
-- (0 row(s) affected)

INSERT INTO #IgnoreDuplicateTest (Col1)
 SELECT tmp.val
 FROM (VALUES (1),(2)) AS tmp(val);
-- Duplicate key was ignored.
-- (1 row(s) affected)

SELECT * FROM #IgnoreDuplicateTest;

-- Col1
--    1
--    2
ALTER PROCEDURE [dbo].[InsertName] (
    @empname varchar(25),
    @email varchar(25)
)
AS
IF EXISTS (
        SELECT
            'True'
        FROM
            Employee
        WHERE      
            EmployeeName = @empname
    )
    BEGIN
        SELECT 'This record already exists!'
    END
ELSE
    BEGIN
        INSERT into Employee(EmployeeName, EmailId) VALUES(@empname,@email)    
    END

EXEC [InsertName]   
   'Darshil','darshil@gmail'

After you set the primary key on some column name, it will check for duplicate entries on that column name and if found simply ignore the duplicate data.

INSERT IGNORE INTO db_name.table_name(col1, col2, col3) VALUES (%s,%s,%s)