有时我们需要读出刚插入数据库的那条记录的ID,然后利用这条记录来插入其它的记录。这个时候,我们就需要使用子查询了。
具体代码如下:
照正常理解应该是这样的:
insert into [Bgs_Receive] (Bgs_Post_id,Yu_User_Id) values ((select top 1 Bgs_Post_id from [Bgs_Post] where Yu_User_Id=@Yu_User_Id order by Bgs_Post_Date desc)," + CheckBoxList_Teacher.Items[i].Value + ")
可是MsSQL不支持insert 使用子查询
正确的写法是这样的:
insert into [Bgs_Receive] (Bgs_Post_id,Yu_User_Id) select top 1 Bgs_Post_id," + CheckBoxList_Teacher.Items[i].Value + " from [Bgs_Post] where Yu_User_Id=@Yu_User_Id order by Bgs_Post_Date desc
注意去掉values