Solved

How can I open a Sql file and run all the script in it?

Posted on 2016-09-26
9
34 Views
Last Modified: 2016-10-03
Hello guys,

I'd like if it is possible to open a sql file with sql scripts on it and run all the code.

If so, how could I do this in access?

thanks
Alex
0
Comment
Question by:hidrau
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 22

Expert Comment

by:Kelvin Sparks
Comment Utility
Is it possible to put all of the script into a stored procedure. You could then execute the stored procedure via ADODB or even via a pass through query.

Either that or setup some form of batch job to execute it.


Kelvin
0
 
LVL 1

Author Comment

by:hidrau
Comment Utility
in access?
0
 
LVL 22

Expert Comment

by:Kelvin Sparks
Comment Utility
The sql file would have to be saved into sql server - or Oracle as stored procs..  (I assume that is what the sql file is for).

With sql server you can use a batch job to run a sql file.

I'm am not aware of ways to run .sql files against an Access database itself (if that is what your data is stored in).

You really need to give a little more detail of what your setup is - where the data is stored and so on.


Kelvin
0
 
LVL 3

Expert Comment

by:bfuchs
Comment Utility
(I assume that is what the sql file is for).
In that case I would request attention and ask moderator to add SQL Server or/and other databases as part of the topics.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 22

Expert Comment

by:Kelvin Sparks
Comment Utility
Can you please confirm that SQL Server is the back end database - or advise if something else.
0
 
LVL 1

Author Comment

by:hidrau
Comment Utility
Hello people,

I need to run the sql file in Access database.

Not in Microsoft SQL. Sorry for my lack of information
0
 
LVL 49

Accepted Solution

by:
Gustav Brock earned 500 total points
Comment Utility
Yes you can:
Public Function ReadSql()

    Dim dbs As Database
    Dim qdf As QueryDef
    
    Dim intFileDesc As Integer          'File descriptor for output file
    Dim strSourceFile As String         'Full path of source file
    Dim strTextLine As String           'Input buffer
    Dim strSQL As String

    Set dbs = CurrentDb
    
    strSourceFile = "c:\test\test.sql"
    
    intFileDesc = FreeFile
    Open strSourceFile For Input As #intFileDesc
    Do While Not EOF(intFileDesc)   ' Loop until end of file.
        Line Input #intFileDesc, strTextLine                  'Read line into buffer
        strSQL = strSQL & strTextLine & vbCrLf
    Loop
    Close #intFileDesc    'Close file.
    
    Debug.Print strSQL
    
    On Error Resume Next
    Set qdf = dbs.QueryDefs("tmpQdf")
    If Err.Number <> 0 Then
        Set qdf = dbs.CreateQueryDef("tmpQdf")
    End If
    qdf.Sql = strSQL
    
    DoCmd.OpenQuery (qdf.Name)
    
    Set qdf = Nothing
    Set dbs = Nothing     'Garbage handling before we exit the function

End Function

Open in new window

For an action query, just call the retrieved SQL:

    CurrentDb.Execute strSQL

/gustav
1
 
LVL 1

Author Closing Comment

by:hidrau
Comment Utility
Thanks a lot
It helped me very very much
0
 
LVL 49

Expert Comment

by:Gustav Brock
Comment Utility
You are welcome!

/gustav
1

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now