触发器不执行,为什么

fhp_cf 2007-07-27 08:09:18
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,这是为什么?
...全文
763 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
vefo 2007-07-27
  • 打赏
  • 举报
回复
zhenmeiyisi

正解

hellowork 2007-07-27
  • 打赏
  • 举报
回复
才看到楼主的另外一个帖子:
http://community.csdn.net/Expert/topic/5678/5678495.xml?temp=.181637
原来楼主是要在插入的时候自动重排名次.使用触发器这样的处理方法在数据量少的时候还可以,但数据量多的时候会影响性能.
如果就楼主本身的需求来讲,完全可以不用在插入的时候自动计算名称,而是在查询的时候再生成名次,例如:
----创建测试数据
declare @t table(姓名 varchar(10),成绩 int)
insert @t
select 'a',60 union all
select 'b',50 union all
select 'c',40 union all
select 'd',80 union all
select 'e',90

----查询
select
名次 = (select count(*) from @t where 成绩 >= a.成绩),
* from @t as a order by 名次

/*结果
名次 姓名 成绩
----------- ---------- -----------
1 e 90
2 d 80
3 a 60
4 b 50
5 c 40
*/
hellowork 2007-07-27
  • 打赏
  • 举报
回复
----创建测试数据
if object_id('tbTest') is not null
drop table tbTest
GO
----表中要有唯一定位行的ID列,用于在触发器中与inserted表连接
create table tbTest(id int identity(1,1),名次 int,姓名 varchar(10),分数 int)
GO
----创建触发器
create trigger trg_insert_tbTest on tbTest
for insert
as
----获得插入前的最大名次
declare @max int
select @max = isnull(max(名次),0) from tbTest
select @max
----对新插入的行逐行更新名次(可适用批量插入的情况)
update a set
@max = @max + 1,
名次 = @max
from tbTest as a inner join inserted as i on a.id = i.id
GO

----测试单个插入(不要指定名次值,由触发器自动生成)
insert tbTest(姓名,分数) select 'c',80
GO
----测试批量插入(不要指定名次值,由触发器自动生成)
insert tbTest(姓名,分数)
select 'a',60 union all
select 'b',70
GO
----查看结果
select * from tbTest

----清除测试环境
drop table tbTest

/*结果
id 名次 姓名 分数
----------- ----------- ---------- -----------
1 1 c 80
2 2 a 60
3 3 b 70
*/
fhp_cf 2007-07-27
  • 打赏
  • 举报
回复
楼上的别说风凉话啊

bootupnow 2007-07-27
  • 打赏
  • 举报
回复
不是触发器不执行,是不按照lz的意思执行吧,呵呵
fhp_cf 2007-07-27
  • 打赏
  • 举报
回复
你说的很对 hellowork(一两清风)

那我该怎么做呢?
hellowork 2007-07-27
  • 打赏
  • 举报
回复
如果int t就是触发器中的变量@fen的话,楼主这样写触发器不行,即使去掉where 分数 >@fen也会导致其它应用程序商业规则问题.
楼主的意思好像是在触发器中自动在已有名次的最大值上加1.
zhenmeiyisi 2007-07-27
  • 打赏
  • 举报
回复
?

CREATE TRIGGER TR_CHENGJI ON [dbo].[成绩表]
AFTER INSERT
AS
declare @fen int
select @fen=分数 from inserted
begin
update 成绩表 set 名次 = 名次 + 1 where 分数 >@fen
END
fhp_cf 2007-07-27
  • 打赏
  • 举报
回复
hellowork(一两清风):
有QQ吗?
hellowork 2007-07-27
  • 打赏
  • 举报
回复
触发器没什么神秘的地方,它就像事件一样,当事件发生时(insert,update,delete),SQLSERVER会自动调用我们写好的代码而已.
fhp_cf 2007-07-27
  • 打赏
  • 举报
回复
string myCommandText = @"INSERT INTO 成绩表 (名次,姓名,分数) Values (" +
"'1','fg','" + t + "')";

我这里面的t就是@fen的值啊
对不起楼上的各位老大
我是第一次用触发器
实在是娄了点
见凉
hellowork 2007-07-27
  • 打赏
  • 举报
回复
declare @fen int
begin
update 成绩表 set 名次 = 名次 + 1 where 分数 >@fen
--------------------------------------------------------------------------
没有为@fen赋值,则@fen默认为NULL,这样的话where返回假值,所以更新不会成功.请为@fen赋值试试.
zhenmeiyisi 2007-07-27
  • 打赏
  • 举报
回复
>@fen 有值吗?
hb_gx 2007-07-27
  • 打赏
  • 举报
回复
什么添进去?
你随便找个地方运行看看你的触发器有没被禁用了
select is_disabled from sys.triggers where name = 'TR_CHENGJI'
如果是1就禁用了,那你就打开,是0就算了


另外建议你看看你的 where 分数 >@fen 是不是这里的问题?,每次可能一个什么条件都没满足,因为不知道你的分数字段是 int 还是 numeric ,如果你字段的类型不行,你的条件可能永远不会成立,所以每次都没更新
fhp_cf 2007-07-27
  • 打赏
  • 举报
回复
在哪里添进去?
hb_gx 2007-07-27
  • 打赏
  • 举报
回复
你的触发器是不是被你无意间禁用了

启用 ENABLE TRIGGER ALL ON [dbo].[成绩表]

34,873

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧