if object_id('tb1') is not null drop table tb1 
 go 
create table tb1([id] int,[学号] varchar(10),[课程编号] int)
insert tb1 select 1,'510331101',1111
union all select 2,'510331102',1111
union all select 3,'510331103',1111
go
if object_id('tb2') is not null drop table tb2 
 go 
create table tb2([已选人数] int,[课程编号] int)
insert tb2 select 3,1111
go
if object_id('tg') is not null drop trigger tg 
 go
create trigger tg on tb1
for insert,delete
as
set nocount on
update b set b.[已选人数]=a.cnt
from (select count(1) cnt,课程编号 from tb1 group by 课程编号) a
  join tb2 b on a.课程编号=b.课程编号
set nocount off
godelete tb1 where id=3select * from tb2
/*
已选人数        课程编号
----------- -----------
2           1111(1 行受影响)
*/
insert tb1 select 3,'510331103',1111
select * from tb2
/*
已选人数        课程编号
----------- -----------
3           1111(1 行受影响)
*/

解决方案 »

  1.   

    在查询分析器里执行没问题但是我作为网站的后台数据库在前台页面里的gridview控件里删除记录就会出现这个错误:
    指定的参数已超出有效值的范围。
    参数名: value 
    说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.ArgumentOutOfRangeException: 指定的参数已超出有效值的范围。
    参数名: value源错误: 执行当前 Web 请求期间生成了未处理的异常。可以使用下面的异常堆栈跟踪信息确定有关异常原因和发生位置的信息。  堆栈跟踪: 
    [ArgumentOutOfRangeException: 指定的参数已超出有效值的范围。
    参数名: value]
       System.Web.UI.WebControls.GridView.set_SelectedIndex(Int32 value) +1522711
       System.Web.UI.WebControls.GridView.HandleDeleteCallback(Int32 affectedRows, Exception ex) +354
       System.Web.UI.DataSourceView.Delete(IDictionary keys, IDictionary oldValues, DataSourceViewOperationCallback callback) +123
       System.Web.UI.WebControls.GridView.HandleDelete(GridViewRow row, Int32 rowIndex) +927
       System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) +1134
       System.Web.UI.WebControls.GridView.RaisePostBackEvent(String eventArgument) +199
       System.Web.UI.WebControls.GridView.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
       System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11
       System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +177
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1746 
    我是用visual studio 2008  (C#)语言做的网站
    请帮忙解决下!!谢谢!!