我写了触发器,表单不能修改了,触发器错在那里?(20分)

  • 主题发起人 主题发起人 xbbtzhao
  • 开始时间 开始时间
X

xbbtzhao

Unregistered / Unconfirmed
GUEST, unregistred user!
我写了触发器,表单不能修改了,触发器错在那里?<br>触发器:<br>CREATE TRIGGER [test3] ON dbo.UserData <br>FOR &nbsp;UPDATE <br>AS<br>DECLARE @USname &nbsp; &nbsp; &nbsp;nvarchar(50)<br>DECLARE @USid &nbsp; &nbsp; &nbsp;nvarchar(50)<br>IF @@ROWCOUNT &gt;0<br>BEGIN<br> &nbsp;SELECT @USname=USname, @USid=USid FROM Inserted<br><br> &nbsp;UPDATE Organize set Cname=@USname where USid=@USid<br>end
 
UserData,Organize是什么结构?
 
UPDATE UserData set Cname=@USname and USid=@USid
 
我想UserData表中有两个USid,USname字段被修改时,UserData中的两个USid,USname字段也同进更新<br><br>SQL:<br>CREATE TABLE [dbo].[Organize] (<br> [AutoID] [int] IDENTITY (1, 1) NOT NULL ,<br> [Cname] [nvarchar] (20) COLLATE Chinese_PRC_CI_AS NOT NULL ,<br> [FatherId] [int] NOT NULL ,<br> [USid] [int] NULL ,<br> [onoff] [char] (10) COLLATE Chinese_PRC_CI_AS NULL <br>) ON [PRIMARY]<br>GO<br><br>CREATE TABLE [dbo].[UserData] (<br> [id] [int] IDENTITY (1, 1) NOT NULL ,<br> [USid] [int] NOT NULL ,<br> [USname] [nvarchar] (20) COLLATE Chinese_PRC_CI_AS NOT NULL ,<br> [USbm] [nchar] (10) COLLATE Chinese_PRC_CI_AS NOT NULL ,<br> [USsex] [nchar] (5) COLLATE Chinese_PRC_CI_AS NULL ,<br> [UShf] [nchar] (10) COLLATE Chinese_PRC_CI_AS NULL ,<br> [USsg] [nchar] (2) COLLATE Chinese_PRC_CI_AS NULL ,<br> [USxn] [nchar] (10) COLLATE Chinese_PRC_CI_AS NULL ,<br> [USxx] [nchar] (20) COLLATE Chinese_PRC_CI_AS NULL ,<br> [USzc] [nchar] (10) COLLATE Chinese_PRC_CI_AS NULL ,<br> [USde] [datetime] NULL ,<br> [USjg] [nchar] (20) COLLATE Chinese_PRC_CI_AS NULL ,<br> [USdz] [nchar] (100) COLLATE Chinese_PRC_CI_AS NULL ,<br> [UStel] [nchar] (20) COLLATE Chinese_PRC_CI_AS NULL ,<br> [USrzde] [datetime] NULL ,<br> [USzw] [nchar] (10) COLLATE Chinese_PRC_CI_AS NULL ,<br> [USjb] [nchar] (10) COLLATE Chinese_PRC_CI_AS NULL ,<br> [USbz] [nchar] (30) COLLATE Chinese_PRC_CI_AS NULL ,<br> [USbz1] [nchar] (50) COLLATE Chinese_PRC_CI_AS NULL <br>) ON [PRIMARY]<br>GO
 
你是根据id更新的,那么id是不是主键?<br>if updated(USname)<br>begin &nbsp;<br>UPDATE Organize set Cname=USname from inserted A where USid=A.USid<br>end
 
你可以高断点跟踪调试你的触发器,参考我的笔记吧:<br>http://www.delphibbs.com/keylife/iblog_show.asp?xid=25217<br><br>授之以鱼不如授之以渔啊
 
updated(USname),这句有问题,不可识别的函数<br><br>CREATE TRIGGER [test3] ON dbo.UserData <br>FOR &nbsp;UPDATE <br>AS<br>DECLARE @USname &nbsp; &nbsp; &nbsp;nvarchar(50)<br>DECLARE @USid &nbsp; &nbsp; &nbsp;nvarchar(50)<br>IF @@ROWCOUNT &gt;0<br>BEGIN<br> if updated(USname)<br> &nbsp;begin<br> &nbsp;UPDATE Organize set Cname=USname from inserted A where USid=A.USid<br><br> &nbsp;end<br>end
 
CREATE TRIGGER [test3] ON dbo.UserData <br>FOR &nbsp;UPDATE <br>AS<br>BEGIN<br> IF UPDATE(USname)<br> &nbsp;begin<br> &nbsp; &nbsp;UPDATE Organize set Cname=USname from inserted A where USid=A.USid<br> &nbsp;end<br>end
 
多谢,但还是不对哟<br>CREATE TRIGGER [test3] ON dbo.UserData <br>FOR &nbsp;UPDATE <br>AS<br>DECLARE @USname &nbsp; &nbsp; &nbsp;nvarchar(50)<br>DECLARE @USid &nbsp; &nbsp; &nbsp;nvarchar(50)<br>BEGIN<br> IF UPDATE(USname)<br> &nbsp;begin<br> &nbsp; &nbsp;UPDATE Organize set Cname=USname from inserted A where USid=A.USid //提示例名USid不明确,还有这个A是什么意思?<br> &nbsp;end<br>end
 
CREATE TRIGGER [test3] ON dbo.UserData <br>FOR &nbsp;UPDATE <br>AS<br>BEGIN<br> IF UPDATE(USname)<br> &nbsp;begin<br> &nbsp; &nbsp;UPDATE Organize set Cname=USname from inserted where Organize.USid=inserted.USid<br> &nbsp;end<br>end
 
成功了,多谢
 
后退
顶部