插入SQL并删除记录

时间:2014-11-05 16:45:13

标签: sql sql-server sql-delete

我正在将标记列表处理到tblOffsite数据库表中,然后需要从核心表(tblOnStock)中删除。我的主要关键是tagID。如何使用已处理的tagID从核心表中删除?

这是我的代码:

sc.ConnectionString = ("Data Source=localhost\\SQLEXPRESS;Initial Catalog=Steripack;User ID=remote;Password=0925Greg#");
sc.Open();

com.Connection = sc;
com.CommandText = ("INSERT INTO tblOffsite (tagID, Hospital, Driver, datetime) SELECT tagID, Hospital, Driver, datetime FROM tblDelivery");

com.ExecuteNonQuery();
sc.Close();

sc.Open();
com.Connection = sc;
com.CommandText = ("DELETE FROM tblOnStock WHERE tagID = @tagID");
com.ExecuteNonQuery();
sc.Close();

1 个答案:

答案 0 :(得分:1)

你的意思是你应该从tblOnStock删除你在tblOffsite中插入的内容吗?

在这种情况下,要删除的SQL应为:

DELETE FROM tblOnStock WHERE tagID IN (SELECT tagID FROM tblDelivery)