从vbscript中的DB2查询中检索日期字段时出现问题

时间:2014-03-31 13:15:09

标签: sql date vbscript db2

每当表的Date列具有默认值0001-01-01时,我收到以下错误。请帮我纠正这个问题。

网页错误详情

用户代理:Mozilla / 4.0(兼容; MSIE 8.0; Windows NT 5.1; Trident / 4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729 ) 时间戳:星期一,2014年3月31日13:09:51 UTC

消息:无效的过程调用或参数:' showList.fields(...)。值' 行:139 查尔:7 代码:0

dim fso,conn

sqlStr = "select * from "& tablename & " "& whereclause & ""

Set oFTPScriptFSO = CreateObject("Scripting.FileSystemObject")
Set oFTPScriptShell = CreateObject("WScript.Shell")
sFTPTemp = oFTPScriptShell.ExpandEnvironmentStrings("%TEMP%")
fpath1= sFTPTemp & tablename &".csv"

set fso = CreateObject("Scripting.FileSystemObject")

set Conn = CreateObject("ADODB.connection")
Conn.ConnectionTimeout = 60
Conn.CommandTimeout = 60

Conn.open "Provider=IBMDADB2;Hostname=1.1.1.1;Protocol=TCPIP;Port=50000;Database=" & dbalias &      ";Uid=" & Userid & " ;Pwd= "&  Pwd & ";"
dim a, showList, intcount
set a = fso.createtextfile(fPath1)
set showList = conn.execute(sqlStr)

if showlist.eof  then
   a.write "No Rows found"
else
   for intcount = 0 to showList.fields.count -1
   if intcount <> showList.fields.count-1 then
      a.write """" & showList.fields(intcount).name & ""","
   else
      a.write """" & showList.fields(intcount).name & """"
   end if
   next
   a.writeline ""
   do while not showList.eof
   for intcount = 0 to showList.fields.count - 1
   if intcount <> showList.fields.count - 1 then
      a.write """" & showList.fields(intcount).value & ""","
   else
      a.write """" & showList.fields(intcount).value & """"
   end if
   next
   a.writeline ""
   showList.movenext
  loop
end if
showList.close
set showList = nothing
set a = nothing

1 个答案:

答案 0 :(得分:2)

根据Docs,您的默认日期&#39;超出范围:

  

在Microsoft Windows中,有效日期的范围是公元100年1月1日。   截至9999年12月31日A.D。;范围因操作而异   系统

一个简单的测试:

>> WScript.Echo DateAdd("yyyy", -1900, Date)
>>
31.03.114
>> WScript.Echo DateAdd("yyyy", -2000, Date)
>>
Error Number:       5
Error Description:  Invalid procedure call or argument

支持在尝试将数据库值转换为VBScript日期时VBScript会产生阻塞的假设。

由于我怀疑您的真实世界问题涉及像0001-01-01这样的日期,因此正确的补救措施是在您的表中为未知日期存储NULL,并且 - 或许 - 将默认值设置为有意义的内容您的特定域属于VBScript的日期范围。

如果您必须在脚本中处理问题,那么您的选项是

  • 使用查询获取超出范围日期的附加标记,并在尝试访问日期值之前查看此标记
  • 更改查询以获取字符串化的日期值,写一个&#39; accessTheDates&#39;查看字符串值并返回有效日期或Null /有效特殊日期的函数
  • 使用 local 将小型函数中的日期访问隔离开On Error Resume Next返回有效日期或 - 如果出现错误 - Null /有效特殊日期