SAS与DBMS系统(SqlServer)通信
1 使用OLEDB
代码说明:将192.168.0.3服务器上TestDB数据库中的TestTable表复制到本地的SAS的Work逻辑库中
libname mydb oledb init_string="Provider=SQLOLEDB.1;Password=******;Persist Security Info=True;User ID=sa;Initial Catalog=TestDB;Data Source=192.168.0.3";
data TestTable;
set mydb.TestTable;
run;
proc print;
run;
data TestTable;
set mydb.TestTable;
run;
proc print;
run;
2 使用ODBC引擎
代码说明:将192.168.0.3服务器上TestDB数据库中的TestTable表复制到本地的SAS的Work逻辑库中
首先在系统中建立ODBC引擎“SasTest”
libname mydb odbc uid=sa pwd=******database=TestDB dsn=SasTest;
data TestTable;
set mydb.TestTable;
run;
proc print;
run;
data TestTable;
set mydb.TestTable;
run;
proc print;
run;
3 数据库链接向导
代码说明:打开数据库链接向导
libname mylib oledb;
%put &sysdbmsg;(%put %superq(SYSDBMSG); SASV9使用此更佳)
data bpSAS;
set mylib.bp;
run;
proc print;
run;
%put &sysdbmsg;(%put %superq(SYSDBMSG); SASV9使用此更佳)
data bpSAS;
set mylib.bp;
run;
proc print;
run;
4 链接Sql Server使用NT验证
代码说明:将SasHelp.Class表传到sql server上
libname x oledb provider=sqloledb dsn='SasTest' properties=("Integrated Security"=SSPI "Persist Security Info"=True
"Initial Catalog"=Northwind);
proc sql;
create table x.class as select * from sashelp.class where age>10;
quit;
"Initial Catalog"=Northwind);
proc sql;
create table x.class as select * from sashelp.class where age>10;
quit;
5 调用数据库存储过程
l 有返回值,存储过程名:usp_who
proc sql;
connect to oledb(dsn='SasTest' uid=as pwd=****** provider=sqloledb);
select * from connection to oledb (usp_who);
quit;
connect to oledb(dsn='SasTest' uid=as pwd=****** provider=sqloledb);
select * from connection to oledb (usp_who);
quit;
proc sql;
connect to oledb(dsn='SasTest' uid=as pwd=****** provider=sqloledb);
execute (sp_rename "BADCRIME", "BARBCRIME") by oledb;
quit;
connect to oledb(dsn='SasTest' uid=as pwd=****** provider=sqloledb);
execute (sp_rename "BADCRIME", "BARBCRIME") by oledb;
quit;
6 Accessing OLAP data(访问 联机分析处理 数据)
PROC SQL;
CONNECT TO OLEDB (PROVIDER=MSOLAP PROPS=('INITIAL CATALOG'='FoodMart 2000'
'DATA SOURCE'='dwtsrv1'));
SELECT * FROM CONNECTION TO OLEDB
(MDX::Select NON EMPTY { [Time].[1997], [Time].[1998] } ON COLUMNS, NON
EMPTY { [Account].[All Account] } ON ROWS From [Budget] Where ([Category].
[All Category],[Measures].[Amount],[Store].[All Stores]) );
QUIT;
CONNECT TO OLEDB (PROVIDER=MSOLAP PROPS=('INITIAL CATALOG'='FoodMart 2000'
'DATA SOURCE'='dwtsrv1'));
SELECT * FROM CONNECTION TO OLEDB
(MDX::Select NON EMPTY { [Time].[1997], [Time].[1998] } ON COLUMNS, NON
EMPTY { [Account].[All Account] } ON ROWS From [Budget] Where ([Category].
[All Category],[Measures].[Amount],[Store].[All Stores]) );
QUIT;