0

我在脚本任务中有这段代码:

Public Sub Main()
    '
    ' Add your code here
    '
    Dim MonthFromSQL As String
    Dim lastMonth As New Date(DateTime.Today.Year, DateTime.Today.Month - 1, 1)
    Dim rcnt As Integer
    Dim msg As String
    'MsgBox("Month name from SQL is " & CStr(Dts.Variables("MonthNameFromSQL").Value))
    rcnt = CInt(Dts.Variables("RowCount").Value)
    If rcnt = 0 Then
            msg = "Job returned 0 rows for month " & CStr(MonthName(lastMonth.Month, False)) & " - check with database operator that previous month's data has been loaded into the database."
     Else
            msg = "Job returned " & rcnt & " rows - Job Finished"
     End If
    'Pass message variable value out to be used in message 
    Dts.Variables("EmailMessage").Value = msg
    Dts.TaskResult = Dts.Results.Success
End Sub

如果我将 IF 语句修改为以下,以便能够根据系统月份值检查 SQL DB 中的月份值,它会给我一个 DTS 脚本错误说:

分配给变量“User::EmailMessage”的值的类型与当前变量类型不同。变量在执行期间可能不会改变类型。变量类型是严格的,除了 Object 类型的变量。

'First check month name from sql match system last month name
    If CStr(MonthName(lastMonth.Month, False)) = CStr(Dts.Variables("MonthNameFromSQL").Value) Then
        MsgBox("Month name variable equals last month value")
        If rcnt = 0 Then
            msg = "Job returned 0 rows for month " & CStr(MonthName(lastMonth.Month, False)) & " - check with outpatient database operator that previous month's data has been loaded into the database."
        Else
            msg = "Job returned " & rcnt & " rows - Job Finished"
        End If
    Else
    'Put in code to handle if month values do not match
    End If

我更换了:

If CStr(MonthName(lastMonth.Month, False)) = CStr(Dts.Variables("MonthNameFromSQL").Value) Then
        MsgBox("Month name variable equals last month value")

和:

If 1=1 Then
MsgBox("Month name variable equals last month value")

只是为了检查它是否可以使用简单的比较语句,并且它与我在 IF 语句中的代码有关,因为我不怀疑它与我正在填充的 msg 变量有任何关系,即使这是错误信息。

所以我意识到这与以下有关:

If CStr(MonthName(lastMonth.Month, False)) = CStr(Dts.Variables("MonthNameFromSQL").Value) Then

montNameFromSQL 填充了先前 SQL 任务的结果集,然后在此脚本中,我使用它来检查系统月份名称。我想检查这些值,如果匹配则成功并继续检查返回的行,并填充要在我的电子邮件任务中发送的消息。

希望这是一件简单的事情,一双新鲜的眼睛可以发现!

谢谢

安德鲁

4

2 回答 2

0

Either you have the wrong data types assigned (which is what the error message is stating) or you have not faithfully reproduced your code.

I created a package with 3 variables.

  • EmailMessage: String -
  • MonthNameFromSQL: String - July
  • RowCount: Int32 - 0

Control flow

I configured my "SCR Works fine" task to allow read access to the second two variables and read/write access to EmailMessage.

VB is icky

Inside the script, I used your code. I ran it with your original that works and the one that doesn't work for you.

Public Sub Main()
    Dim MonthFromSQL As String
    Dim lastMonth As New Date(DateTime.Today.Year, DateTime.Today.Month - 1, 1)
    Dim rcnt As Integer
    Dim msg As String

    'MsgBox("Month name from SQL is " & CStr(Dts.Variables("MonthNameFromSQL").Value))
    rcnt = CInt(Dts.Variables("RowCount").Value)
    'If rcnt = 0 Then
    '    msg = "Job returned 0 rows for month " & CStr(MonthName(lastMonth.Month, False)) & " - check with database operator that previous month's data has been loaded into the database."
    'Else
    '    msg = "Job returned " & rcnt & " rows - Job Finished"
    'End If

    'First check month name from sql match system last month name
    If CStr(MonthName(lastMonth.Month, False)) = CStr(Dts.Variables("MonthNameFromSQL").Value) Then
        MsgBox("Month name variable equals last month value")
        If rcnt = 0 Then
            msg = "Job returned 0 rows for month " & CStr(MonthName(lastMonth.Month, False)) & " - check with outpatient database operator that previous month's data has been loaded into the database."
        Else
            msg = "Job returned " & rcnt & " rows - Job Finished"
        End If
    Else
        'Put in code to handle if month values do not match
        msg = "I am here"
    End If

    'Pass message variable value out to be used in message 
    Dts.Variables("EmailMessage").Value = msg

    Dts.TaskResult = ScriptResults.Success
End Sub

As you can see from the value of the EmailMessage variable, it's populated and the script has completed.

enter image description here

This package is using SQL Server 2012 but the fundamentals of what you are trying has been available since 2005.

于 2013-08-14T02:45:37.127 回答
0

我看到的唯一问题是 MonthNameFromSQL 变量的数据类型,MonthNameFromSQL 以某种方式被分配了错误的数据类型或它得到空值。

我会建议您执行以下操作。

  1. 确保表中的 date_column 日期时间字段不是 NULLABLE 或在 sql 语句中进行空值检查,以确保它没有将任何空值传递给 MonthNameFromSQL 变量。

    select distinct ISNULL(convert(varchar(12),(datename(month, date_column))), '') as monthName FROM dbo.table1

  2. 从变量窗口中删除 MonthNameFromSQL 变量(显示在 Visual Studio 的左侧窗格中),再次重新创建此变量,确保变量的范围在包级别并且数据类型是字符串类型。从执行 sql 任务属性的结果集中选择此变量。

  3. 如果上述步骤失败,请重新创建包。

于 2013-08-14T05:38:23.263 回答