码迷,mamicode.com
首页 > 其他好文 > 详细

SSDT-BI之六:脚本任务

时间:2015-05-26 22:50:41      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:

前几篇博客中介绍了‘执行SQL任务’,本次介绍脚本任务:随心所欲的编写C#Code。

1,如下图,从左侧工具箱拖入脚本任务,双击打开脚本任务编辑器:

技术分享

2,点击ReadOnlyVariables右侧的按钮,如图,可以选择在脚本中可以访问的变量(ReadWriteVariables同样的做法):

技术分享

3,点击脚本,如图就可以按照示例编写脚本:

技术分享

4,编写脚本完成,保存之后退出编辑,一个脚本任务就可以正常执行了,另外脚本任务支持调试,就像普通的C#代码在VS中调试一样。

脚本任务可以实现的功能比想象的要多,最起码工具箱中的任务都可以利用脚本任务来实现,当然一般情况下还是使用工具箱中提供的任务更为方便。

我这里举一个我们项目中简单的而且特别的例子。

在我们的项目中。有一些Table是启用了CDC功能的,那这些表的升级改动都需要对CDC进也行相应的升级。CDC的升级分为两个部分,一是数据迁移,而是CDC表升级。升级脚本的结构如下:

1,复制CDC表数据到临时表。

2,禁用CDC

3,启用CDC

4,从临时表复制数据到CDC表

5,删除临时表

在实际测试中发现,如果以上五部操作在一个sql 脚本中执行,将可能导致CDC功能不能正常使用(不能正常的生成数据)。

经过多次测试,我们把以上五个步骤分到了两个脚本文件,如下:

脚本文件1:

1,复制CDC表数据到临时表。

2,禁用CDC

脚本文件2:

3,启用CDC

4,从临时表复制数据到CDC表

5,删除临时表

但是,分成两个脚本文件,并在Pacakge中使用两个执行SQL任务来执行,也是有问题的。再次经过多次惨无人道的测试,我们最终发现如果在执行两个脚本文件的中间能有一个小小的时间间隔,那么最终的效果还是比较理想的,基本上每次都能升级CDC成功。这个时候脚本任务就发挥大作用了,最终的执行路径如下:

执行SQL任务1-》脚本任务(Thread.Sleep(3000))-》执行SQL任务2

至于脚本任务的其他功能和用法,自己参悟吧。另外也可以参考初识情况下,脚本任务中给出的注释。例如,下面的注释就指出了如何使用变量(前提:变量已经在第二步中勾选。):

#region Help:  Using Integration Services variables and parameters in a script
        /* To use a variable in this script, first ensure that the variable has been added to 
         * either the list contained in the ReadOnlyVariables property or the list contained in 
         * the ReadWriteVariables property of this script task, according to whether or not your
         * code needs to write to the variable.  To add the variable, save this script, close this instance of
         * Visual Studio, and update the ReadOnlyVariables and 
         * ReadWriteVariables properties in the Script Transformation Editor window.
         * To use a parameter in this script, follow the same steps. Parameters are always read-only.
         * 
         * Example of reading from a variable:
         *  DateTime startTime = (DateTime) Dts.Variables["System::StartTime"].Value;
         * 
         * Example of writing to a variable:
         *  Dts.Variables["User::myStringVariable"].Value = "new value";
         * 
         * Example of reading from a package parameter:
         *  int batchId = (int) Dts.Variables["$Package::batchId"].Value;
         *  
         * Example of reading from a project parameter:
         *  int batchId = (int) Dts.Variables["$Project::batchId"].Value;
         * 
         * Example of reading from a sensitive project parameter:
         *  int batchId = (int) Dts.Variables["$Project::batchId"].GetSensitiveValue();
         * */

        #endregion

SSDT-BI之六:脚本任务

标签:

原文地址:http://www.cnblogs.com/marsyan/p/4531745.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!