我在脚本任务中有这段代码:
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 任务的结果集,然后在此脚本中,我使用它来检查系统月份名称。我想检查这些值,如果匹配则成功并继续检查返回的行,并填充要在我的电子邮件任务中发送的消息。
希望这是一件简单的事情,一双新鲜的眼睛可以发现!
谢谢
安德鲁