日期:2014-05-19  浏览次数:20640 次

触发器不执行,为什么
CREATE   TRIGGER     TR_CHENGJI   ON   [dbo].[成绩表]  
AFTER   INSERT
AS
declare   @fen   int
begin
update   成绩表   set   名次   =   名次   +   1   where   分数   > @fen
END


/*****************************************************
private   void   button1_Click(object   sender,   EventArgs   e)
                {
                        int   t;
                        t=**;//两位随机数
                        string   strConn   =   @ "Data   Source=127.0.0.1,1433;Initial   Catalog=gpData;User   ID=sa;Password=***** ";
                        SqlConnection   myConnection   =   new   SqlConnection(strConn);
                        myConnection.Open();
                        string   myCommandText   =   @ "INSERT   INTO   成绩表   (名次,姓名,分数)   Values   ( "   +
                            " '1 ', 'fg ', ' "   +   t   +   " ') ";

                        SqlCommand   myCommand   =   new   SqlCommand(myCommandText,   myConnection);
                        myCommand.ExecuteNonQuery();
                        myConnection.Close();
                }
//************************************
我在程序里按了按纽之后,记录是添加进去了,可名次就是不改变,还是1,这是为什么?

------解决方案--------------------
你的触发器是不是被你无意间禁用了

启用 ENABLE TRIGGER ALL ON [dbo].[成绩表]
------解决方案--------------------
什么添进去?
你随便找个地方运行看看你的触发器有没被禁用了
select is_disabled from sys.triggers where name = 'TR_CHENGJI '
如果是1就禁用了,那你就打开,是0就算了


另外建议你看看你的 where 分数 > @fen 是不是这里的问题?,每次可能一个什么条件都没满足,因为不知道你的分数字段是 int 还是 numeric ,如果你字段的类型不行,你的条件可能永远不会成立,所以每次都没更新

------解决方案--------------------
> @fen 有值吗?

------解决方案--------------------
declare @fen int
begin
update 成绩表 set 名次 = 名次 + 1 where 分数 > @fen
--------------------------------------
没有为@fen赋值,则@fen默认为NULL,这样的话where返回假值,所以更新不会成功.请为@fen赋值试试.
------解决方案--------------------
?

CREATE TRIGGER TR_CHENGJI ON [dbo].[成绩表]
AFTER INSERT
AS
declare @fen int
select @fen=分数 from inserted
begin
update 成绩表 set 名次 = 名次 + 1 where 分数 > @fen
END
------解决方案--------------------
才看到楼主的另外一个帖子:
http://community.csdn.net/Expert/topic/5678/5678495.xml?temp=.181637
原来楼主是要在插入的时候自动重排名次.使用触发器这样的处理方法在数据量少的时候还可以,但数据量多的时候会影响性能.