Solved

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

Posted on 2016-09-26
9
43 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 22

Expert Comment

by:Kelvin Sparks
ID: 41816538
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
ID: 41816543
in access?
0
 
LVL 22

Expert Comment

by:Kelvin Sparks
ID: 41816558
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
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
LVL 4

Expert Comment

by:bfuchs
ID: 41816890
(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
 
LVL 22

Expert Comment

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

Author Comment

by:hidrau
ID: 41816980
Hello people,

I need to run the sql file in Access database.

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

Accepted Solution

by:
Gustav Brock earned 500 total points
ID: 41817322
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
ID: 41826240
Thanks a lot
It helped me very very much
0
 
LVL 50

Expert Comment

by:Gustav Brock
ID: 41826246
You are welcome!

/gustav
1

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

733 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