473,385 Members | 1,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Execute SQL Script

MS-SQL Server utilities Enterprise Manager and Query Analyzer will model
almost any MS-SQL object as a simple script file with a default ".sql"
extension.

But how to "run these files? They are in a slightly modifies character set
and contain commands only usable by the Server's SQL utilities. If one
tries to use them on a machine which does not have the SQL utilities
installed, one seems to have a laborious cut-and-paste task.

(Of course, if Access has a simple and better way, I hope you will let me
know.)

I have written a tiny bit of utility code that
allows one to select such an sql file,
decodes and parses the script
and allows one to Execute or not Execute its various parts.

You can find it at
http://ffdba.com/downloads

but I think it is small enough to post here:

'Don't mess with this unless you are experienced and capable.
'Back up your database before using.

Option Explicit
Option Base 0

Private Declare Function CommDlgExtendedError Lib "comdlg32.dll" () As Long

Private Declare Function GetOpenFileName Lib "comdlg32.dll" _
Alias "GetOpenFileNameA" _
(pOpenfilename As OPENFILENAME) As Long

Private Type OPENFILENAME
lStructSize As Long
hwndOwner As Long
hInstance As Long
lpstrFilter As String
lpstrCustomFilter As String
nMaxCustFilter As Long
nFilterIndex As Long
lpstrFile As String
nMaxFile As Long
lpstrFileTitle As String
nMaxFileTitle As Long
lpstrInitialDir As String
lpstrTitle As String
flags As Long
nFileOffset As Integer
nFileExtension As Integer
lpstrDefExt As String
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type

Private Const OFN_HIDEREADONLY = &H4
Private Const OFN_NOCHANGEDIR = &H8
Private Const OFN_SHAREAWARE = &H4000
Private Const MAX_PATH = 260
Private Const DELIMITER = vbNewLine & "GO" & vbNewLine

Private Sub FindAndExecuteSQLScript()
Dim Script As String
Dim FullPathToFile As String
FindSQLScript Script, FullPathToFile
Debug.Print Script
If MsgBox( _
"Execute " _
& vbNewLine _
& vbNewLine _
& FullPathToFile & "?" _
& vbNewLine _
& vbNewLine _
& "Are you SURE?" _
& vbNewLine _
& vbNewLine _
& "If you're not sure, choose No," _
& vbNewLine _
& "and examine the immediate window" _
& vbNewLine _
& "where the Script has been copied.", _
vbQuestion Or vbYesNo, _
"This Procedure Can Cause Significant Damage To Your
Database.") _
= vbYes Then
ExecuteSQLScript Script
End If
End Sub

Private Sub ExecuteSQLScript( _
ByVal Script As String)
Dim aSubScripts() As String
Dim SubScript As String
Dim z As Long
aSubScripts = Split(Script, DELIMITER)
For z = 0 To UBound(aSubScripts)
aSubScripts(z) = Trim$(aSubScripts(z))
SubScript = aSubScripts(z)
If Len(SubScript) > 255 Then
SubScript = Left$(aSubScripts(z), InStr(255, aSubScripts(z),
vbNewLine))
End If
If Len(Replace(SubScript, vbNewLine, "")) > 1 Then
If Left$(SubScript, 3) <> "SET" Then
If MsgBox("EXECUTE " & vbNewLine & SubScript, vbQuestion Or
vbYesNo, "FFDBA") = vbYes Then
CurrentProject.Connection.Execute aSubScripts(z)
End If
End If
End If
Next z
End Sub

Private Sub FindSQLScript( _
ByRef Script As String, _
ByRef FullPathToFile As String, _
Optional ByVal Owner As String, _
Optional ByVal ChatPartner As String)

Dim EntireMessage As String
Dim FileNumber As Integer

' get full path to file
If Len(FullPathToFile) = 0 Then
FullPathToFile = GetFile()
End If

' get entire message
FileNumber = FreeFile()
Script = String(FileLen(FullPathToFile), vbNullChar)
Open FullPathToFile For Binary As #FileNumber
Get #FileNumber, , Script
Close #FileNumber

Script = Mid$(StrConv(Script, vbFromUnicode), 2)

FindSQLScriptExit:
Close
Exit Sub
FindSQLScriptErr:
With Err
MsgBox .Description, , "Error " & .Number
End With
Resume FindSQLScriptExit
End Sub

Private Function GetFile(Optional InitialDir As String, _
Optional FilterMessage As String = "MS-SQL Scrips", _
Optional FilterSkelton As String = "*.sql", _
Optional File As String = "*.sql", _
Optional Title As String = "Use the Open Button to Select") As String
GetFile = GetPath(InitialDir, FilterMessage, FilterSkelton, File,
Title)
End Function

Private Function GetPath( _
Optional InitialDir As String, _
Optional FilterMessage As String = "Choose Folder Only", _
Optional FilterSkelton As String = "*|*", _
Optional File As String = "Folders Only", _
Optional Title As String = "Use the Open Button to Select") As String
Dim CommDlgError As Long
Dim OFN As OPENFILENAME
If Len(InitialDir) = 0 Then InitialDir = CurDir$()
With OFN
.lStructSize = Len(OFN)
.lpstrFilter = FilterMessage & vbNullChar & FilterSkelton & String
(2, vbNullChar)
.lpstrFile = File & String(MAX_PATH - Len(File), vbNullChar)
.nMaxFile = MAX_PATH
.lpstrInitialDir = InitialDir & vbNullChar
.lpstrTitle = Title
.flags = OFN_HIDEREADONLY Or OFN_NOCHANGEDIR Or OFN_SHAREAWARE
If GetOpenFileName(OFN) <> 0 Then
If FilterSkelton = "*|*" Then
GetPath = Left$(.lpstrFile, .nFileOffset)
Else
GetPath = .lpstrFile
End If
GetPath = Left$(GetPath, InStr(GetPath, vbNullChar) - 1)
Else
CommDlgError = CommDlgExtendedError
' if not just a cancel
If CommDlgError <> 0 Then
MsgBox "Common Dialog Error # " & CommDlgError _
& vbCrLf _
& vbCrLf _
& "Consult Common Dialog Documumentation" _
& vbCrLf _
& "(in MSDN Library)" _
& vbCrLf _
& vbCrLf _
& "for meaning.", _
vbCritical, _
"FFDBA"
End If
End If
End With
End Function
--
Lyle
(for e-mail refer to http://ffdba.com/contacts.htm)
Nov 12 '05 #1
3 9773
"Lyle Fairfield" <Mi************@Invalid.Com> wrote in message
news:Xn*******************@130.133.1.4...
MS-SQL Server utilities Enterprise Manager and Query Analyzer will model
almost any MS-SQL object as a simple script file with a default ".sql"
extension.

But how to "run these files? They are in a slightly modifies character set
and contain commands only usable by the Server's SQL utilities. If one
tries to use them on a machine which does not have the SQL utilities
installed, one seems to have a laborious cut-and-paste task.

(Of course, if Access has a simple and better way, I hope you will let me
know.)


Not that I know of! We wrote a utility that does similar - but it can be
used to execute a bunch of scripts one after the other.

http://www.assaynet.com/downloads/Access/UpdateSQL.mdb if anyone wants a
copy.
We use it for updating our clients databases. AFAIK there are also some
commercial ones available that do similar.
Nov 12 '05 #2
Check out osql.exe - it ships with SQL Server.
-Tom.

"Lyle Fairfield" <Mi************@Invalid.Com> wrote in message
news:Xn*******************@130.133.1.4...
MS-SQL Server utilities Enterprise Manager and Query Analyzer will model
almost any MS-SQL object as a simple script file with a default ".sql"
extension.

But how to "run these files? They are in a slightly modifies character set
and contain commands only usable by the Server's SQL utilities. If one
tries to use them on a machine which does not have the SQL utilities
installed, one seems to have a laborious cut-and-paste task.

(Of course, if Access has a simple and better way, I hope you will let me
know.)

I have written a tiny bit of utility code that
allows one to select such an sql file,
decodes and parses the script
and allows one to Execute or not Execute its various parts.

You can find it at
http://ffdba.com/downloads

but I think it is small enough to post here:

'Don't mess with this unless you are experienced and capable.
'Back up your database before using.

Option Explicit
Option Base 0

Private Declare Function CommDlgExtendedError Lib "comdlg32.dll" () As Long
Private Declare Function GetOpenFileName Lib "comdlg32.dll" _
Alias "GetOpenFileNameA" _
(pOpenfilename As OPENFILENAME) As Long

Private Type OPENFILENAME
lStructSize As Long
hwndOwner As Long
hInstance As Long
lpstrFilter As String
lpstrCustomFilter As String
nMaxCustFilter As Long
nFilterIndex As Long
lpstrFile As String
nMaxFile As Long
lpstrFileTitle As String
nMaxFileTitle As Long
lpstrInitialDir As String
lpstrTitle As String
flags As Long
nFileOffset As Integer
nFileExtension As Integer
lpstrDefExt As String
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type

Private Const OFN_HIDEREADONLY = &H4
Private Const OFN_NOCHANGEDIR = &H8
Private Const OFN_SHAREAWARE = &H4000
Private Const MAX_PATH = 260
Private Const DELIMITER = vbNewLine & "GO" & vbNewLine

Private Sub FindAndExecuteSQLScript()
Dim Script As String
Dim FullPathToFile As String
FindSQLScript Script, FullPathToFile
Debug.Print Script
If MsgBox( _
"Execute " _
& vbNewLine _
& vbNewLine _
& FullPathToFile & "?" _
& vbNewLine _
& vbNewLine _
& "Are you SURE?" _
& vbNewLine _
& vbNewLine _
& "If you're not sure, choose No," _
& vbNewLine _
& "and examine the immediate window" _
& vbNewLine _
& "where the Script has been copied.", _
vbQuestion Or vbYesNo, _
"This Procedure Can Cause Significant Damage To Your
Database.") _
= vbYes Then
ExecuteSQLScript Script
End If
End Sub

Private Sub ExecuteSQLScript( _
ByVal Script As String)
Dim aSubScripts() As String
Dim SubScript As String
Dim z As Long
aSubScripts = Split(Script, DELIMITER)
For z = 0 To UBound(aSubScripts)
aSubScripts(z) = Trim$(aSubScripts(z))
SubScript = aSubScripts(z)
If Len(SubScript) > 255 Then
SubScript = Left$(aSubScripts(z), InStr(255, aSubScripts(z),
vbNewLine))
End If
If Len(Replace(SubScript, vbNewLine, "")) > 1 Then
If Left$(SubScript, 3) <> "SET" Then
If MsgBox("EXECUTE " & vbNewLine & SubScript, vbQuestion Or vbYesNo, "FFDBA") = vbYes Then
CurrentProject.Connection.Execute aSubScripts(z)
End If
End If
End If
Next z
End Sub

Private Sub FindSQLScript( _
ByRef Script As String, _
ByRef FullPathToFile As String, _
Optional ByVal Owner As String, _
Optional ByVal ChatPartner As String)

Dim EntireMessage As String
Dim FileNumber As Integer

' get full path to file
If Len(FullPathToFile) = 0 Then
FullPathToFile = GetFile()
End If

' get entire message
FileNumber = FreeFile()
Script = String(FileLen(FullPathToFile), vbNullChar)
Open FullPathToFile For Binary As #FileNumber
Get #FileNumber, , Script
Close #FileNumber

Script = Mid$(StrConv(Script, vbFromUnicode), 2)

FindSQLScriptExit:
Close
Exit Sub
FindSQLScriptErr:
With Err
MsgBox .Description, , "Error " & .Number
End With
Resume FindSQLScriptExit
End Sub

Private Function GetFile(Optional InitialDir As String, _
Optional FilterMessage As String = "MS-SQL Scrips", _
Optional FilterSkelton As String = "*.sql", _
Optional File As String = "*.sql", _
Optional Title As String = "Use the Open Button to Select") As String
GetFile = GetPath(InitialDir, FilterMessage, FilterSkelton, File,
Title)
End Function

Private Function GetPath( _
Optional InitialDir As String, _
Optional FilterMessage As String = "Choose Folder Only", _
Optional FilterSkelton As String = "*|*", _
Optional File As String = "Folders Only", _
Optional Title As String = "Use the Open Button to Select") As String
Dim CommDlgError As Long
Dim OFN As OPENFILENAME
If Len(InitialDir) = 0 Then InitialDir = CurDir$()
With OFN
.lStructSize = Len(OFN)
.lpstrFilter = FilterMessage & vbNullChar & FilterSkelton & String
(2, vbNullChar)
.lpstrFile = File & String(MAX_PATH - Len(File), vbNullChar)
.nMaxFile = MAX_PATH
.lpstrInitialDir = InitialDir & vbNullChar
.lpstrTitle = Title
.flags = OFN_HIDEREADONLY Or OFN_NOCHANGEDIR Or OFN_SHAREAWARE
If GetOpenFileName(OFN) <> 0 Then
If FilterSkelton = "*|*" Then
GetPath = Left$(.lpstrFile, .nFileOffset)
Else
GetPath = .lpstrFile
End If
GetPath = Left$(GetPath, InStr(GetPath, vbNullChar) - 1)
Else
CommDlgError = CommDlgExtendedError
' if not just a cancel
If CommDlgError <> 0 Then
MsgBox "Common Dialog Error # " & CommDlgError _
& vbCrLf _
& vbCrLf _
& "Consult Common Dialog Documumentation" _
& vbCrLf _
& "(in MSDN Library)" _
& vbCrLf _
& vbCrLf _
& "for meaning.", _
vbCritical, _
"FFDBA"
End If
End If
End With
End Function
--
Lyle
(for e-mail refer to http://ffdba.com/contacts.htm)

Nov 12 '05 #3
Thanks, Lyle. Although I haven't run this yet, it is impressive.


Nov 12 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: myang | last post by:
After we compile a C program, we can put the executable file under a directory (like /bin) so that we can run it everywhere, and don't have to type the full path. Can we do the same thing to a...
1
by: Anzu | last post by:
I've been working on a script that finds and updates a record in database based on the user's input. When I try it I get this error: Active Server Pages, ASP 0113 (0x80004005) The maximum...
4
by: werdna.sivad | last post by:
In PHP I need to be able to execute another script from inside another and catch the output. How do I do this? The require() and include() functions will execute a script, but it sends the output...
0
by: Luis Esteban Valencia Muñoz | last post by:
I have a big project I did on the developing company I am working, it has the connection string on the web.config. and It works perfectly in our testing environment. I want to make a setup...
0
by: oliver | last post by:
QUESTION: How to access an object embedded in a UserControl through Javascript file? Another way to ask this question: How to execute script from a UserControl, accessing other objects in that...
4
by: cupa | last post by:
Hi, is it posible to execute script from file within function (sql or pl/sql or ...)
4
by: brad | last post by:
When I use idle or a shell to execute a python script, the script executes in the directory it is currently in (in this case, my desktop). However, when using GNOME and right clicking the py script...
1
shrek123
by: shrek123 | last post by:
Hi, I am in a big mess. I have to execute command on remote machine using perl script. I have used INET Socket module to do this operation. Following is the code snipet. use IO::Socket;
2
by: heminfotech | last post by:
Hi I would like to update the status of users of my site. I am not getting any clue as to how execute a script on close event of the browser. I have tried one thing that is I have used to...
2
by: thallasridevi | last post by:
i want to execute a script for every 10 seconds . and the loop is 10 minutes long. I written the code like this but i got timeout warning. i have changed the execution time too in .ini file. please...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.