我有表MyTable,我有一After trigger(On Insert)对表.
当我运行批量插入语句时MyTable,它会触发触发器以捕获插入的数据(即用于审计日志目的).
我们截断了表格MyTable,现在在表格中插入5条记录.此外,该Auditlog表还有现有数据.假设审计日志表中已有500条记录.
我的问题是,当表有一个触发器时,在表SCOPE_IDENTITY()的BULK INSERT语句中会返回什么?
--Create table
Create table MyTable
(
FirstCol int identity(1,1) primary key,
SecondCol varchar(10)
)
Create table AuditLog
(
AID int identity(1,1) primary key,
Comments varchar(50)
)
--Insert data to MyTable
INSERT INTO MyTable (SecondCol)
VALUES ('First'), ('Second'), ('Third'), ('Fourth'), ('Fifth')
Select * from MyTable
Select Scope_identity()
Run Code Online (Sandbox Code Playgroud) 我试图找出如何通过查询得到以下结果,但无法做同样的事情.
表名:ProductMaster
ProductCode Quantity EntryDate
A1 10 10/03/2015
A1 10 15/03/2015
A2 10 18/03/2015
A2 10 25/03/2015
A1 10 10/04/2015
A2 10 15/04/2015
Run Code Online (Sandbox Code Playgroud)
我想获得结果如果我选择3月份,结果应该是:
ProductCode MonthCount TotalCount
A1 20 30
A2 20 30
Run Code Online (Sandbox Code Playgroud)
如果我选择4月份,结果应为:
ProductCode MonthCount TotalCount
A1 10 30
A2 10 30
Run Code Online (Sandbox Code Playgroud)
我的查询:
SELECT ProductCode, SUM(Quantity)
FROM ProductMaster
WHERE DATEPART(MONTH, EntryDate) = @Month
GROUP BY ProductCode
Run Code Online (Sandbox Code Playgroud)
在哪里@month = 3或4基于输入.
另外,我如何获得产品代码的数量.对于Month = 3
ProductCode MonthCount TotalCount
A1 2 3
A2 2 3
Run Code Online (Sandbox Code Playgroud) 如果可能,任何人都可以用一个例子详细解释,为什么AFTERSQL Server中的视图不支持触发器?
我知道我们使用AFTER触发器后insert,update或delete桌子上,又何尝不是在视图上,太?