如何在VBScript中使用SQL获取超过24小时的数据?

时间:2017-04-02 01:36:26

标签: sql-server vbscript

我正在使用VBScript。我使用ADODB连接连接到SQL数据库。我在VBScript中编写了一个内联SQL查询。

要求是:获取过去24小时内的所有记录(systemtimestamp - 24小时)。查询正在按预期运行并在SQL Server中获取数据。但是如果我在VBScript文件中使用相同的查询,我没有得到记录,它返回null。

SELECT *
FROM customer 
WHERE CAST(InsertDate AS DATETIME) > = CAST(DATEADD(day, -1, GETDATE()) AS DATETIME)

VBScript代码:

Dim Connection
Dim Recordset
Dim SQL
Dim Server
Dim field
Dim Header
dt= Date
dt = Replace(dt,"/","-")
dt1= DateAdd("d", -1, Date)
dt1 = Replace(dt1,"/","-")
t=FormatDateTime(now, 4)
t1=FormatDateTime(now-24, 4)

Set objFSO = CreateObject("Scripting.FileSystemObject")

outFile="D:\"

if not objFSO.FileExists(outfile) then
Set objFile = objFSO.CreateTextFile(outFile,True)
else
Set objFile = objFSO.OpenTextFile(outFile,8,True)
End if

objFile.Write " "& vbCrLf & vbCrLf &"LOG Details on  "

objFile.Write(NOW) & vbCrLf & vbCrLf

SQL = "SELECT * FROM customer WHERE CAST(InsertDate AS DATETIME) > = CAST(DATEADD(day, -1, GETDATE()) AS DATETIME)"
'create an instance of the ADO connection and recordset objects
Connection.Open "Provider=SQLOLEDB; Data Source=DS;UID=id;PWD=pwd;Database=db"
Set Connection = CreateObject("ADODB.Connection")
Set Recordset = CreateObject("ADODB.Recordset")
objFile.Write "1. Connected to the Database" & vbCrLf & vbCrLf
'Open the recordset object executing the SQL statement and return records
Recordset.Open SQL, Connection
'first of all determine whether there are any records
    If Recordset.EOF Then
    objFile.Write "2. No Records Retrieved From the Database" & vbCrLf & vbCrLf
    mail()

'if there are records then loop through the fields
    Else
    objFile.Write "2. Records Retrieved From the Database" & vbCrLf & vbCrLf
    'Send the Email with data
    sendMail()
    End If
'close the connection and recordset objects to free up resources

Recordset.Close
Set Recordset=nothing
Connection.Close
Set Connection=nothing

0 个答案:

没有答案