一个 Table 到另一个 Table 插入仅检查特定字段重复

One Table to Another Table Insert only check particular field Duplication

我正在向 table 中插入值,其中已经有一行名为 TestDueAlert 的值,另一个 table 具有相同的列名称和 table其中的一行值命名为 temp,

INSERT INTO TestDueAlert(Name,EmailId,TankNo,TankType,CertificateType,TestedBy,TestDate,ExpiryDate,NoOfSnooze)
    SELECT[Name],[EmailId],[TankNo],[TankType],[CertificateType],[TestedBy],[TestDate],[ExpiryDate],[NoOfSnooze] 
    FROM @temp  
    EXCEPT
    SELECT [Name],[EmailId],[TankNo],[TankType],[CertificateType],[TestedBy],[TestDate],[ExpiryDate],[NoOfSnooze] 
    FROM TestDueAlert

现在如何通过检查特定字段重复并插入来检查和插入从 temp table 到 TestDueAlert 的值?

此处 TankNo 字段不应允许 TestDueAlert 中的重复记录。

这是我创建的 Table 脚本,

CREATE TABLE  TestDueAlert(TAID  INT NOT NULL,Name NVARCHAR(500),EmailId VARCHAR(250),TankNo VARCHAR(250),TankType VARCHAR(500),CertificateType VARCHAR(500),
    TestedBy VARCHAR(500),TestDate DateTime,ExpiryDate DateTime,NoOfSnooze INT);

使您的意图非常明确的一种方法是使用 not exists:

-- insert rows into testDueAlert only if the TankNo value does not already exist
insert TestDueAlert
(
   Name,
   EmailId,
   TankNo,
   TankType,
   CertificateType,
   TestedBy,
   TestDate,
   ExpiryDate,
   NoOfSnooze
)
select   t.[Name],
         t.[EmailId],
         t.[TankNo],
         t.[TankType],
         t.[CertificateType],
         t.[TestedBy],
         t.[TestDate],
         t.[ExpiryDate],
         t.[NoOfSnooze] 
from     @temp t
where    not exists
         (
            select * from TestDueAlert where TankNo = t.TankNo
         )