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

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
LVL 1
hidrauAsked:
Who is Participating?
 
Gustav BrockCIOCommented:
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
 
Kelvin SparksCommented:
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
 
hidrauAuthor Commented:
in access?
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
Kelvin SparksCommented:
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
 
bfuchsCommented:
(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
 
Kelvin SparksCommented:
Can you please confirm that SQL Server is the back end database - or advise if something else.
0
 
hidrauAuthor Commented:
Hello people,

I need to run the sql file in Access database.

Not in Microsoft SQL. Sorry for my lack of information
0
 
hidrauAuthor Commented:
Thanks a lot
It helped me very very much
0
 
Gustav BrockCIOCommented:
You are welcome!

/gustav
1
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.