首页 \ 问答 \ TSQL触发器不保存变量和/或不正确执行(TSQL Trigger Not Saving Variables and/or not Executing Properly)

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
更新时间:2022-08-05 06:08

最满意答案

我的模型是否有意义?

它不是:如果你的目标是这样的,比如说,爱丽丝是一个家庭的母亲,也是另一个孩子的母亲,那么爱丽丝在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 in persons be? Should Alice be an instance of Parent or Child?

Subclasses must be disjunct sets.

相关问答

更多
  • 问题在于字段“间隔”。 它是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 ...
  • 如果你不想使用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 ...
  • 这是个好主意吗 ? 这取决于。 它违反了规范化,因为表没有单一用途。 当你第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 ...
  • 好。 事实证明,这是因为rails始终没有看到整个继承层次结构。 当它重新加载每个请求中的所有项时,这解释了不一致的行为(在某些地方,before_filter可能导致模型加载,在其他地方也许不会)。 它可以通过推杆固定 require_dependency 'stock_thing_template' 在我所有参考这些东西的控制器的顶部。 有关rails wiki的更多信息 - 请转到页面底部 OK. Turns out this is because rails doesn't see the ent ...
  • 好的,我发现了您的错误,但您应该在日志中看到以前的错误: 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 ...
  • 我知道这不是你问题的答案,但我认为你应该重新考虑使用其他东西而不是继承。 继承是一种很好的OO机制,但经常被过度使用。 大多数情况下,您可以通过关联和某种策略模式比继承更好地完成工作。 Activerecords与继承这种设计更加兼容,它也更加尊重单一责任原则,这意味着它更容易单独测试。 如果你想在你的activerecord模型中使用策略模式,可以采用多态关联(这使你甚至可以拥有有状态策略) I know it is not the answer to your question but I think ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)