如果不存在则插入记录 SQL Server 2005

发布于 2024-10-18 08:25:21 字数 450 浏览 2 评论 0原文

SQL Server 2005 数据库。我有一个包含许多记录的临时表,这些记录来自 RSS feed,需要定期插入。有些数据会发生变化,有些则保持不变。我只需要插入“新”记录,并消除插入冗余数据导致重复记录的机会。我该如何实现这个目标?

示例,临时表,

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

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?

Example, tempTable,

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

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

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

发布评论

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

评论(2

夏有森光若流苏 2024-10-25 08:25:21

一种方法是使用 notists 子查询:

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
        )

One way is a not exists subquery:

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
        )
白衬杉格子梦 2024-10-25 08:25:21

试试这个:

con.Open();
SqlCommand cmd = new SqlCommand("If Not Exists(select * from stholidays where holiday='"+ dd + "') begin insert into stholidays values('" + dateTimePicker12.Text + "','" + dateTimePicker20.Text + "','" + dateTimePicker13.Text + "','" + mbk + "','" + dateTimePicker14.Text + "','" + dateTimePicker15.Text + "','" + lt + "','" + dateTimePicker16.Text + "','" + dateTimePicker17.Text + "','" + ebk + "','" + dateTimePicker18.Text + "','" + dateTimePicker19.Text + "','" + textBox105.Text + "','" + textBox106.Text + "','" + textBox107.Text + "','" + dd + "','" + textBox104.Text + "')end", con);
cmd.ExecuteNonQuery();
con.Close();

Try this:

con.Open();
SqlCommand cmd = new SqlCommand("If Not Exists(select * from stholidays where holiday='"+ dd + "') begin insert into stholidays values('" + dateTimePicker12.Text + "','" + dateTimePicker20.Text + "','" + dateTimePicker13.Text + "','" + mbk + "','" + dateTimePicker14.Text + "','" + dateTimePicker15.Text + "','" + lt + "','" + dateTimePicker16.Text + "','" + dateTimePicker17.Text + "','" + ebk + "','" + dateTimePicker18.Text + "','" + dateTimePicker19.Text + "','" + textBox105.Text + "','" + textBox106.Text + "','" + textBox107.Text + "','" + dd + "','" + textBox104.Text + "')end", con);
cmd.ExecuteNonQuery();
con.Close();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文