VBScript 检查记录是否存在

发布于 2024-08-05 02:22:38 字数 1944 浏览 3 评论 0原文

我有一个表格和一个文本文件。一旦表中的记录复制到文本文件中,记录将被删除。但该表仍在使用中,并且会不时插入新记录(由另一个程序)。我该怎么做检查如何确保如果表中没有记录,程序将永远不会复制到文本文件中。

任何解决方案或参考都非常感谢。非常感谢。我正在 WSH 中进行测试并使用 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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

骄傲 2024-08-12 02:22:39

您可以放在

If rs.RecordCount > 0 Then
   exit sub
End If

前面

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

,即如果没有记录,则不要执行任何语句。

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.

北斗星光 2024-08-12 02:22:39

您能否以如下格式设置代码,在这种格式中,延迟打开输出文件,直到触发查询并检索到至少一个响应之后:

Set up SQL statement 
Execute SQL query 
init bFirstRecord as true 
Loop over results   
  if bFirstRecord
   check folder and file existence, create as necessary
   open output file
   bFirstRecord = false   
  end if   
  write record to output 
End Loop
Close up files, etc

Can you set your code up in a format such as the following, in which you delay opening the output file until after you have fired your query and retrieved at least one response:

Set up SQL statement 
Execute SQL query 
init bFirstRecord as true 
Loop over results   
  if bFirstRecord
   check folder and file existence, create as necessary
   open output file
   bFirstRecord = false   
  end if   
  write record to output 
End Loop
Close up files, etc
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文