插入记录(如果不存在)SQL Server 2005 [英] Insert records if not exist SQL Server 2005

查看:194
本文介绍了插入记录(如果不存在)SQL Server 2005的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

SQL Server 2005数据库.我有一个包含许多记录的临时表,这些记录来自RSS提要,需要定期插入.某些数据将更改,而某些数据将保持不变.我只需要插入新"记录,并消除插入冗余数据导致重复记录的机会.我该怎么做?

SQL Server 2005 Database. I have a temporary table with many records, these records are coming from an RSS feed and need to be inserted periodically. Some of the data will change, and some will remain the same. I only need to insert the 'new' records, and eliminate the chance of inserting redundant data resulting in duplicate records. How do I accomplish this?

示例,tempTable,

Example, tempTable,

        BEGIN
            INSERT INTO myTable
                (
                    row1
                    ,row2
                    ,row3
                )
            SELECT
                row1
                ,row2
                ,row3
            FROM @tempTable
        END

推荐答案

一种方法是not exists子查询:

INSERT  myTable
        (row1, row2, row3)
SELECT  row1, row2, row3
FROM    @tempTable temp
WHERE   NOT EXISTS
        (
        SELECT  *
        FROM    myTable
        WHERE   row1 = temp.row1
                and row2 = temp.row2
                and row3 = temp.row3
        )

这篇关于插入记录(如果不存在)SQL Server 2005的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆