[英]Execute sql script statement output order is not follow the script statement order
[英]VBS Script to Execute SQL statement?
要打开,我从来没有写过vbs脚本。 我编写了许多 SQL 脚本、视图、开发的数据库。 我在 Access 应用程序中编写了大量 VBA。
为此,我只是尝试将 SQL 脚本设置为 VBS 脚本,这样用户就不必进入 SSMS 来运行它。 他们只需双击 VBS 脚本,在出现提示时指定服务器和数据库,就会为他们运行快速脚本。
这是我到目前为止所得到的,但我不断收到 Microsoft VBScript 编译错误。 最新的是第 3 行字符 17,它位于 Dim 语句中。 只是想看看是否有人能告诉我我是否遗漏了这个脚本的基本内容,这会阻止它正确编译或处理。
这是一个非常简短的脚本:
Dim conn
Set conn = createobject("Adodb.Connection")
Dim sConnString As String
Dim SqlStatement As String
sSourceServer = InputBox ("Enter the name of the SQL Server","Enter SQL Server Name","")
If Len(sSourceServer) = 0 Then
MsgBox "No SQL Server was specified.", , "Unable to Continue"
Exit Sub
End if
sSourceDB = InputBox ("Enter the name of the Law SQL Database","Enter Law SQL DB Name","")
If Len(sSourceDB) = 0 Then
MsgBox "No SQL DB was specified.", , "Unable to Continue"
Exit Sub
End if
' Create the connection string.
sConnString = "Provider=SQLOLEDB;Data Source=" & sSourceServer & "; Initial Catalog=" & sSourceDB & "; Integrated Security=SSPI;"
MsgBox sConnString
' Open the connection and execute.
conn.Open sConnString
conn.CommandTimeout = 900
SqlStatement = "UPDATE [tablename] " & _
"SET UUID = CASE WHEN CHARINDEX('.',[Filename]) > 1 THEN LEFT(CAST([Filename] AS VARCHAR),CHARINDEX('.',[Filename])-1) ELSE [Filename] END " & _
"WHERE [Filename] IS NOT NULL"
conn.Execute(SqlStatement)
conn.Close
Set rs = Nothing
SqlStatement = vbNullString
MsgBox "All Done! Go Check your results!"
如果有人可以提供帮助,我将不胜感激。
谢谢
没关系。 我一直在进行故障排除,最终让它工作起来。 对于那些可能有帮助的人,与 VBA 不同,不将变量声明为类型更容易。 只是调暗它们并继续前进。 见下文:
Dim conn
Set conn = createobject("Adodb.Connection")
Dim sConnString
Dim SqlStatement
StartScript
Sub StartScript()
sSourceServer = InputBox ("Enter the name of the SQL Server","Enter SQL Server Name","")
If Len(sSourceServer) = 0 Then
MsgBox "No SQL Server was specified.", , "Unable to Continue"
Exit Sub
End if
sSourceDB = InputBox ("Enter the name of the Law SQL Database","Enter Law SQL DB Name","")
If Len(sSourceDB) = 0 Then
MsgBox "No SQL DB was specified.", , "Unable to Continue"
Exit Sub
End if
' Create the connection string.
sConnString = "Provider=SQLOLEDB;Data Source=" & sSourceServer & "; Initial Catalog=" & sSourceDB & "; Integrated Security=SSPI;"
' Open the connection and execute.
conn.Open sConnString
conn.CommandTimeout = 900
SqlStatement = "UPDATE [tablename] " & _
"SET UUID = CASE WHEN CHARINDEX('.',[Filename]) > 1 THEN LEFT(CAST([Filename] AS VARCHAR),CHARINDEX('.',[Filename])-1) ELSE [Filename] END " & _
"WHERE [Filename] IS NOT NULL"
conn.Execute(SqlStatement)
conn.Close
Set rs = Nothing
SqlStatement = vbNullString
End Sub
MsgBox "All Done! Go Check your results!"
请记住,对于那些希望将其用作脚本基础的人 - 我不会进行任何检查,因此如果您不知道自己的数据,那么运行这是一件危险的事情。
了解您的数据,备份您的数据,如果可以的话,添加一些检查,以确保在运行之前检查和重新检查不是选择语句的任何内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.