Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8492357
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:41:42+00:00 2026-06-10T22:41:42+00:00

I am trying to load data from database (either MS Access or SQL server)

  • 0

I am trying to load data from database (either MS Access or SQL server) using odbc sqlfile it seems that the code is running with any error but I am not getting data. I am using the following code odbc sqlfile("sqlcode.sql"),dsn("mysqlodbcdata"). Note that sqlcode.sql contains just sql statement with SELECT. The thing is that the same sql code is giving data with odbc load,exec(sqlstmt) dsn("mysqlodbcdata"). Can anyone suggest how can I use odbc sqlfile to import data? This would be a great help for me.

Thanks
Joy

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T22:41:44+00:00Added an answer on June 10, 2026 at 10:41 pm

    sqlfile doesn’t load any data. It just executes (and displays the results when the loud option is specified), without loading any data into Stata. That’s somewhat counter-intuitive, but true. The reasons are somewhat opaquely explained in the pdf/dead tree manual entry for the odbc command.

    Here’s a more helpful answer. Suppose you have your SQL file named sqlcode.sql. You can open it in Stata (as long as it’s not too long, where too long depends on your flavor of Stata). Basically, -file read- reads the SQL code line by line, storing the results in a local macro named exec. Then you pass that macro as an argument to the -odbc load- command:

    Updated Code To Deal With Some Double Quotes Issues

    Cut & paste the following code into a file called loadsql.ado, which you should put in directory where Stata can see it (like ~/ado/personal). You can find such directories with the -adopath- command.

    program define loadsql
    *! Load the output of an SQL file into Stata, version 1.3 (dvmaster@gmail.com)
    version 14.1
    syntax using/, DSN(string) [User(string) Password(string) CLEAR NOQuote LOWercase SQLshow ALLSTRing DATESTRing]
    
    #delimit;
    tempname mysqlfile exec line;
    
    file open `mysqlfile' using `"`using'"', read text;
    file read `mysqlfile' `line';
    
    while r(eof)==0 {;
        local `exec' `"``exec'' ``line''"';
        file read `mysqlfile' `line';
    };
    
    file close `mysqlfile';
    
    
    odbc load, exec(`"``exec''"') dsn(`"`dsn'"') user(`"`user'"') password(`"`password'"') `clear' `noquote' `lowercase' `sqlshow' `allstring' `datestring';
    
    end;
    

    /* All done! */

    The syntax in Stata is

    loadsql using "./sqlfile.sql", dsn("mysqlodbcdata") 
    

    You can also add all the other odbc load options, such as clear, as well. Obviously, you will need to change the file path and the odbc parameters to reflect your setup. This code should do the same thing as -odbc sqlfile(“sqlfile.sql”), dsn(“mysqlodbcdata”)- plus actually load the data.

    I also added the functionality to specify your DB credentials like this:

    loadsql using "./sqlfile.sql", dsn("mysqlodbcdata") user("user_name") password("not12345") 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to load data (4 columns from 1 table) from an SQL database
I am trying to load some data into R from an Access database. All
I am trying to load mysql data from my database from a html select
I am trying to load some XML data into C++ code (classes) using gsoap.
I'm trying to load data from database and display it in table like here:
I'm trying to load data from a CSV file into a MySQL database, and
I am trying to load database data by using dbplannercalender1. procedure TForm1.DBPlannerCalendar1DaySelect(Sender: TObject; SelDate:
Trying to use 'LOAD DATA INFILE' in order to populate a database table from
I'm trying to create a chart using data from my database calling json, but
Hello I'm trying to load my listview with data from a database via a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.