Connect SAS to a Microsoft SQL Server database - sql

I'd like to connect SAS to a Microsoft SQL Server database but I didn't succeed.
I already have a database connection using a NT authentication.
For instance, when I want to connect R (the statistical sofware) to the db, I do that
ch_db <- odbcConnect("sql_nt")
But when I do that in SAS, it doesn't work :
LIBNAME sql ODBC DSN=’sql_nt’;
I have this error message :
ERROR: Libname SQL is not assigned. ERROR: Error in the LIBNAME
statement. ERROR 22-7: Invalid option name SQL_NT.
I probably do a stupid mistake, but I don't see it.

In SAS to make a ODBC CONNECTION TO SQL SERVER;
First make a User DSN using Windows ODBC Data Source Administrator. I use the SQL Server Native Client and the defaults.
THEN IN SAS EXECUTE THE FOLLOWING STATEMENT
libname mySasLib odbc datasrc='myUserDSN';
ALTERNATIVELY, from the SAS Explorer window GUI, choose New to invoke the New Library Dialog.
Note the DSN sources on your machine will be listed in the Data Source dropdown.
User ID, Password, and Options fields are optional and are left blank for Windows Integrated Security.
;
SUBSEQUENTLY--to get the power of SQL pass-through--here is the syntax for creating a virtual view in Work; this is an incredible performance boost for my situation.
proc sql;
connect to ODBC as mycon (datasrc='myUserDSN');
create view one as
select colA, colB from connection to mycon
(select colA, colB from tableInDataSrc order by colA);
disconnect from mycon;
quit;
Then something like:
proc univariate data=one;
by colA;
histogram colB;
run;

Take a look at this page:
http://support.sas.com/documentation/cdl/en/acreldb/63647/HTML/default/viewer.htm#a001355231.htm
Specifically, I think you should try it this way:
libname mydblib odbc user=testuser password=testpass datasrc=mydatasource;
Typically you should provide user name and password when connecting to a SQL server. I assume you've verified that the ODBC connection was set up correctly (such as testing it with R).
Edit from comments:
If you're using NT authentication, then follow the instructions here: (from http://support.sas.com/techsup/technote/ts802.pdf )
libname odbclib odbc noprompt="dsn=sql_NT;Trusted_Connection=yes"
schema=DBO;
I suspect the old style of just dsn="stuff" doesn't work on newer versions - though I only use OLEDB so I'm not 100% sure.

I have tried to connect using both proc sql statements and the libname format. The latter seems more user friendly, here is one example:
LIBNAME testLib ODBC DSN='sqlserver' user=userID pw=xxxxxxxx schema=dbo;
title 'Testing Libname Option';
proc contents data= testLib.mytable;
proc print data=testLib.mytable(obs=10);
UserID and PWD need to come from your ODBC connection setup.

I am a SAS newbie and wasted quite a lot of time trying to create a 'user DSN' .
Thankfully, I checked with admin team in my company.
One of the SAS admin replied
"DSNs are configured on SAS server side "
She requested for 'MSSQL database and SAS server name' and created DSN for me.
Once connection was established i used below query
(P.S : I used windows authentication)
LIBNAME sql odbc = 'xxxxxx' ;
LIBNAME myfolder 'xxxxxxxxxxxxxxxx' ;
proc sql;
create table sql.target_table as select * from myfolder.mydata ;
quit;

Related

How do I make a read-only "Connect to ODBC" connection in SAS Proc SQL?

I am creating a SAS macro function that my co-workers may use to connect via ODBC connections to various databases using SAS Proc SQL, to run pass-through SQL code. I would like the connection to be read-only, even though the users have write-access to the databases. I can do this in a LIBNAME statement, with ACCESS=READONLY:
LIBNAME myLib ODBC NOPROMPT="DATABASE=myDB;Server=myServer;DRIVER={SQL Server};Trusted_Connection=Yes;" ACCESS=READONLY;
I can create a read-write connection like so:
PROC SQL ;
CONNECT TO ODBC AS myCon(NOPROMPT="DATABASE=myDB;Server=myServer;DRIVER={SQL Server};Trusted_Connection=Yes;") ;
EXECUTE( INSERT INTO myTable(myColumn) VALUES(1) ) BY myCon ; ** insert a row where myColumn=1;
QUIT;
But inserting ACCESS=READONLY as an option in Connect To ODBC (), like CONNECT TO ODBC AS myCon(ACCESS=READONLY NOPROMPT="DATABASE=myDB;Server=myServer;DRIVER={SQL Server};Trusted_Connection=Yes;") ;, results in ERROR: Invalid option name ACCESS.
I know that there is nothing I can put within the connection string to make the connection read-only. Is there some way to make this Connect to ODBC ... into a read-only connection?
First, you can do it using the libname!
proc sql;
connect using mylib;
... sql stuff ...
quit;
Second, this is going to be in the ODBC init string (so inside the "quotes") and will depend on what driver you're using. The modern SQL server drivers have ApplicationIntent=ReadOnly; see for example this doc page for the v15 driver. I don't know if the older {SQL Server} driver supports that or not.

How to create Oracle 19C Database SQL Developer New Connection?

Well I am a college student and I have a database project to build on Oracle 19c (SQL Developer) just like a management system. When I click on new connection in SQl Developer , then enter database name (my project name) and enter user name as system and pswd. and is SID I write 'orclpdb' and when I connect it gives an error that database is not open. How can I start making tables and stuff and complete my project on oracle 19c sql developer. Please guide me.
Update: While creating new database connection in SQL Developer, In Service name if I write 'orcl' and test then the connection is successful but when I write 'orclpdb' it says 'database not open'
So it sound like the container database (orcl) is started but the pluggable database (orclpdb) is not. You need to start it.
In order to actually manage a database you are going to have to learn to work without SQL Developer, and use the command-line utility sqlplus.
From a command line:
C:> set ORACLE_SID=orcl
C:> sqlplus / as sysdba
SQL> alter pluggable database orclpdb open;
In the above, the sqlplus command is launching the command line utility 'sqlplus'. the '/' indicates to make a local connection to the database specified by the env variable ORACLE_SID, using OS authentication (os user is a member of the ORACLE_DBA group), and connect with 'sysdba' authority. On the next line the 'SQL>' is just indicating you are now at the sql prompt within sqlplus, you actually enter the 'alter' command, whose purpose should be self-evident.
The listener is a totally separate process. It is like a telephone switchboard. It 'listens' (hence, its name) for connection requests coming across the network, and set up the connection, then is out of the picture. You check its status at the command line:
C:> lsnrctl status
One last bit of useful (for us) information. What this the connection 'type' you've defined in SQL Dev? Is it 'basic' or 'tns'? Either way, what did you specify for the values? Please name the field(s) AND the value you supplied.

Connect SQL Server to SAP Backend (also a SQL Server)

I am trying to connect my SQL Server directly to the SAP backend database so that I don't have to extract the data everyday (for fresh data) using SSIS packages.
Instead I want to create views that will access this data directly (direct querying) and get refreshed periodically.
Can someone give me a link or show/tell me the steps on how to do this?
If you know the hostname of the backend server, you can do something like this:
SELECT TOP 1 * FROM hostname.databasename.dbo.tablename
If you want to access your data from external SQL server directly in SAP you can define connection to external database in transaction DBCO:
Then in your ABAP code you can write SQL statements using OPEN SQL:
EXEC SQL.
CONNECT TO my_new_connection.
ENDEXEC.
...
EXEC SQL.
SELECT * INTO TABLE :lt_internal_table FROM dbtable.
ENDEXEC.
...
EXEC SQL.
DISCONNECT my_new_connection.
ENDEXEC.
or using Classes CL_SQL_CONNECTION, CL_SQL_STATEMENT. An Example program ADBC_DEMO show how to use it.

Moving SAS dataset into SQL Server WITHOUT using SSIS packages

I have a vb.net 2010 project that would remotely run a prepared SSIS package that exports a SAS 9.3 dataset into a SQL Server 2012 database. As far as I can tell, the process tested fine.
However, I was told not to use it (because SSIS packages are unreliable?), so are there alternatives to doing this?
I have looked up SAS ODBC to see if I can do this from the SAS end, but I am not sure of the DSN argument, the example I looked up is like this:
LIBNAME SQL ODBC DSN='sqlsrv_nt' schema=MSSQLTips;
Besides not being sure that the DSN is applicable for me, I am not administrator on my workstation so I can't play with ODBC settings much - I'm not certain that's the way to go.
You can either use a driver or DSN (customized shortcut data source connection object with all configurations/settings set). Once connected, append your data from local to remote source.
* WITH DSN;
libname mssqldata odbc datasrc="DSN Name" user="username" password="password";
* WITH DRIVER;
libname mssqldata odbc complete="driver={SQL Server}; Server=servername; user=username; pwd=password; database=databasename;";
** APPEND TO DATABASE TABLE;
proc datasets;
append base = mssqldata.DBTable
data = Work.SASdataset
force;
quit;
** UN-ASSIGN ODBC LIBRARY;
libname mssqldata clear;
Be very careful with assigning library to database as it is a live connection and not copies. Hence, modifying/adding/deleting will reflect immediately to server.
You don't need to be administrator. Set up a DSN connection under user profile. Then assign your libname and you can upload or update data in SQL server as permissions allow.
Your libname statement looks correct.

How do you setup a linked server to an Oracle database on SQL 2000/2005?

I am able to create and execute a DTS package that copies tables from a remote Oracle database to a local SQL server, but want to setup the connection to the Oracle database as a linked server.
The DTS package currently uses the Microsoft OLE DB Provider for Oracle with the following properties:
Data Source: SERVER=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=10.1.3.42)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=acc)));uid=*UserName*;pwd=*UserPassword*;
Password: UserPassword
User ID: UserName
Allow saving password: true
How do I go about setting a linked server to an Oracle database using the data source defined above?
I was able to setup a linked server to a remote Oracle database, which ended up being a multi-step process:
Install Oracle ODBC drivers on SQL Server.
Create System DSN to Oracle database on SQL Server.
Create linked server on SQL server using System DSN.
Step 1: Install Oracle ODBC drivers on server
a. Download the necessary Oracle Instant Client packages: Basic, ODBC, and SQL*Plus (optional)
b. Unzip the packages to a local directory on the SQL server, typically C:\Oracle. This should result in a [directory] like C:\Oracle\instantclient_10_2, which will be the value of [directory] referenced in the rest of this answer.
c. Create a text file named tnsnames.ora within the instant client [directory] that contains the following:
OracleTnsName =
(
DESCRIPTION=
(
ADDRESS = (PROTOCOL=TCP)(HOST=10.1.3.42)(PORT=1521)
)
(
CONNECT_DATA = (SERVICE_NAME=acc)
)
)
Note: Actual HOST, PORT, and SERVICE_NAME will vary based on Oracle server you are establishing a connection to. This information can often be found using the Oracle network client tools under the listeners.
The OracleTnsName can be any name you want to assign to the Oracle data source, and will be used when setting up the system DSN. You can also use the syntax above to define multiple TNS names in the same tnsnames.ora file if desired.
d. Add the [directory] to the system PATH environment variable.
e. Create a new system environment variable named TNS_Admin that has a value of [directory]
f. Execute the [directory]\odbc_install.exe utility to install the Oracle ODBC drivers.
g. It is recommended that you reboot the SQL server, but may not be necessary. Also, you may want to grant security permissions to this directory for the SQL server and SQL agent user identities.
Step 2: Create a System DNS that uses the Oracle ODBC driver
a. Open the ODBC Data Source Administrator tool. [ Administrative Tools --> Data Sources (ODBC) ]
b. Select the System DSN tab and then select the Add button.
c. In the drivers list, select Oracle in instantclient {version}. (e.g. 'Oracle in instantclient 10_2') and then select Finish button.
d. Specify the following:
Data Source Name: {System DSN Name}
Description: {leave blank/empty}
TNS Service Name: should have the OracleTnsName you defined in the tnsnames.ora file listed, select it as the value.
User ID: {Oracle user name}
e. Select Test Connection button. You should be prompted to provide the {Oracle user password}. If all goes well the test will succeed.
Step 3: Create linked server in SQL to the Oracle database
Open a query window in SQL server and execute the following:
EXEC sp_addlinkedserver
#server = '{Linked Server Name}'
,#srvproduct = '{System DSN Name}'
,#provider = 'MSDASQL'
,#datasrc = '{System DSN Name}'
EXEC sp_addlinkedsrvlogin
#rmtsrvname = '{Linked Server Name}'
,#useself = 'False'
,#locallogin = NULL
,#rmtuser = '{Oracle User Name}'
,#rmtpassword = '{Oracle User Password}'
Note: The {Linked Server Name} can be anything you want to use when referencing the Oracle server, but the {System DNS Name} must match the name of the system DSN you created previously.
The {Oracle User Name} should be the same as the User ID used by the system DSN, and the {Oracle User Password} should be the same as you used to successfully test the ODBC connection. See KB 280106 for information on troubleshooting Oracle linked server issues.
Querying the Oracle linked server
You may use OPENQUERY to execute pass-through queries on the Oracle linked server, but be aware that for very large recordsets you may receive a ORA-01652 error message if you specify a ORDER BY clause in the pass-through query. Moving the ORDER BY clause from the pass-through query to the outer select statement solved this issue for me.
I had the same problem. I was on the phone with Microsoft for hours, and they did not have a solution. None of those "connection timeout" settings helped me.
To resolve it, I created a DTS job that runs a proc which only updates the time on one row, in one column, every two minutes. Then I setup a replication between SQL Server and Oracle, scheduled to replicate that single cell change, from SQL to Oracle, every 3 minutes. It keeps the connection alive!