【问题标题】:VB Scripting for reading a text file and executing query用于读取文本文件和执行查询的 VB 脚本
【发布时间】:2012-04-12 17:57:38
【问题描述】:

我有一个如下格式的txt文件

11SNMMMMTESTCASEJBS1961123123232YExist 

从这个文件中,我必须检查第 33 列的值,即 Y 或 N。 如果是N,我必须去数据库并使用下面的查询

Update table XYZ set Status = Not Exist where cust_id = ** ( taken from record)

如果是的话

Update table XYZ set Status = Exist where cust_id = ** ( taken from record)

从文本文件读取后,我尝试使用存储在变量中的值连接到 SQLplus,并尝试更新表,但出现以下错误:“未终止的字符串常量 这是代码的样子,感谢 Guido 在步骤 1 中帮助我。 谁能指出错误。 If & Else Part 中的一些错误,SQL 查询或连接错误

dim fs, txt, line, yesno , cust_id
set fs = CreateObject("Scripting.FileSystemObject")
set txt = fs.OpenTextFile("E:\batchfiletest\Eapp3\scotia1.txt", 1, false) 

' loop through all the lines
do while not txt.AtEndOfStream
    line = txt.readLine

' read the character and store it in a variable
    yesno = Mid(line, 127, 1)
    cust_id = Mid(line, 1,20)   

' execute the correct query
    if yesno = "Y" then

    set WshShell = CreateObject("WScript.Shell")
    set oEnv=WshShell.Environment("Process") 
    cmdString = "E:\oracle\product\10.2.0\db_1\BIN\sqlplusw.exe -S sysman/csaadmin@convcsd

    UPDATE csa_sli_all.T_CONV_quote set HOLD_CODE = 'CAQ' where quote_id =  cust_id ;
    commit;"

    Set oExec = WshShell.Exec(cmdString)

     ELSE  
    set WshShell = CreateObject("WScript.Shell")
    set oEnv=WshShell.Environment("Process") 
    cmdString = "E:\oracle\product\10.2.0\db_1\BIN\sqlplusw.exe -S sysman/csaadmin@convcsd

    UPDATE csa_sli_all.T_CONV_quote set HOLD_CODE = 'PVQ' where quote_id =  cust_id ;
    commit;"

    Set oExec = WshShell.Exec(cmdString)

    end if
loop
MsgBox "Press OK to close when done reading the output."

【问题讨论】:

  • 您似乎在要求 SO 为您编写代码?
  • 指针可以帮助我,代码更好:)
  • 您是否查看了我在下面的答案中包含的链接?这是非常基本的代码。让我知道这是否适合你,否则我可以给你写一个例子。

标签: sql-server vbscript


【解决方案1】:

看看 vbscript String 函数。专门寻找Mid 函数。

然后您应该能够读取特定的字符位置,并执行正确的查询。

从文本文件中读取行、存储该行中的字符并在以后使用它的示例:

dim fs, txt, line, yesno

' create a file system object and open the text file
set fs = CreateObject("Scripting.FileSystemObject")
set txt = fs.OpenTextFile("C:\Users\dgaurav\Deskto‌​p\CSA -3\scotia.txt", 1, false) 

' loop through all the lines
do while not txt.AtEndOfStream
    line = txt.readLine

    ' read the character and store it in a variable
    yesno = Mid(line, 33, 1)

    ' execute the correct query
    if yesno = "Y" then
        WScript.Echo "Yes"
    else
        WScript.Echo "No"
    end if
loop

【讨论】:

  • 我已经更新了我的答案,以包含您所要求的示例。
  • 这是我的代码 Set objFileToRead = CreateObject("Scripting.FileSystemObject").OpenTextFile("C:\Users\dgaurav\Desktop\CSA -3\scotia.txt",1) Dim strLine do虽然不是 objFileToRead.AtEndOfStream strLine = objFileToRead.ReadLine() dim yesno : yesno = write(Mid(txt,33,1)) if yesno = "Y" then WScript.Echo "Yes" else WScript.Echo "No" end if loop objFileToRead.Close Set objFileToRead = Nothing 此代码未运行,出现以下错误:type mismatch 'write'
  • 像宝石一样工作,我现在也获取了 Cust_ID(前 2 个),现在将尝试在查询中使用它们,看看是否有效。感谢您的帮助 Guido .. 你摇滚 :)
  • 很高兴我能帮上忙。 :)。如果您支持我的回答并标记它,我们做得很好,先生。
  • 至少需要 15 声望才能增加该计数器 :( 。一旦我拥有它就会这样做。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-07-30
  • 1970-01-01
  • 1970-01-01
  • 2016-12-08
  • 1970-01-01
  • 2021-12-30
  • 2018-01-20
相关资源
最近更新 更多