Creating a Procedure in Oracle to create View - sql

I am trying to create a procedure in oracle, which upon calling from PL SQL block will create a view in database from which i will query data for a report. I am new to Oracle and need help with this code.
CREATE OR REPLACE PROCEDURE CREATE_VIEW
(
TO_DT IN Date
) AS
BEGIN
Create or Replace view BORR_DUR As
SELECT e."Deal_No", (Select "DeskName" From MM_S_DESK Where e."DeskCode" = MM_S_DESK."DeskCode") Facility, e."Remarks" Counterparty,
m."MaturityDate", m."PriRedem" Principal,
(select MAX("INTEREST_RATE") from MM_BOR_PLA_PAR d
WHERE e."Deal_No" = d."DEAL_NO" and "INTERESTINPUTDATE" <= to_dt)/100 yield, (m."MaturityDate" - To_date(to_dt,'dd/mm/yyyy')) Days_to_Mat,
Round(((m."MaturityDate" - To_date(to_dt,'dd/mm/yyyy'))/365)/ (1+((select MAX("INTEREST_RATE") from MM_BOR_PLA_PAR d
WHERE e."Deal_No" = d."DEAL_NO" and "INTERESTINPUTDATE" <= to_dt)/100)),4) MDURATION
FROM MM_T_BORROWING e, MM_T_BORROWING_PM_DETAIL m
Where e."DeskCode" in ('10','11','12','13') and e."Value_Date" <= to_dt and e."Maturity_Date" > to_dt and e."Status" not in ('C', 'D', 'Z', '0','X')
and e."Deal_No" = m."Deal_No" and "PriRedem" > '0' and m."MaturityDate" > to_dt;
END CREATE_VIEW;
On Compilation, i get PLS00103 error which says
encountered the symbol "Create" when expecting one of the
following....
Any help in solving this issue will be greatly appreciated.

When you want execute SQL statement which is dynamic you have to use EXECUTE IMMEDIATE statement
First , you don't need double quotes in fields name , after that you can try the query of the view and check if it runs without errors .
Put the create replace view... statement in an variable and in your procedure call :
BEGIN
EXECUTE IMMEDIATE view_string_variable ;
END;
/

Related

How to run procedure without parameter in Teradata

How to run procedure without parameter in Teradata
I'm trying with : call db1.proc_dbOSA()
Error Msg:
Call failed 3707: PROC_DBOSA: Syntax error, expected something
like a name or a Unicode delimited identifier between ‘(‘ and ‘)’
New Procedure with error result.
When i run only code then everything works ok.
REPLACE PROCEDURE db1.proc_dbOSA()
BEGIN
DELETE FROM db1.LOG_dbOSA;
INSERT INTO
db1.LOG_dbOSA
(StoreNo, IDX, Flow, Status, MRP, OSA_IDX, OSA_AC)
WITH a AS (
SELECT
c.StoreCode,
CAST(SUBSTRING(c.ArticleCode FROM 13 FOR 6) AS INT) AS IDX,
RpType,
CASE
WHEN c.MinimumTargetStockWrpl >= l.MinimumTargetStockWrpl THEN CAST(l.MinimumTargetStockWrpl AS INT)
WHEN c.MinimumTargetStockWrpl < l.MinimumTargetStockWrpl THEN CAST(c.MinimumTargetStockWrpl AS INT)
End AS StoreMin,
c.ValUnrestrictedStock
FROM
db1.tab1 c
INNER JOIN
(
SELECT
StoreCode,
ArticleCode,
MinimumTargetStockWrpl
FROM
db1.tab1
WHERE
ProcessingDate = CURRENT_DATE - 14
) l ON c.StoreCode = l.StoreCode AND c.ArticleCode = l.ArticleCode
WHERE
c.ProcessingDate = CURRENT_DATE AND c.MinimumTargetStockWrpl IS NOT NULL AND l.MinimumTargetStockWrpl IS NOT NULL AND l.MinimumTargetStockWrpl > 0
)
, t AS
(
SELECT
CAST(SUBSTRING(ArticleCode FROM 13 FOR 6) AS INT) AS IDX,
RpType,
ArticlesPlanner
FROM
DWH_db_V.STK_B_ARTICLE_DAY_V
WHERE
ProcessingDate = CURRENT_DATE AND StoreCode = 'DR04'
)
SELECT
a.StoreCode,
a.IDX,
t.RpType,
t.ArticlesPlanner,
a.RpType,
CASE
WHEN a.ValUnrestrictedStock > 0 THEN 1
WHEN a.ValUnrestrictedStock <= 0 THEN 0
End AS OSA_IDX,
CASE
WHEN a.ValUnrestrictedStock >= StoreMin THEN 1
WHEN a.ValUnrestrictedStock < StoreMin THEN 0
End AS OSA_AC
FROM
a
LEFT JOIN
t ON t.IDX = a.IDX;
End;
BTEQ Error:
+---------+---------+---------+---------+---------+---------+---------+----
Call proc_dbOSA;
Call proc_dbOSA;
$
* Failure 3707 Syntax error, expected something like '(' between the word
'proc_dbOSA' and ';'.
Statement# 1, Info =18
* Total elapsed time was 1 second.
Call proc_dbOSA();
* Failure 3707 PROC_DBOSA:Syntax error, expected something like a name or
a Unicode delimited identifier between '(' and ')'.
* Total elapsed time was 1 second.
Stored procedures do not support the following:
EXPLAIN and USING request modifiers within a stored procedure
EXECUTE macro statement
WITH clause within a stored procedure.
Stored procedures, as well as macros, do not support the following query logging statements:
BEGIN QUERY LOGGING
END QUERY LOGGING
FLUSH QUERY LOGGING
REPLACE QUERY LOGGING

SAP HANA SQL Query with Dynamic Placeholder

I have a query that is passing the current year as a placeholder parameter that right now is hard coded. How can I have this just pass the current year? I've seen a few different potential solutions but most of them are in HANA Studio or involve dynamic SQL generation.
I'm putting the SQL into Tableau so those are both off the table.
...sum("StockInQualityInspection") as in_quality,
sum("StockInTransit") as its
from "_SYS_BIC"."stream.models.marketing.poly/InventoryQuery" ('PLACEHOLDER' = ('$$IPCurrentYear$$', '2018'))
where "StockValuatedUnrestrictedUse" <> 0 or "StockInQualityInspection" <> 0 or "StockInTransit" <> 0
group by case when "ReceivingPlant" is null then "Plant" else "ReceivingPlant" end,
case....
Remove the parameters input of your CV
Add this expression: year(now())
If you don't have access to manipulate the CV, into your query use:
('PLACEHOLDER' = ('$$IPCurrentYear$$', select year(now()) from DUMMY))
Regards
while placing a query is not permitted, you can pass a parameter as following
do begin
declare lv_param nvarchar(100);
select max('some_date')
into lv_param
from dummy /*your_table*/;
select *
from "_SYS_BIC"."path.to.your.view/CV_TEST" (
PLACEHOLDER."$$P_DUMMY$$" => :lv_param
);
end;
more can be found here
credit to #astentx

Create View in procedure PL/SQL

i've had a look on the forum but at the moment theres no answer to this question. Basically i'm trying to create a view inside my procedure as its easier than a complicated select statement that doesnt seem to be working because its returning 'ORA-01422 exact fetch returns more than requested number of rows'.I've put the statement at the bottom for reference
However the view option doesnt seem to be working its return another error 'Encountered the symbol "CREATE" when expecting one of the following:'. Is there something i'm doing wrong?
Thanks for having a look.
procedure proRekt (par_id number) is
vardec farm.parcel.description%type;
vland farm.landuse.landuse_id%type;
v_luse farm.landuse.landuse%type;
varpl farm.parcel.parcel_id%type;
begin
create or replace view Door as
select a.parcel_id,a.description,b.landuse_id,c.landuse
from
farm.parcel a,
farm.parcel_landuse b,
farm.landuse c
where a.parcel_id = b.parcel_id
and b.landuse_id = c.landuse_id;
select parcel_id,description,landuse
into varpl,
vardec,
vland
from door
where parcel_id = parID;
**** faulty select statement
select a.parcel_id,c.description,a.landuse_id,b.landuse
into varpl,
vardec,
vland,
v_luse
from farm.parcel_landuse a,
farm.landuse b,
farm.parcel c
where c.parcel_id = parID
and a.landuse_id = b.landuse_id
and a.parcel_id = c.parcel_id;
Create the view in the SQL scope:
create or replace view Door as
select a.parcel_id,
a.description,
b.landuse_id,
c.landuse
from tithedb.parcel a
INNER JOIN tithedb.parcel_landuse b
ON ( a.parcel_id = b.parcel_id )
INNER JOIN tithedb.landuse c
ON ( b.landuse_id = c.landuse_id );
Use it in the PL/SQL scope of the procedure:
procedure proexempt (parID number)
is
vardescription tithedb.parcel.description%type;
varland tithedb.landuse.landuse_id%type;
varlanduse tithedb.landuse.landuse%type;
varparcel tithedb.parcel.parcel_id%type;
begin
select parcel_id,
description,
landuse
into varparcel,
vardescription,
varlanduse
from door
where parcel_id = parID;
-- ... more stuff
EXCEPTION
WHEN NO_DATA_FOUND THEN
NULL; -- Handle error here.
WHEN TOO_MANY_ROWS THEN
NULL; -- Handle error here (or use "AND ROWNUM = 1" in your query).
END proexempt;
/

Using variable from cursor for loop in where clause

I was doing some SQL on Oracle and I found something weird.
I have that FOR loop that iterate over a list of date (Stored as VARCHAR2):
FOR current_date in (SELECT DISTINCT DATCRE as datcre
FROM EVE cdv
WHERE cdv.datcre BETWEEN p_datedeb AND p_datefin)
LOOP
[queries]
END LOOP;
I need to use current_date in the where clause of the following query:
SELECT COUNT(cdv.numeve)
INTO nb
FROM EVE cdv
WHERE cdv.datcre = current_date.datcre;
This query doesn't work, and I get a ORA-00933: SQL command not properly ended.
Now, if I declare a curDate VARCHAR2(10) variable, store current_date.datcre in it and use it in the query, it works fine:
curDate := (current_date.datcre);
SELECT COUNT(cdv.numeve)
INTO nb
FROM EVE cdv
WHERE cdv.datcre = curDate;
Am I missing something or is this just the way Oracle works ?

Compilation error in Oracle PL/SQL

I have been staring at this hours trying to figure out why it won't run, and cannot see any syntax error which should stop this from compiling, but I am nonetheless getting the following errors:
Error(6,1): PL/SQL: SQL Statement ignored
Error(22,7): PL/SQL: ORA-00933: SQL command not properly ended
The function I am trying to create is as follows:
create or replace function caps_get_Uoffer_count_BD(ayrc in varchar2
,mcrc in varchar2)
return number
is
app_count number;
begin
select count(*)
from
(select distinct cap.cap_stuc,cap.cap_apfs , cap.cap_seqn from
intuit.srs_cap cap
,intuit.srs_apf apf
,intuit.srs_ioi ioi
where cap.cap_ayrc = ayrc
and cap.cap_mcrc = mcrc
and apf.apf_stuc = cap.cap_stuc
and apf.apf_seqn = cap.cap_apfs
and apf.apf_recd <= to_date('1501'||substr(ayrc,1,4),'DDMMYYYY')
and cap.cap_stuc = ioi.ioi_stuc
and cap.cap_mcrc = ioi.ioi_mcrc
and ioi.ioi_iodc ='PAPERLESS'
and cap.cap_stac like 'A%'
and cap.cap_idrc in ('U','UF','UI','UD','CFU','CFUF','CIUI','CIUF','CIUD','CIU','CFUI','CFUD'))
return(app_count);
end;
Database is Oracle 11g. Can anyone help?
Many thanks in advance