假設我在SQL 「abc」中有一個表,並且其中有一列「number」,此列包含(1,2,3,4)等。比較兩個表並更新第三個
在我的第二張表「xyz」我有一個「數字」列,此列包含(1,2,3,4,5,6,7,8,9)。
現在我想比較這兩個並在第三個表格中插入相同的數據。 那麼我該如何做到這一點?
代碼:
string str = "SELECT Invoice_Details.PGI_ID, PARTY_BOOKING_DETAILS.PGI_ID AS abc ";
str += "FROM PARTY_BOOKING_MAIN INNER JOIN ";
str += " PARTY_BOOKING_DETAILS ON PARTY_BOOKING_MAIN.PBM_ID = PARTY_BOOKING_DETAILS.PBM_ID CROSS JOIN ";
str += " Invoice_Details where PARTY_BOOKING_MAIN.PM_ID = 1 ";
SqlConnection con = new SqlConnection("data source = .; database = ePartyDatabase01; integrated security = true");
con.Open();
SqlCommand cmd1 = new SqlCommand("update Invoice_Details set [status] = @a", con);
SqlCommand cmd = new SqlCommand(str, con);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
if (dr["PGI_ID"].ToString() == dr["abc"].ToString())
{
cmd1.Parameters.AddWithValue("@a", 1);
}
}
dr.Close();
cmd1.ExecuteNonQuery();
con.Close();
檢查線程http://stackoverflow.com/questions/4602083/sql-compare-data-from-two-tables – Madhabendra 2013-02-27 09:53:10
另外檢查:http://stackoverflow.com/questions/5990586/mulitply-2-值從2不同的表 – kyndigs 2013-02-27 09:54:25
要清楚,在第三個表中你會插入1,2,3,4? – 2013-02-28 01:24:17