【问题标题】:Running SQL in Stata在 Stata 中运行 SQL
【发布时间】:2015-07-30 14:09:55
【问题描述】:

我正在尝试将数据从 SQL Server Management Studio 加载到 Stata。如何让 Stata 运行 .sql 文件?我使用了另一篇文章中的 -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;

【问题讨论】:

  • 您可以将密码存储为该 DSN 的 ODBC 设置的一部分。
  • 感谢您的原始发帖!不幸的是,Stata 要求我重新输入用户名和密码,即使我已将其存储在该 DSN 的 ODBC 设置中,
  • 这有点奇怪。无论如何,我编辑了原始代码给你这个选项。

标签: sql stata


【解决方案1】:

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

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

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

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

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

【讨论】:

  • 是的,我认为这也可以,但是当我使用“./sqlfile.sql”调用它时,dsn("mysqlodbcdata") u(userId) p(Password) stata 返回 ' option u() not allowed' 我认为错误可能在这一行: odbc load, exec("``exec''"') dsn("dsn'"') u("u'"') p("p'"') clear' noquote' lowercase' sqlshow' allstring' ` datestring'; 因为当我调用它时用户和密码不在引号中
  • 您是否将User(string) 和Password(string) 添加到syntax 后面的括号部分?
  • @ander2ed 是正确的。我编辑了此答案中链接的代码以添加此选项。
  • 是的,我确实添加了该部分,但我没有将它放在括号内。谢谢你们的帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-11
  • 2018-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多