简体   繁体   English

如果 DML 语句包含一个没有 INTO 子句的 OUTPUT 子句,则该语句不能有任何启用的触发器

[英]DML statement cannot have any enabled triggers if the statement contains an OUTPUT clause without INTO clause

I have been unsuccessfully trawling StackOverflow and other sites to solve my error so time to post and see if someone can tell me where I'm going wrong.我一直未能成功地拖网 StackOverflow 和其他网站来解决我的错误,所以有时间发帖看看是否有人可以告诉我哪里出错了。

My system has a PowerApp with a simple form.我的系统有一个带有简单表单的 PowerApp。 On saving the form, the fields are written to table1.保存表单时,字段将写入 table1。 I then want to create a trigger on table1 so that one field from each new record is inserted into table2.然后我想在 table1 上创建一个触发器,以便将每个新记录中的一个字段插入 table2。

Despite there being no output clause in my code, I am getting the following error:尽管我的代码中没有输出子句,但我收到以下错误:

The requested operation is invalid.请求的操作无效。 Server Response: Microsoft SQL: The target table 'table1' of the DML statement cannot have any enabled triggers if the statement contains an OUTPUT clause without INTO clause.服务器响应:Microsoft SQL:如果 DML 语句的目标表 'table1' 包含没有 INTO 子句的 OUTPUT 子句,则该语句不能有任何启用的触发器。 inner exception: Microsoft SQL: The target table 'table1' of the DML statement cannot have any enabled triggers if the statement contains an OUTPUT clause without INTO clause.内部异常:Microsoft SQL:如果 DML 语句的目标表 'table1' 包含没有 INTO 子句的 OUTPUT 子句,则该语句不能有任何已启用的触发器。

Stripping my code back to the following still throws the same error:将我的代码剥离回以下仍然会引发相同的错误:

ALTER TRIGGER [dbo].[FormatID] 
   ON  [dbo].[table1]
   AFTER INSERT
AS 
BEGIN

    INSERT INTO [dbo].[table2](origID) 
    VALUES (1)

END

Any suggestions please fire away, I feel as though I am missing something obvious...任何建议请开火,我觉得我好像遗漏了一些明显的东西......

your application is OUTPUT-ing inserted/deleted values of a dml statement as a resultset.您的应用程序将 dml 语句的插入/删除值输出为结果集。 This is not possible when there are triggers on the table (target of the dml)当表上有触发器(dml 的目标)时,这是不可能的

create table dbo.table1(id int);
create table dbo.table2(origID int);
go

CREATE OR ALTER TRIGGER [dbo].[FormatID] 
   ON  [dbo].[table1]
   AFTER INSERT
AS 
BEGIN

    INSERT INTO [dbo].[table2](origID) 
    VALUES (1)

END;


--succeeds
insert into dbo.table1(id) 
values (1);
go

--fails
insert into dbo.table1(id) 
output inserted.id --output cannot be a resultset(in the void) because there is a trigger
values(1);

--succeeds
declare @outputtable table (id int);
insert into dbo.table1(id) 
output inserted.id into @outputtable(id)--output into a table
values(1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM