如何使用vbscript从servermanagementstudio中的特定数据库执行存储过程?

qaxu7uf2  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(348)

我的服务器管理工作室里有很多数据库。在其中一个数据库中,我使用了一个存储过程。我想执行它使用vbscript和存储结果使用记录集到一个文本文件。有什么可能的办法吗?请帮帮我。
这是我的密码:

Const DB_CONNECT_STRING = "Provider=SQLOLEDB.1;Data Source=CIBCWT12;Initial Catalog=dbFXCashCommerceManager;Trusted_Connection=yes;"

SET objFSO = CREATEOBJECT("Scripting.FileSystemObject")
CurrentDirectory = objFSO.GetParentFolderName(WSCript.ScriptFullName)
folder_name = CurrentDirectory & "\ModifReport\" & Replace(Date, "-", "_")

arr = split(folder_name, "\")
path = ""

For Each dir In arr
  If path <> "" Then path = path & "\"
  path = path & dir
  If objFSO.FolderExists(path) = False Then 
     objFSO.CreateFolder path
  End If
Next

'creating the file
set outFile = CreateObject("Scripting.FileSystemObject").CreateTextFile(folder_name & "\ModifReport" & "_" & Replace(Date, "-", "_") &".csv")

Set myConn = CreateObject("ADODB.Connection")
Set myRS = CreateObject("ADODB.recordSet" )
Set cmd = CreateObject("ADODB.Command" )

Set objFileToRead = CreateObject("Scripting.FileSystemObject").OpenTextFile(CurrentDirectory & "\listfile.txt",1)
SQLStr1 = objFileToRead.ReadAll()
objFileToRead.Close
Set objFileToRead = Nothing

myConn.Open DB_CONNECT_STRING

cmd.ActiveConnection = myConn
cmd.CommandText = SQLStr1
cmd.CommandType = 4
'cmd.Execute

'myRS.CursorType = 1
'myRS.Open SQLStr1, myConn, 0, 1, 4

Set myRS = myConn.Execute(SQLStr1,4)

outFile.Writeline "FXO_Warehouse_Code;TrackingNumber;1. Submitted;2. Processing;1-2 Hr;1-2 BHr;Processing Flag;3. Order Released;2-3 Hr;2-3 BHr;Released Flag;4. Order Ack by Branch;3-4 Hr;3-4 BHr;5. Order Completed;4-5 Hr;4-5 BHr;ExpectedDelivery;Order Returned to Intria;Transit;firstname;lastname;Currency Code;FGN Amount;Branch Flag;1-2BD;2-3BD;3-4BD;4-5BD;1-4BD;Brink Del Days;SLA Flag"
msgbox myRS.state 
msgbox myConn.state 

Do Until myRS.eof  
   outFile.Writeline myRS.Fields("FXO_Warehouse_Code") & ";" & myRS.Fields("TrackingNumber") & ";" & myRS.Fields("1. Submitted") & ";" & myRS.Fields("2. Processing") & ";" & myRS.Fields("1-2 Hr") & ";" & myRS.Fields("1-2 BHr") & ";" & myRS.Fields("Processing Flag") & ";" & myRS.Fields("3. Order Released") & ";" & myRS.Fields("2-3 Hr") & ";" & myRS.Fields("2-3 BHr") & ";" & myRS.Fields("Released Flag") & ";" & myRS.Fields("4. Order Ack by Branch") & ";" & myRS.Fields("3-4 Hr") & ";" & myRS.Fields("3-4 BHr") & ";" & myRS.Fields("5. Order Completed") & ";" & myRS.Fields("4-5 Hr") & ";" & myRS.Fields("4-5 BHr") & ";" & myRS.Fields("ExpectedDelivery") & ";" & myRS.Fields("Order Returned to Intria") & ";" & myRS.Fields("Transit") & ";" & myRS.Fields("firstname") & ";" & myRS.Fields("lastname") & ";" & myRS.Fields("Currency Code") & ";" & myRS.Fields("FGN Amount") & ";" & myRS.Fields("Branch Flag") & ";" & myRS.Fields("1-2BD") & ";" & myRS.Fields("2-3BD") & ";" & myRS.Fields("3-4BD") & ";" & myRS.Fields("4-5BD") & ";" & myRS.Fields("1-4BD") & ";" & myRS.Fields("Brink Del Days") & ";" & myRS.Fields("SLA Flag")
   myRS.MoveNext()
loop

myRS.Close
SET myRS = Nothing

myConn.Close
outFile.Close

msgbox "execution completed"

当我为一个普通的查询尝试上面的代码时,我得到了结果。但如果我试图将其用于存储过程,它将显示为“对象关闭时不允许操作”
这里我从一个文本文件中读取我的存储过程。
对于执行普通查询,我使用 myRS.Open SQLStr1, myConn, 0, 1, 4 执行我正在使用的sp Set myRS = myConn.Execute(SQLStr1,4) 我不明白为什么在执行存储过程时记录集会自动关闭。有人能帮我吗?

gdrx4gfi

gdrx4gfi1#

来自存储过程的额外返回消息可能会导致ado结果集由于错误而提前关闭。因此,当试图关闭结果集rs时,您会得到“对象关闭时不允许操作”错误。
在代码调用的存储过程中,使用set nocount on强制存储过程不返回受影响的行计数。使用setansi\u warnings off来消除存储过程错误导致vbscript崩溃的可能性也可能是明智的,也可能不是明智的。

相关问题