对于TSQL插入/更新触发器,可以插入和删除都为空吗?(For a TSQL insert/update trigger, can inserted and deleted both be empty?)
我一直在使用TSQL触发器,假设insert表总是包含插入或更新的记录,而删除的表总是包含更新记录(我忽略删除)。
这是相关的MSDN文章:
http://msdn.microsoft.com/en-us/library/ms191300.aspx
但是,我遇到插入和删除都为空的情况。 这是我一直在使用的测试触发器。
CREATE TRIGGER [dbo].[InsertUpdateTest] ON [dbo].[Test] AFTER INSERT, UPDATE AS DECLARE @countInserted INT DECLARE @countDelete INT SET @countInsert = (SELECT COUNT(*) FROM INSERTED) SET @countDeleted = (SELECT COUNT(*) FROM DELETED) IF (@countInserted = 0 AND @countDelete = 0) BEGIN print 'Inserted and deleted are both empty' END
在什么条件下会发生这种情况?
I have been using a TSQL trigger under the assumption that the inserted table always contains records for an insert or update and the deleted table always contains records for updates (I am ignoring deletes).
Here is the related MSDN article:
http://msdn.microsoft.com/en-us/library/ms191300.aspx
However, I have encountered situations where both inserted and deleted are empty. Here is a test trigger I have been using.
CREATE TRIGGER [dbo].[InsertUpdateTest] ON [dbo].[Test] AFTER INSERT, UPDATE AS DECLARE @countInserted INT DECLARE @countDelete INT SET @countInsert = (SELECT COUNT(*) FROM INSERTED) SET @countDeleted = (SELECT COUNT(*) FROM DELETED) IF (@countInserted = 0 AND @countDelete = 0) BEGIN print 'Inserted and deleted are both empty' END
Under what conditions does this occur?
原文:https://stackoverflow.com/questions/11791294
最满意答案
请参阅https://stackoverflow.com/a/27574474上的用户'sprocket'答案:
ROWS和RANGE允许窗口函数查看用户定义的行窗口,例如前面的27运行移动平均值:
SELECT spend, SUM(spend) OVER (PARTITION BY user ORDER BY date ROWS BETWEEN 27 PRECEDING AND CURRENT ROW), user, date FROM user_spend;
See user 'sprocket' answer at https://stackoverflow.com/a/27574474:
ROWS and RANGE allow the window function to look at a user defined window of rows, for example the previous 27 to run a moving average:
SELECT spend, SUM(spend) OVER (PARTITION BY user ORDER BY date ROWS BETWEEN 27 PRECEDING AND CURRENT ROW), user, date FROM user_spend;
相关问答
更多-
未定义的是一个普通变量,可以使用undefined = "new value";简单地更改undefined = "new value"; 。 所以jQuery创建一个非重定义的本地“未定义”变量。 出于性能原因,窗口变量是本地的。 因为当JavaScript查找变量时,它首先遍历本地变量,直到找到变量名称为止。 当没有找到它时,JavaScript会遍历下一个范围等,直到它遍历全局变量。 因此,如果窗口变量是本地的,JavaScript可以更快地查找。 更多信息: 加快您的JavaScript - Nic ...
-
什么是功能($){$(function(){})}(window.jQuery)吗?(What does !function ($) { $(function(){ }) }(window.jQuery) do?)[2022-02-12]
让我们解释一下打破代码 function () { }() 或者经常写成 (function () { })() 是一个self-invoking anonymous函数,也称为即时调用函数表达式(IIFE) 。 它立即执行匿名函数内联。 在解释封装的匿名函数语法中阅读更多信息。 匿名函数是一个强大的功能,并且具有范围(“变量名称间隔”)等优点,请参阅javascript中自行执行函数的目的是什么? 。 现在他们正在使用 function ($) { }(window.jQuery) 我们跳过! 目 ... -
局部常量 两者都是正确的语法,但是在where子句中,除非是递归调用,否则不需要参数化任何东西,因此您的第一个变体更好: complexMath num1 num2 = sum + 1000 where sum = num1 + num2 这是非参数化的where如果有一个你想要重复使用的值,那么这是非常有用的 complexMath num1 num2 = sum * (sum + 1000) where sum = num1 + num2 第二个不需要括号,因为函数应用具有更高的优先级 c ...
-
您可以使用lag()并显式计算平均值。 select t.*, (score + (case when lag(date, 1) over (partition by id order by date) >= date - interval '2 day' then lag(score, 1) over (partition by id order by date) else 0 ...
-
聚合窗口中的Filter子句不按预期方式丢弃行(Filter clause in aggregate window not discarding rows as expected)[2022-02-03]
over子句优先于filter子句。 所以你取last_2(即当前行和前一行)并从这些过滤器中获取只有一行(偶数一行)。 你要找的是这样的: sum(case when num % 2 = 0 then num else 0 end) over last_2 The over clause has precedence over the filter clause. So you take last_2 (i.e. the current row and the previous to it) and f ... -
PostgreSQL窗口函数“列必须出现在GROUP BY子句中”(PostgreSQL Window Function “column must appear in the GROUP BY clause”)[2022-07-19]
您需要GROUP BY user_id,因为它没有被聚合。 然后你可以按照你想要的降序排列SUM(score) 。 SQL小提琴演示 SELECT user_id, SUM(score), RANK() OVER (ORDER BY SUM(score) DESC) FROM rewards GROUP BY user_id; user_id | sum | rank ---------+-----+------ 1 | 5 | 1 2 | 5 | 1 ... -
从手册中引用 : 如果查询包含任何窗口函数,则在执行任何分组,聚合和HAVING过滤之后对这些函数进行评估 强调我的 Quote from the manual: If the query contains any window functions, these functions are evaluated after any grouping, aggregation, and HAVING filtering is performed emphasis mine
-
请参阅https://stackoverflow.com/a/27574474上的用户'sprocket'答案: ROWS和RANGE允许窗口函数查看用户定义的行窗口,例如前面的27运行移动平均值: SELECT spend, SUM(spend) OVER (PARTITION BY user ORDER BY date ROWS BETWEEN 27 PRECEDING AND CURRENT ROW), user, date FROM user_spend; ...
-
R:window()函数(R: window() function)[2023-08-01]
当您创建时间序列对象时, ts函数期望第一个参数是数据,并且没有时间。 (你可能想要使用zoo-objects。它们更有意义。)看看会发生什么: > window(tser[,1],start=5,end=8) Time Series: Start = 5 End = 8 Frequency = 1 [1] 1275264900 1275265200 1275265500 1275265800 您的日期(以数字表示)已成为数据! 要使用动物园,它非常直接。 我不确定你的起点是什么。 我在数据框中有数 ... -
es6 arrow函数,更多细节https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/Arrow_functions 和类https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Classes es6 arrow function, for more detail https://developer.mozilla.org/en/docs/Web/ ...