sql-server - 用于读取文本文件并执行查询的 VB 脚本

标签 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 & 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."

最佳答案

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

关于sql-server - 用于读取文本文件并执行查询的 VB 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10085605/

相关文章:

c# - 带有 ROW_NUMBER 的 SQL 查询在 LINQ 中效率不高

sql - (SQL) 识别字符串格式在字段中多次出现的位置

windows - 通过 Windows Script Host 使用 UTF-8 编码运行 JScript 或 VBScript 文件时出现问题

vba - VB6/VBScript 将文件编码更改为 ansi

vbscript - 在 Windows 上使用 VBS 处理 MS Word

SQL查询将多行值归一化为一行一列字段

mysql - 选择与另一列的值匹配的列值

asp-classic - ASP 用户登录脚本

vbscript excel 创建工作表

sql-server - 如何使用 Azure SQL 数据库防止/处理 ErrBadConn