Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: SQL problem: ID already defined

From   Friedrich Huebler <>
Subject   st: SQL problem: ID already defined
Date   Mon, 6 Jun 2011 16:07:37 -0400

I am trying to load some data from an ODBC source into Stata with
-odbc load, exec()-. A command that works in SQL Server Management
Studio fails in Stata when the two variables that are used to match
observations have the same name in both tables. The problem can be
reproduced with the Northwind database that is usually installed with
MS Access.

. odbc query "Northwind"
. odbc load, exec("SELECT * FROM [Order Details] as a INNER JOIN
[Orders] as b ON a.OrderID = b.OrderID")
OrderID already defined

My solution is to load the two tables separately, save them as Stata
datasets, and then merge them over the common identifier. Instead of
the commands below, is it possible to join the tables directly with a
variation of the SQL command shown above?

. odbc load, exec("SELECT * FROM [Order Details]")
. sort OrderID
. tempfile details
. save `details'
. clear
. odbc load, exec("SELECT * FROM [Orders]")
. sort OrderID
. merge 1:m OrderID using `details'


*   For searches and help try:

© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index