插入从表_a到table_b的记录,而无需插入任何重复项
我有两个表
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用左联接来确保在插入记录之前尚不存在。
如果您有时在某些字段中有空格或空,则可以使用以下查询将空值转换为空白。
You can use a left join to make sure that the record doesn't already exist before inserting it.
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.
使用的正常方法是不存在
另一种方法是使用 ,如果列包含null值,则很有用,因为它不依赖于平等,而是而是使用是null
The normal way to approach this is using not exists
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