VB脚本用于读取文本文件和执行查询

时间:2012-04-10 08:45:11

标签: sql-server vbscript

我有一个格式为

的txt文件
11SNMMMMTESTCASEJBS1961123123232YExist 

从这个文件中我必须检查第33列的值,它将是Y或N. 如果是N,我必须转到数据库并使用以下查询

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

如果是Y

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

从文本文件中读取后,我尝试使用存储在变量中的值连接到SQLplus并尝​​试更新表但是我得到以下错误:“未终止的字符串常量 这是代码的样子,感谢Guido帮助我完成第1步。 任何人都可以指出错误。 If&中的一些错误其他部分,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."

1 个答案:

答案 0 :(得分:4)

看看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