我需要向表中添加多个值,这些值取自其他表或使用其他表计算。我正在使用如下所示的代码。请告诉我们如何计算从其他表中获取的值。我需要从表transac(tid,sciptname,accnum,transactype,Quantity,date)插入表sales(scriptname,accnum,sharesbought,sharessold,remshares)。基于transac类型,我需要找到它是sharebought还是shareold,而剩余股份是sharebought-sharesold;
我使用.Net来做这件事请告诉我我使用它的代码是否正确。
SqlConnection Con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
Con.Open();
string insertsale = "INSERT INTO sales s (accnum, scriptname, shares_bought,sharessold,rem_shares) select case when t.transactype == 'sell' then s.accnum = t.accnum, s.scriptname = t.scriptname. s.shares_bought = 0,s.shares_sold=t.quanity, (s.shares_bought-s.shares_sold) as s.rem_shares
else
s.accnum = t.accnum, s.scriptname = t.scriptname. s.shares_bought = t.quanity,s.shares_sold=0, (s.shares_bought-s.shares_sold) as s.rem_shares from transac t";
SqlCommand cmd = new SqlCommand(insertsale, Con);
cmd.ExecuteNonQuery();
Con.Close();