运行 SQL 在 Stata 中

Running SQL in Stata

我正在尝试将数据从 SQL 服务器管理工​​作室加载到 Stata。如何让 Stata 进入 运行 .sql 文件?我使用了另一个 post 的 -ado- 过程,但它不起作用,因为我的数据库有用户名和密码。

原-ado-代码:

program define loadsql
*! Load the output of an SQL file into Stata, version 1.2 (dvmaster@gmail.com)
version 12.1
syntax using/, DSN(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'"') `clear' `noquote' `lowercase' `sqlshow' `allstring' `datestring';

end;

help odbc 讨论 connect_options 以连接到 odbc 数据源。其中两个是 u(userId)p(password),可以添加到@Dimitriy V. Masterov 编写的原始代码中(参见 post here)。

我相信您应该能够使用 SQL 服务器身份验证进行连接,方法是在 ado 文件中将 u(string)p(string) 作为附加选项添加到 syntax 之后,并且然后再次低于

 odbc load, exec(`"``exec''"') dsn(`"`dsn'"')

这还要求您在调用程序时将这些参数传递给程序:

loadsql using "./sqlfile.sql", dsn("mysqlodbcdata") u(userId) p(Password)