Sql – VBScript on checking if the records exist

filesystemssqlsql-servervbscript

I have a table and a text file. Once the records in the table copied into textfile, the records will be deleted. But the table are still in used and will be inserted with a new records from time to time(by another program). I what to do checking on How to make sure that if there are no records in the table, the program will never copy into textfile.

Any solution, or references are very thankful. Thank you very much. Im testing in WSH and using MSSQL Server 2005.

'call functions
  call CopyFile()
  call tblDelete()
Sub tblDelete() Dim sql1 sql1 = "DELETE from tblOutbox" rs = conn.Execute(sql1) End Sub
Sub CopyFile 'set the sql command cmd.CommandText = "SELECT * FROM tblOutbox" cmd.CommandType = 1 ''# adCmdText Command text is a SQL query Dim rs : Set rs = cmd.Execute 'create obj for the FileSystem Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject") Dim objFile, objFolder
Dim strDir, strFile strDir = "c:\" strFile = "\newFile.txt" 'check that the strDirectory folder is exist If objFSO.FolderExists(strDir) Then Set objFolder = objFSO.GetFolder(strDir) Else Set objFolder = objFSO.CreateFolder(strDirectory) WScript.Echo "Just created " & strDir End If If objFSO.FileExists(strDir & strFile) Then Set objFolder = objFSO.GetFolder(strDir) Else Set objFile = objFSO.CreateTextFile(strDir & strFile) Wscript.Echo "Just created " & strDir & strFile End If Set objFile = Nothing Set objFolder = Nothing 'open files and copy into
Dim objtextStream : Set objtextStream = objFSO.OpenTextFile(strDir & strFile, 8, True) Do Until rs.EOF objtextStream.Write rs("id") & ", "
objtextStream.Write rs("ip") & ", " objtextStream.Write rs("msg") & ", " objtextStream.WriteLine rs("date")
rs.MoveNext Loop objTextStream.WriteLine objTextStream.WriteLine "Report Generate at " & Now objTextStream.WriteLine "--------------------------------------------" objtextStream.Close rs.Close
End Sub

Best Solution

You could put

If rs.RecordCount > 0 Then
   exit sub
End If

before

Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objFile, objFolder 

i.e. Don't execute any of the statements, if there are no records.