2012-04-10 85 views
2

我有一個txt文件在以下格式VB腳本讀取的文本文件和執行查詢

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) 

後readi ng從一個文本文件,我想連接到SQLplus與存儲在一個變量的值,並試圖更新表,但我得到以下錯誤:「未終止的字符串常量 這是代碼的樣子,感謝Guido的幫助我出了第1步。 任何人都可以請指出錯誤。某些錯誤裏面如果& else部分,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/[email protected] 

    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/[email protected] 

    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." 
+2

你似乎會問SO寫你的代碼嗎? – 2012-04-10 08:48:52

+0

指針可以幫助我,代碼更好:) – 2012-04-10 09:54:43

+0

你看過我在下面的答案中包含的鏈接嗎?這是非常基本的代碼。讓我知道,如果這對你有用,否則我可以寫你一個例子。 – 2012-04-10 10:05:30

回答

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 
+0

我已經更新了我的答案,以包含您要求的示例。 – 2012-04-10 10:38:08

+1

這是我的代碼 集objFileToRead =的CreateObject( 「Scripting.FileSystemObject的」)的OpenTextFile( 「C:\用戶\ dgaurav \桌面\ CSA -3 \ scotia.txt」,1)。 昏暗strLine中 做雖不objFileToRead .AtEndOfStream strLine中= objFileToRead.ReadLine() 昏暗YESNO:YESNO =寫(MID(TXT,33,1)) 如果YESNO = 「Y」,然後 WScript.Echo 「是」 其他 WScript.Echo 「No」 end if loop objFileToRead.Close Set objFileToRead = Nothing 此代碼未運行,出現以下錯誤:鍵入不匹配'寫' – 2012-04-10 10:49:08

+0

我已更新我的答案。你幾乎在那裏:)。 – 2012-04-10 10:55:25