SQL Server 2005数据库。我有一个包含许多记录的临时表,这些记录来自RSS提要,需要定期插入。有些数据会改变,有些数据会保持不变。我只需要插入“新”记录,并消除插入冗余数据的可能性,从而导致重复记录。我该如何做到这一点?
示例,tempTable,
BEGIN
INSERT INTO myTable
(
row1
,row2
,row3
)
SELECT
row1
,row2
,row3
FROM @tempTable
END
答案 0 :(得分:5)
一种方法是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
)
答案 1 :(得分:1)
试试这个:
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();