插入从表_a到table_b的记录,而无需插入任何重复项

发布于 2025-02-14 01:15:47 字数 399 浏览 1 评论 0原文

我有两个表

CC_TEMP 
   [Record Type] char(2)
  ,[Segment Code] char(2)
  ,[Headquarters Code] char(5)
  
CC_PERM
   [recID] numeric IDENTITY(1,1) NOT NULL
  ,[Record Type] char(2)
  ,[Segment Code] char(2)
  ,[Headquarters Code] char(5)

CC_TEMP是创建新数据的地方。 CC_PERM是主要的永久数据表。所有新数据都必须在此处放置。没有唯一的标识符。

如何将数据从CC_TEMP插入CC_PERM中,以确保没有CC_Perm的现有记录与CC_Perm?我尝试使用不运气的CTE

I have two tables

CC_TEMP 
   [Record Type] char(2)
  ,[Segment Code] char(2)
  ,[Headquarters Code] char(5)
  
CC_PERM
   [recID] numeric IDENTITY(1,1) NOT NULL
  ,[Record Type] char(2)
  ,[Segment Code] char(2)
  ,[Headquarters Code] char(5)

CC_TEMP is where new data is created.
CC_PERM is the main permanent data tables. All new data has to be placed here. There are no unique identifiers.

How do I insert the data from CC_TEMP into CC_PERM making sure there are no existing records vs from CC_PERM? I tried using a CTE without luck

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤云独去闲 2025-02-21 01:15:47

您可以使用左联接来确保在插入记录之前尚不存在。

INSERT INTO CC_PERM
SELECT T.*
FROM CC_TEMP T
LEFT JOIN CC_PERM P ON
    T.[Record Type] = P.[Record Type]
    AND T.[Segment Code] = P.[Segment Code]
    AND T.[Headquarters Code] = P.[Headquarters Code]
WHERE P.[recID] IS NULL

如果您有时在某些字段中有空格或空,则可以使用以下查询将空值转换为空白。

INSERT INTO CC_PERM
SELECT T.*
FROM CC_TEMP T
LEFT JOIN CC_PERM P ON
    ISNULL(T.[Record Type], '') = ISNULL(P.[Record Type], '')
    AND ISNULL(T.[Segment Code], '') = ISNULL(P.[Segment Code], '')
    AND ISNULL(T.[Headquarters Code], '') = ISNULL(P.[Headquarters Code], '')
WHERE P.[recID] IS NULL

You can use a left join to make sure that the record doesn't already exist before inserting it.

INSERT INTO CC_PERM
SELECT T.*
FROM CC_TEMP T
LEFT JOIN CC_PERM P ON
    T.[Record Type] = P.[Record Type]
    AND T.[Segment Code] = P.[Segment Code]
    AND T.[Headquarters Code] = P.[Headquarters Code]
WHERE P.[recID] IS NULL

If you sometimes have spaces or NULLs in some of the fields, then the following query can be used to convert NULL values to blanks.

INSERT INTO CC_PERM
SELECT T.*
FROM CC_TEMP T
LEFT JOIN CC_PERM P ON
    ISNULL(T.[Record Type], '') = ISNULL(P.[Record Type], '')
    AND ISNULL(T.[Segment Code], '') = ISNULL(P.[Segment Code], '')
    AND ISNULL(T.[Headquarters Code], '') = ISNULL(P.[Headquarters Code], '')
WHERE P.[recID] IS NULL
-小熊_ 2025-02-21 01:15:47

使用的正常方法是不存在

insert into CC_PERM ([Record Type], [Segment Code], [Headquarters Code])
select [Record Type], [Segment Code], [Headquarters Code]
from CC_TEMP t
where not exists (
  select * from CC_PERM p where 
      p.[Record Type]       = t.[Record Type] 
  and p.[Segment Code]      = t.[Segment Code] 
  and p.[Headquarters Code] = t.[Headquarters Code]
);

另一种方法是使用 ,如果列包含null值,则很有用,因为它不依赖于平等,而是而是使用是null

insert into CC_PERM ([Record Type], [Segment Code], [Headquarters Code])
select [Record Type], [Segment Code], [Headquarters Code]
from CC_TEMP
except
select [Record Type], [Segment Code], [Headquarters Code]
from CC_PERM

The normal way to approach this is using not exists

insert into CC_PERM ([Record Type], [Segment Code], [Headquarters Code])
select [Record Type], [Segment Code], [Headquarters Code]
from CC_TEMP t
where not exists (
  select * from CC_PERM p where 
      p.[Record Type]       = t.[Record Type] 
  and p.[Segment Code]      = t.[Segment Code] 
  and p.[Headquarters Code] = t.[Headquarters Code]
);

Another way to approach this is using except which is useful if a column contains NULL values as it doesn't rely on equality but instead uses is null

insert into CC_PERM ([Record Type], [Segment Code], [Headquarters Code])
select [Record Type], [Segment Code], [Headquarters Code]
from CC_TEMP
except
select [Record Type], [Segment Code], [Headquarters Code]
from CC_PERM
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文