简体   繁体   English

SSIS中的多个语句执行SQL任务

[英]Multiple statements in SSIS Execute SQL Task

In my SSIS package I have an Execute SQL Task with 2 statements. 在我的SSIS包中,我有一个带有2条语句的Execute SQL Task。 Basically, they are: 基本上,它们是:

INSERT INTO table2 SELECT * FROM table1;
TRUNCATE TABLE table1

First statement failed (duplicate records). 第一条语句失败(重复记录)。 But table1 is empty now! 但是table1现在是空的! Do I misunderstand basic principles of databases?! 我会误解数据库的基本原理吗? I expected the whole batch to fail if the first statement failed. 如果第一条语句失败,我希望整个批处理都将失败。

In the execution report there are expected error messages: 1. Cannot insert duplicate key row. 在执行报告中,有预期的错误消息:1.无法插入重复的键行。 2. The statement has been terminated. 2.该语句已终止。 Possible failure reasons bla-bla 可能的故障原因bla-bla

My server is SQL Server 2012 SP2 CU1, OLEDB connection 我的服务器是SQL Server 2012 SP2 CU1,OLEDB连接

There are no other truncate statements for this table known to me. 我知道此表没有其他截断语句。 Have I gone insane? 我疯了吗?

You need to trap for error .. 您需要捕获错误..

INSERT INTO table2 SELECT * FROM table1;
IF @@ERROR = 0
    TRUNCATE TABLE table1

Okay, after some testing I confirmed that this is the intended behavior. 好的,经过一些测试,我确认这是预期的行为。 Possible solutions include: 可能的解决方案包括:

  1. Using IF @@ERROR (thanks t_m!) 使用IF @@ ERROR(感谢t_m!)
  2. Splitting lines into batches with GO. 使用GO将生产线分成批次。 Lines within a batch will execute but the next batch will not. 一批中的行将执行,但下一批将不行。
  3. Splitting lines into multiple tasks (duh). 将行拆分为多个任务(duh)。

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

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