TSQL触发器不保存变量和/或不正确执行(TSQL Trigger Not Saving Variables and/or not Executing Properly)
我无法让TSQL触发器正常工作。 我通过调试器运行它并没有根据SQL Server Management Studio设置任何变量。 最糟糕的事情是触发器本身正确执行,执行时没有错误(只是说'执行成功')。
代码如下(这是一项正在进行的工作....只是让我自己熟悉):
USE TestDb IF EXISTS (SELECT name FROM sysobjects WHERE name = 'OfficeSalesQuotaUpdate' AND type = 'TR') DROP TRIGGER OfficeSalesQuotaUpdate GO CREATE TRIGGER OfficeSalesQuotaUpdate ON SalesReps AFTER UPDATE, DELETE, INSERT AS DECLARE @sales_difference int, @quota_difference int DECLARE @sales_original int, @quota_original int DECLARE @sales_new int, @quota_new int DECLARE @officeid int DECLARE @salesrepid int --UPDATE(Sales) returns true for INSERT and UPDATE. --Not for DELETE though. IF ((SELECT COUNT(*) FROM inserted) = 0) SET @salesrepid = (SELECT SalesRep FROM deleted) ELSE SET @salesrepid = (SELECT SalesRep FROM inserted) --If you address the @salesrepid variable, it does not work. Doesn't even --print out the 'this should work line. PRINT 'This should work...' --+ convert(char(30), @salesrepid) IF (@salesrepid = NULL) PRINT 'SalesRepId is null' ELSE PRINT 'SalesRepId is not null' PRINT convert(char(50), @salesrepid) SET @officeid = (SELECT RepOffice FROM SalesReps WHERE SalesRep = @salesrepid) SELECT @sales_original = (SELECT Sales FROM deleted) SELECT @sales_new = (SELECT Sales FROM inserted) --Sales can not be null, so we'll remove this later. --Use this as a template for quota though, since that can be null. IF (@sales_new = null) BEGIN SET @sales_new = 0 END IF (@sales_original = 0) BEGIN SET @sales_original = 0 END SET @sales_difference = @sales_new - @sales_original UPDATE Offices SET Sales = Sales + @sales_difference WHERE Offices.Office = @officeid GO
那么,有什么提示吗? 我完全不知道这个。 提前致谢。
I've having trouble getting a TSQL trigger to even work correctly. I've run it through the debugger and it's not setting any of the variables according to SQL Server Management Studio. The damnedest thing is that the trigger itself is executing correctly and there are no errors when it is executed (just says 'execution successful').
The code is as follows (it's a work in progress.... just getting my self familiar):
USE TestDb IF EXISTS (SELECT name FROM sysobjects WHERE name = 'OfficeSalesQuotaUpdate' AND type = 'TR') DROP TRIGGER OfficeSalesQuotaUpdate GO CREATE TRIGGER OfficeSalesQuotaUpdate ON SalesReps AFTER UPDATE, DELETE, INSERT AS DECLARE @sales_difference int, @quota_difference int DECLARE @sales_original int, @quota_original int DECLARE @sales_new int, @quota_new int DECLARE @officeid int DECLARE @salesrepid int --UPDATE(Sales) returns true for INSERT and UPDATE. --Not for DELETE though. IF ((SELECT COUNT(*) FROM inserted) = 0) SET @salesrepid = (SELECT SalesRep FROM deleted) ELSE SET @salesrepid = (SELECT SalesRep FROM inserted) --If you address the @salesrepid variable, it does not work. Doesn't even --print out the 'this should work line. PRINT 'This should work...' --+ convert(char(30), @salesrepid) IF (@salesrepid = NULL) PRINT 'SalesRepId is null' ELSE PRINT 'SalesRepId is not null' PRINT convert(char(50), @salesrepid) SET @officeid = (SELECT RepOffice FROM SalesReps WHERE SalesRep = @salesrepid) SELECT @sales_original = (SELECT Sales FROM deleted) SELECT @sales_new = (SELECT Sales FROM inserted) --Sales can not be null, so we'll remove this later. --Use this as a template for quota though, since that can be null. IF (@sales_new = null) BEGIN SET @sales_new = 0 END IF (@sales_original = 0) BEGIN SET @sales_original = 0 END SET @sales_difference = @sales_new - @sales_original UPDATE Offices SET Sales = Sales + @sales_difference WHERE Offices.Office = @officeid GO
So, any tips? I've completely stumped on this one. Thanks in advance.
原文:https://stackoverflow.com/questions/4302088
最满意答案
我的模型是否有意义?
它不是:如果你的目标是这样的,比如说,爱丽丝是一个家庭的母亲,也是另一个孩子的母亲,那么爱丽丝在
persons
中排行的type
应该是什么? Alice应该是Parent
还是Child
的实例?子类必须是分离集。
Does my model make sense?
It does not: if your goal is that, say, Alice is a mother in some family and also a child in some other, what should the
type
of Alice's row inpersons
be? Should Alice be an instance ofParent
orChild
?Subclasses must be disjunct sets.
相关问答
更多-
Hibernate:一对多和继承,表没有被创建(Hibernate: one-to-many and inheritance, table not getting created)[2023-04-10]
问题在于字段“间隔”。 它是MySql中的保留字。 将其更改为其他内容。 The problem is with the field 'interval'. It is a reserved word in MySql. Change it to something else. -
我的模型是否有意义? 它不是:如果你的目标是这样的,比如说,爱丽丝是一个家庭的母亲,也是另一个孩子的母亲,那么爱丽丝在persons中排行的type应该是什么? Alice应该是Parent还是Child的实例? 子类必须是分离集。 Does my model make sense? It does not: if your goal is that, say, Alice is a mother in some family and also a child in some other, what sho ...
-
使用单表继承登录(Login with Single Table Inheritance)[2022-01-16]
如果你不想使用get_class,你会做这样的事情 class Client extends Account { protected $discr = 'client'; ... } class Administrator extends Account { protected $discr = 'administrator'; ... } 对象中的字段(不在数据库中) 并在Acount类中创建getType()方法 if you do not want to use ... -
单表继承(Single Table Inheritance)[2022-07-01]
这是个好主意吗 ? 这取决于。 它违反了规范化,因为表没有单一用途。 当你第n次扩展基类时会发生什么? 您必须向表中添加列。 大多数现代数据库都没有问题,因为你可以修改表,但是重构和删除类呢。 现在你有了没有目的的列。 经验法则 - 如果大部分设计已经完成,那么它可能是安全的。 如果设计经常变化 - 您还有其他问题,需要锁定用例/用户要求。 (是的,不是真正的XP友好) 我不知道Rails效果。 Is it a good idea ? It depends. It breaks Normalization ... -
解决了。 查询应该是: from Guide g where parent.idGuide = :parent order by type Solved. Query should have been: from Guide g where parent.idGuide = :parent order by type
-
不,单表继承的工作方式不同。 继承策略需要在超类上定义。 请参阅JPA Wikibook以供参考。 @Entity @Inheritance @DiscriminatorColumn(name="USER_TYPE") @Table(name="USER") public abstract class BaseUser{ ... } @Entity @DiscriminatorValue("A") public class Admin extends BaseUser{ ... } @Entity @D ...
-
单表继承查找问题(Single Table Inheritance find issues)[2023-02-09]
好。 事实证明,这是因为rails始终没有看到整个继承层次结构。 当它重新加载每个请求中的所有项时,这解释了不一致的行为(在某些地方,before_filter可能导致模型加载,在其他地方也许不会)。 它可以通过推杆固定 require_dependency 'stock_thing_template' 在我所有参考这些东西的控制器的顶部。 有关rails wiki的更多信息 - 请转到页面底部 OK. Turns out this is because rails doesn't see the ent ... -
hibernat中的单表继承显示SQL语法异常(single table inheritance in hibernate showing SQL grammar exception)[2023-02-22]
好的,我发现了您的错误,但您应该在日志中看到以前的错误: ERROR: HHH000389: Unsuccessful: create table Person (DTYPE varchar(31) not null, id varchar(255) not null auto_increment, Name varchar(255), age integer not null, sex varchar(255), pincode integer, postcode varchar(255), primar ... -
在您的属性实体中具有多个子类(Composers和authors)关联是没有意义的。因为您具有单表继承,即您的属性所有者表将具有property_id的外键。 相反,您可以拥有单一关联。 @OneToMany Set
composers = new HashSet (); 您可以使用DiscriminatorColumn类型将此集合拆分为内存中的authors和composer。 There is no point in having multip ... -
Ruby on Rails 3单表继承还是多表继承?(Ruby on Rails 3 single table inheritance or multiple table inheritance?)[2023-11-27]
我知道这不是你问题的答案,但我认为你应该重新考虑使用其他东西而不是继承。 继承是一种很好的OO机制,但经常被过度使用。 大多数情况下,您可以通过关联和某种策略模式比继承更好地完成工作。 Activerecords与继承这种设计更加兼容,它也更加尊重单一责任原则,这意味着它更容易单独测试。 如果你想在你的activerecord模型中使用策略模式,可以采用多态关联(这使你甚至可以拥有有状态策略) I know it is not the answer to your question but I think ...