SAS to Oracle ODBC - passing a SAS table INTO the database - oracle

Can anyone please advise the syntax for passing a table FROM a SAS library INTO an oracle database?
example code below (although obviously the connection to the WORK library cannot be referenced in this way)
PROC SQL noprint;
connect to ODBC as X (dsn='ALIAS' uid='USER1' pwd='passwd' quote_char='');
exec (CREATE TABLE Test AS
SELECT * from WORK.MY_SAS_TABLE
)by X;
disconnect from X;
quit;
A similar question was asked here but seems to relate to a SQLSERVER connection rather than oracle..

Set up a libref to point to your Oracle database, either using the ODBC libname engine or the Oracle libname engine (which will be faster if you have the right licence and software installed):
libname X oracle username='USER1' password='passwd' path=ORCL;
If an empty table with the right columns already exists in Oracle, you can use:
proc sql noprint;
insert into X.test select * from work.my_sas_table;
quit;
If the table doesn't exist, you can use a data step:
data X.test;
set work.my_sas_table;
run;

I'm a bit rusty, but what if you set up your database as a libref?
Something like:
libname X odbc dsn='ALIAS' uid='USER1' pwd='passwd' quote_char='';
data X.test;
set work.my_sas_table;
run;

Related

Invalid DataType Error for Proc SQL Query

I'm creating a basic query using Oracle Pass Through and a macro loop. It worked fine when I did Connect to Oracle, create table from connection but when I just try to use just the execute(DDL statements) by Oracle, it give me an invalid datatype error. I'm not sure how to fix it. I'm just creating tables from another existing table. I'm using SAS EG for this particular program but just the code editor not the query builder.
The &STATES macro resolves to a list of states in a global macro I created
I've written the same query successfully using the connect to Oracle, create table from connection pass through method but would like to also get it to work with just the execute method to see test which is more efficient. However, when I use the execute method, I get the datatype error.
%let NUM=2;
%let FT=LT;
%let STATES=&&STATESR#
PROC SQL;
connect to oracle(path=&mydb user=&USER pw=&PW);
%macro DTCNT() / parmbuff;
%let i=1;
%let ST=%scan(&SYSPBUFF,&I);
%do %while (%str(&ST)^=);
execute(CREATE TABLE TMSIS_&ST._&FT._HDR_ADJDT_CNTS1 (bulkload=yes dbcreate_table_opts=nologging) AS
SELECT adj_date,
count(*) as row_cnt
from mcd_r&NUM._own.&ST._&FT._header_f
group by adj_date
order by adj_date) by oracle;
execute (commit) by oracle;
%let i=%eval(&I+1);
%let ST=%scan(&SYSPBUFF,&I);
%end;
%mend DTCNT;
%DTCNT(&STATES);
disconnect from oracle;
QUIT;
ERROR: ORACLE execute error: ORA-00902: invalid datatype.
You are using the EXECUTE() statement so the SQL inside that needs to be Oracle code. Oracle will not understand SAS style dataset options. You seem be asking to create a variable named bulkload with a data type of yes.
Perhaps you meant something like this?
execute(
CREATE TABLE TMSIS_&ST._&FT._HDR_ADJDT_CNTS1 AS
SELECT adj_date
, count(*) as row_cnt
from mcd_r&NUM._own.&ST._&FT._header_f
group by adj_date
) by oracle;

How to execute sybase stored procedure in SQL Developer with input and out put params?

I am working on a new Database in Sybase. I am not sure how to run sybase stored procedures from SQL Developer. I have used SQL developer before with Oracle.
my stored procedure look like this. IT is working stored procedure.
create proc stored_proc_name #string_input varchar(20)
as
declare
# temp_string varchar(20)
select #temp_string from table where conduction
return 0
fail:
....
exec db_schema..stored_proc_name 'string_input'
Output
db_schema..stored_proc_name 'string_input' succeeded.
ITs not printing the actual output.
Please help me fix my issue.

Joining 2 Oracle tables using Join in SSIS

I have to get data from database1 something like
select *
from tab1
where dtCol > <*dtVariable*>
The value of dtVariable comes from database2 from
select *
processdt
where proc_name = 'PROCESS1'
Can you please let me how to do this?
I am using SSIS 2008
Oracle db - 12c
Oracle Drivers - Attunity 1.2
No DBLInks ( basically, we need to avoid using db links)
You would simply need to pull the input value from Db2 to a variable in SSIS as step 1.
Step 2, you would create an expression that forms that SELECT sql call encompassing that variable.
MSDN has an article how to use Execute SQL Task.

How query from another database that is in another machine/server

I've been looking for answer to this but I can't seem to find the right answer online and my problem goes like this.
I'm trying to query a set of records from another table which is in an another database installed in a different machine. To make it clearer:
My stored procedure is running on IP: 192.168.XX.X1. I get to retrieve all the information I need in this server but I have another set of information or records that can only be retrieved from IP: 192.168.XX.X2.
I was thinking to achieve something like:
DECLARE
-- given that both queries will only return 1 record
CURSOR IS curSample1
SELECT * FROM Database1.Table1;
colSample curSample1%ROWTYPE;
CURSOR IS curSample2
SELECT * FROM Database2.Table1;
colSample curSample2%ROWTYPE;
vText1 VARCHAR(20);
vText2 VARCHAR(20);
BEGIN
OPEN curSample1;
LOOP
FETCH curSample1 INTO colSample1;
EXIT WHEN curSample1%NOTFOUND;
vText1 := colSample1.Column1;
END LOOP;
CLOSE curSample1;
OPEN curSample2;
LOOP
FETCH curSample2 INTO colSample2;
EXIT WHEN curSample2%NOTFOUND;
vText2 := colSample2.Column2;
END LOOP;
CLOSE curSample2;
dbms_output.put_line(vText1 || ',' || vText2);
END;
Any help you could provide will be much appreciated. Thank you very much.
Note: I'm trying this approach as this is the only way we could possibly do it as of now. Thanks again.
You will have to create a db link between your database 1 and database 2. For creating a database link it is not required to have both databases on the same server. Since in your case the databases are on different server you can start with the following steps.
You need a tns entry (pointing to database 2) in the tnsnames.ora file on your database 1 server. You can check if you have this entry by connecting to SQLPLUS from your database 1 machine to database 2.
sqlplus <username>/<password>#<tnsnames of database2>
If you are able to connect from your database 1 server then you can proceed with the following steps for creating the db link.
CREATE DATABASE LINK <dblink_name> CONNECT TO <username> IDENTIFIED BY <password> USING <tnsnames of database2>
Post this you can test your database link by running the following SQL command.
select * from Table#<dblink_name>;
as i know you cannot query data cross database directly.
1,maybe you can use DBlink or DataSync to let the data which in other database can be query.
2,instead of pl/sql procedure, use other development language to do cross DB process is a good idea(ex independent java program).
3,instead of pl/sql procedure, use Oracle Java Procedure to do this.

SAS: How to copy a local sas table into oracle via SQL pass-through

After making some searches that didn't lead to anything useful for me, I would like to ask you this question.
Some background:
I would like to create an oracle table via two different methods in order to compare performances. Actually I want to copy a table from one of my local SAS libraries to Oracle.
I used a first method (that works perfectly) with a libname to oracle:
LIBNAME dblib ORACLE USER=usr PASSWORD="&dbpwd_sas" PATH="DM_CT_TEST" SCHEMA="SAS";
PROC SQL NOPRINT;
CREATE TABLE dblib.TEST_WIN7 AS SELECT *
FROM SASHELP.CARS
WHERE STRIP(UPCASE(make)) EQ "ACURA"
;
QUIT;
LIBNAME dblib CLEAR;
But I also try to use another method via SQL pass-through that doesn't work:
PROC SQL NOPRINT;
CONNECT TO ORACLE (USER=usr PASSWORD="&dbpwd_sas" PATH="DM_CT_TEST");
EXECUTE ( CREATE TABLE sas.TEST_WIN7 AS
SELECT * FROM SASHELP.CARS
WHERE STRIP(UPCASE(make)) EQ "ACURA"
) BY ORACLE;
DISCONNECT FROM ORACLE;
QUIT;
With this method, SASHELP.cars is not found by the procedure.
So here is my question:
Is it possible to copy a local SAS table into oracle via an SQL pass-through? If yes, how to proceed.
Thank you
In SQL Passthrough, the connected session cannot see your SAS libraries, unless it is separately connected through DBMS-specific connection methods (unlikely). Pass-through is literally taking code and submitting it for execution on the remote DBMS, SAS is just a dumb terminal.
The only way you can access information from SAS in explicit pass-through is to transmit it as text in a macro variable. As such, you can perform in queries by putting the list of values in a macro variable and submitting it; but you cannot reference a SAS table directly. You could pass the contents of an entire table in a macro variable or a set of macro variables and then use insert statements to include them, but it's not a good idea; it won't be faster than using a libname connection and has a lot more risk of error (and is more work).
In most instances, you should simply use libname access when you want to see both SAS and RDBMS data at the same time. SAS will use implicit pass-through when it can to speed things up when using libname access.
If you're trying to use passthrough in order to avoid SAS having to download data to perform a join, one thing you can do is upload the SAS data to a global temporary table using libname access. Then you can connect via passthrough and use that temporary table. Just be aware that the two connections are separate, so they won't share non-global temporary tables. (I believe in newer versions they have added a feature to make them share connections, but I couldn't find the syntax and don't recall having much success using it myself.)

Resources