Want to create Stored Procedure of new table with calculated variable - oracle

I want to create a stored procedure using the table from Oracle and want to access in SAS EG.
I have below code .
create table xyz as
select * from (
select a,b,c,d
from table_name
)
pivot (MIN('X') for Variable_name in (
'PQR' as PQR, 'PGT' as PGT, 'KLD' as KLD,
'opd' as opd
)
)
order by Variable_name;
Is it possible to make this table as stored procedure? And if not then please suggest solution.

If I'm understanding your problem correctly you should be able to do this using SQL pass-through. You will just need to specify the Oracle database in the libname statement in SAS which will look something like this:
libname mydblib oracle user=user_name password=pw path='myoracleserver';
You can then access this using proc sql in SAS.
proc sql;
connect using mydblib;
create table xyz as
select * from connection to mydblib (
select * from (
select a,b,c,d
from table_name
)
pivot (MIN('X') for Variable_name in (
'PQR' as PQR, 'PGT' as PGT, 'KLD' as KLD,
'opd' as opd
)
)
order by Variable_name
);
quit;
This code creates the table xyz in your SAS work library.

Related

PLSQL ORACLE : Inner join between table variables

I need to create two table-type variables in oracle and make inner join between them.
I can't create temporary table in the source database because I dont have privileges.
How to create an anonymous plsql block in oracle something relative to this code in SQL server?
DECLARE #TB_PROJETO TABLE
(
ID INT,
NAME NVARCHAR(MAX)
)
DECLARE #TB_CAMERA TABLE
(
ID INT,
NAME NVARCHAR(MAX),
PROJETOID INT
)
BEGIN
INSERT INTO #TB_PROJETO
SELECT [ProjetoId], [Nome] FROM [dbo].[TbProjeto]
INSERT INTO #TB_CAMERA
SELECT [CameraId], [Nome],[ProjetoId] FROM [dbo].[TbCamera]
SELECT * FROM #TB_PROJETO P INNER JOIN #TB_CAMERA C ON P.ID = C.PROJETOID
END
The use of table variables is very common in SQL Server but Oracle doesn't have them because Oracle is pretty good at efficiently joining tables. So in Oracle the equivalent of your T-SQL routine would just be this:
SELECT c.ProjetoId
, p.Nome as project_nome
, c.CameraId
, c.Nome as camera_nome
FROM TbProjeto p
inner join TbCamera c
ON P.ID = C.PROJETOID
Column aliasing optional but done for clarity

Combining sets+tables+windowfunction in one go

I want to make something like this work in proc sql:
proc sql;
%connect_to_sql_macro;
create table sql.table as
(
select some_id, date from connection to oracle
(
select some_id, date, row_number()over(partition by some_id order by date) as row from dataset d join sql_table s on d.some_id=s.some_id
) where row=1
Basically i need to create table in oracle based on dataset joined with oracle table and take the first date for each id.
If i use "from oracle" pass through it doesn't see the dataset, if i make it the other way around i can't use window functions(row_number()) becouse they are not in SAS. For now i create a table from that dataset and then do a pass through but it's large and i need to do it in one go. Any suggestions?
You're probably better off doing it in SAS.
Can be done in one step too
libname lib <oracle connection options>;
proc sql;
select some_id
,date
from dataset d
inner join lib.sql_table s
on d.some_id=s.some_id
group by some_id
having date=min(date)
;
quit;
or
proc sql;
%connect_to_sql_macro;
select some_id
,date
from dataset d
inner join (select * from connection to oracle (
select * from sql_table
)
) s
on d.some_id=s.some_id
group by some_id
having date=min(date)
;
quit;
Also, limit as much as you can the amount of records that are being read from the oracle table.

How can to retrieve and write the BLOB in my storage in oracle

I have a table with this script:
CREATE TABLE PRSNL_IMG
(
PER_IMG LONG RAW,
NATIONAL_COD NUMBER
);
Now I want to retrieve and write the BLOB ( PER_IMG type: LONG RAW ) into a file with name "NATIONAL_COD" on path on hard sush as "D:\Photo_OUT\" from the database with name .My table have 82000 records.I have work with form6i and my database version is oracle 11g.
please help me.
You can create a table with BLOB like this
CREATE GLOBAL TEMPORARY TABLE foo (
b BLOB);
INSERT INTO foo VALUES ('1f8b080087cdc1520003f348cdc9c9d75128cf2fca49d1e30200d7bbcdfc0e000000');
and just select it
SELECT b FROM foo;
-- (BLOB)
SELECT UTL_RAW.CAST_TO_RAW(UTL_RAW.CAST_TO_VARCHAR2(b))
FROM foo;
-- 1F8B080087CDC1520003F348CDC9C9D75128CF2FCA49D1E30200D7BBCDFC0E000000
If the content is not too large, you can also use
SELECT CAST ( <blobfield> AS RAW( <maxFieldLength> ) ) FROM <table>;

Assign formats/informats for Oracle dataset in SAS

I use SAS/ACCESS path-through facility in order to create a table in Oracle:
proc sql;
connect to oracle(user=mylogin orapw=mypw path=mypath);
execute (
create table FCFCORE.RUS_FSC_RATE
( DATE_KEY NUMBER(8),
RATE NUMBER(20,10),
)
) by oracle;
disconnect from oracle;
quit;
When in EG I check properties of the table I see that DATE_KEY has format/informat 9., and RATE has informat 22.10. How can I change these formats?
If I use proc datasets the following error occures: ERROR: The HEADER/VARIABLE UPDATE function is not supported by the ORACLE engine.
You can use the DBSASTYPE data step option to override the default.
Example:
proc append base=dblib.hrdata (dbsastype=(empid='CHAR(20)'))
data=saslib.personnel;
run;

How to insert only new ( not duplicate ) row to the existing table - in proc sql - sas?

Does any one know how to insert only "new" row to the existing table with in SAS PROC SQL ?
proc sql;
create table class as
select *
from sashelp.class
where sex = 'F';
quit;
proc sql;
create table classm as
select *
from sashelp.class
where sex = 'M' or Name = 'Alice';
quit;
proc sql;
insert into class
select *
from classm ;
quit;
The insert statement does not allow me to use where statement to insert only 10 new row from classm ( without Alice )
Is there a way to go around this ? Because I'm working with big data I would like to do this with in proc sql , or data step is fine.
Thanks
This worked for me...
proc sql;
create table class as
select *
from sashelp.class
where sex = 'F';
quit;
proc sql;
create table classm as
select *
from sashelp.class
where sex = 'M' or Name = 'Alice';
quit;
proc sql;
insert into class
select *
from classm
where name^="John";
quit;
why not use the merge?
you sort your two tables by the columns who are the reference (id) and the merge the two tables with the selected keys.

Resources