比较 3 个用户变量(行计数),如果不匹配,则引发错误



我正在SSIS包中使用脚本任务。

我想比较3个用户变量(3个表之间的行数(,如果它们不相等,就会引发错误。否则就是成功。

我是C#开发的新手。

我需要你的帮助。

感谢

这是我的代码:

public void Main()
        {
            // TODO: Add your code here
            Dts.TaskResult = (int)ScriptResults.Success;
            //MessageBox.Show(Dts.Variables["User::RowCtn_src1"].Value.ToString());
            //MessageBox.Show(Dts.Variables["User::RowCtn_src2"].Value.ToString());
            //MessageBox.Show(Dts.Variables["User::RowCtn_src3"].Value.ToString());
            Dts.Variables["User::RowCtn_src1"].Value = "";
            Dts.Variables["User::RowCtn_src2"].Value = "";
            Dts.Variables["User::RowCtn_src3"].Value = "";
            Dts.Variables["User::Err"].Value = "";
            
            if ("User::RowCtn_src1" == "User::RowCtn_src2" == "User::RowCtn_src3")     //As you an see there is error in the code
                //this what I want : 
                //if it's true then success otherwise raise un error'
                //How can I do it?

两件事:

  • 行计数变量应定义为数字数据类型,如int
  • 变量应该在之前的一些任务中设置,比如数据流
  • 3个之间的比较可以通过比较两对来完成,那么所有3个必须匹配

你可以这样做:

    public void Main()
    {
        int src1 = (int)Dts.Variables["User::RowCtn_src1"].Value;
        int src2 = (int)Dts.Variables["User::RowCtn_src2"].Value;
        int src3 = (int)Dts.Variables["User::RowCtn_src3"].Value;
        if (src1 != src2 || src2 != src3)
        {
            var msg = $"Counts do not match.  src1= {src1}; src2= {src2}; src3= {src3};";
            Dts.Events.FireError(0, "Row Count Check", msg, "", 0);
            Dts.TaskResult = (int)ScriptResults.Failure;
        }
        Dts.TaskResult = (int)ScriptResults.Success;
    }

如果要测量表计数,您可能需要在SQL任务中尝试此操作。

将结果设置为一个变量,然后使用控制流路径将0流到错误处理程序中。

select iif(ct1=ct2,iif(ct2=ct3,1,0),0) doCountsMatch
from (select count(*) ct1 from table1) a
cross apply (select count(*) ct2 from table2) b
cross apply (select count(*) ct3 from table3) c

这也会起作用,更容易添加更多的表计数:

select iif(Min(ct)=Max(ct),1,0) doCountsMatch
from (select count(*) ct from table1 
union all select count(*) ct2 from table2
union all select count(*) ct3 from table3) cts

相关内容

最新更新