How to reference function in Node.js Postgres API Call - sql

I have this function called pvt():
Create Or Replace Function pvt()
RETURNS void
LANGUAGE 'plpgsql'
AS '
declare
sqlColumn varchar;
qr varchar;
columnlist varchar;
Begin
sqlColumn= ''select distinct D.sys_cat from (select Row_Number() Over (Partition By Project,Date_ Order By System_) as sys_cat From your_table) D order by D.sys_cat;'';
qr=''prepare pvtstmt as Select D.Project,D.Date_,'';
For columnlist In EXECUTE sqlColumn
Loop
qr=qr || ''
Max(Case When sys_cat=''|| chr(39) || columnlist || chr(39) ||'' Then System_ ||'' ||chr(39)||''-''||chr(39)||''|| Result_ Else '' ||chr(39)||chr(39)||'' End) As System'' || columnlist || '' , '';
END LOOP;
qr=substr(qr, 0, length(qr) - 1);
qr=qr || ''From
(select *, Row_Number() Over (Partition By Project,Date_ Order By System_) as sys_cat From your_table) D Where D.Project='' || chr(39) || ''Proj1'' || chr(39) || ''Group By D.Project,D.Date_ Order By D.Project,D.Date_'';
Deallocate All;
EXECUTE qr;
End;
'
I need to execute these SQL statements:
Select pvt();
Execute pvtstmt;
Currently, I have a queries.js file where I have defined:
const getRunsByProjectTable = `
Select pvt();
Execute pvtstmt;
`
However, when I try to make the API call I do not receive any data back:
const getRunsByProjectTable = (req, res) => {
pool.query(queries.getRunsByProjectTable, (error, results) => {
if (error) throw error;
res.status(200).json(results.rows);
})
}
Does anyone know what the issue may be? Any help would be much appreciated!

Related

DBMS_ASSERT.enquote_name() in Oracle

I have a sample code from http://phil-sqltips.blogspot.com/2015/07/beware-of-empty-partitions.html and I'd like to understand this xmlgen sql.
WITH t AS (
SELECT table_owner
, table_name
, partition_name
, TO_NUMBER (EXTRACTVALUE (XMLTYPE (DBMS_XMLGEN.getxml ('SELECT COUNT(*) AS rows_exist FROM '
|| DBMS_ASSERT.enquote_name (str => table_owner)
|| '.'
|| DBMS_ASSERT.enquote_name (str => table_name)
|| ' PARTITION ('
|| DBMS_ASSERT.enquote_name (str => partition_name)
|| ') WHERE ROWNUM <= 1'
)
)
, '/ROWSET/ROW/ROWS_EXIST'
)
) AS rows_exist
FROM all_tab_partitions
WHERE table_owner = 'WH'
AND table_name IN ('POINT_OF_SALE_FACTS')
ORDER BY table_owner
, table_name
, partition_position
)
SELECT 'ALTER TABLE '
|| DBMS_ASSERT.enquote_name (str => table_owner)
|| '.'
|| DBMS_ASSERT.enquote_name (str => table_name)
|| ' DROP PARTITION '
|| DBMS_ASSERT.enquote_name (str => partition_name)
|| ';' AS stmt
, t.*
FROM t
WHERE rows_exist = 0
;
I've found most of them except this,
DBMS_ASSERT.enquote_name (str => table_owner).
What is the syntax of arrow in (str => table_owner) inside of enquote_name?
I found this https://docs.oracle.com/cd/B28359_01/appdev.111/b28419/d_assert.htm#BABDEHBC but there is no such arrow syntax, and couldn't find web sites for this syntax.
That's the named parameter syntax. Here's the relevant documentation. So str => table_name says to set the str parameter to table_name.
In this case, it isn't necessary, since str is the first parameter for DBMS_ASSERT.ENQUOTE_NAME. So you could omit it and just call DBMS_ASSERT.enquote_name(table_owner).
But in some cases it can be very useful. For example, when there are multiple optional parameters (with defaults) and you only want to specify one of them. Or when you want to clarify the purpose of a parameter by displaying the parameter name next to the value.

Oracle Apex Refreshing Chart is not working

I have created a dialog page in my apex application. On this page a user is possible to select several filters for a chart. After clicking on a button the user is redirected to the previous page and the region which contains the chart is refreshed. The problem I have is that setting the filters doesn't work. After being redirected to the previous page the chart is being refreshed endless until an error occurs that says: Bad Gateway. So I don't see any result of the chart. Is it possible that the query of my chart is too complex? The dialog page contains four items of type shuttle and here is the query of my chart.
select COUNT(TRIGGER_TABLE.DATUM_UHRZEIT) as Anzahl_Trigger,
TEST.ID as ID
from BRIDGE_SYSTEM_TRIGGER, SYSTEM_TABLE, TRIGGER_TABLE, FAHRT, TEST, MITARBEITER
where BRIDGE_SYSTEM_TRIGGER.SYSTEM_TABLE_SYSTEM_ID = SYSTEM_TABLE.SYSTEM_ID
and BRIDGE_SYSTEM_TRIGGER.TRIGGER_TABLE_TRIGGER_ID = TRIGGER_TABLE.TRIGGER_ID
and TRIGGER_TABLE.FAHRT_FAHRT_ID = FAHRT.FAHRT_ID
and MITARBEITER.QNUMMER = FAHRT.MITARBEITER_QNUMMER
and FAHRT.TEST_ID = TEST_ID
and TRIGGER_TABLE.PRIORITAET = 0
or ((instr(':' || upper(:P26_Tests) || ':', upper(Test.Test_ID)) > 0)
or (instr(':' || upper(:P26_UEBERSYSTEM) || ':',':' || upper(system_table.system_name) ||':') > 0) or (instr(':' || upper(:P26_UNTERSYSTEM) || ':',':' || upper(system_table.system_name) ||':') > 0)
or (instr(':' || upper(:P26_COUNTRIES) || ':', upper(FAHRT.LAND)) > 0)
or (instr(':' || upper(:P26_REF) || ':', upper(Test.REF)) > 0)
or (instr(':' || upper(:P26_DRIVER) || ':', upper(MITARBEITER.QNUMMER)) > 0))
GROUP BY TEST.ID
ORDER BY TEST_ID;
The query doesn't throw any errors. Another problem I thought about is that something with the refresh is not correctly defined or not working.
I could solve my problem! There was no mistake in the refresh. Although I could not proove why the refresh was not loading, I think it was a runtime-issue because the query was too complex.
I changed the last five or-conditions of my query to and-clauses. After doing that the refresh works again.
select COUNT(TRIGGER_TABLE.DATUM_UHRZEIT) as Anzahl_Trigger,
TEST.ID as ID
from BRIDGE_SYSTEM_TRIGGER, SYSTEM_TABLE, TRIGGER_TABLE, FAHRT, TEST, MITARBEITER
where BRIDGE_SYSTEM_TRIGGER.SYSTEM_TABLE_SYSTEM_ID = SYSTEM_TABLE.SYSTEM_ID
and BRIDGE_SYSTEM_TRIGGER.TRIGGER_TABLE_TRIGGER_ID = TRIGGER_TABLE.TRIGGER_ID
and TRIGGER_TABLE.FAHRT_FAHRT_ID = FAHRT.FAHRT_ID
and MITARBEITER.QNUMMER = FAHRT.MITARBEITER_QNUMMER
and FAHRT.TEST_ID = TEST_ID
and TRIGGER_TABLE.PRIORITAET = 0
and ((instr(':' || upper(:P26_Tests) || ':', upper(Test.Test_ID)) > 0)
and (instr(':' || upper(:P26_UEBERSYSTEM) || ':',':' || upper(system_table.system_name) ||':') > 0) or (instr(':' || upper(:P26_UNTERSYSTEM) || ':',':' || upper(system_table.system_name) ||':') > 0)
and (instr(':' || upper(:P26_COUNTRIES) || ':', upper(FAHRT.LAND)) > 0)
and (instr(':' || upper(:P26_REF) || ':', upper(Test.REF)) > 0)
and (instr(':' || upper(:P26_DRIVER) || ':', upper(MITARBEITER.QNUMMER)) > 0))
GROUP BY TEST.ID
ORDER BY TEST_ID;
The query has definetely to look like that because I want to filter and that's why I need the and-condition. Each element has to be true, otherwise wrong results will appear.

I want to extract the tag name using sql from table with XML stored as clob. (ORACLE)

Sample XML as below:
<Root>
...
<XMLNSC>
...
<SOAP-ENV:Envelope>
...
<SOAP-ENV:Body>
<typesNS0:getABCD xmlns:typesNS0="http://xxx.xx/xxxxxx/xxxxxxxxx/x/" xmlns:xalan="http://xml.apache.org/xslt">
last tag typesNS0:getABCD want to select
select xmltype(a).extract('//typesNS0:getABCD','xmlns:typesNS0="http://xxx.xx/xxxxxx/xxxxxxxxx/x/"')
from (
select
'<Root xmlns:SOAP-ENV="http://a.b.org/">
<XMLNSC />
<SOAP-ENV:Envelope/>
<SOAP-ENV:Body/>
<typesNS0:getABCD xmlns:typesNS0="http://xxx.xx/xxxxxx/xxxxxxxxx/x/" xmlns:xalan="http://xml.apache.org/xslt">123</typesNS0:getABCD>
</Root>' a from dual
)
You may run below script if you want get exact tag value;
DECLARE
vs_Xml VARCHAR2(32000):= '<Root xmlns:SOAP-ENV="http://a.b.org/">
<XMLNSC />
<SOAP-ENV:Envelope/>
<SOAP-ENV:Body/>
<typesNS0:getABCD xmlns:typesNS0="http://xxx.xx/xxxxxx/xxxxxxxxx/x/" xmlns:xalan="http://xml.apache.org/xslt">123</typesNS0:getABCD>
</Root>';
vx_ParameterList XMLTYPE;
vx_Parameter XMLTYPE;
vn_ParameterIndex NUMBER;
vs_Key VARCHAR2(64);
vs_XPath VARCHAR2(255);
vs_Value VARCHAR2(10000);
BEGIN
vx_ParameterList := xmltype(vs_Xml);
vn_ParameterIndex := 1;
vs_XPath := '/Root';
WHILE vx_ParameterList.existsNode(vs_XPath || '[' || vn_ParameterIndex || ']') = 1 LOOP
vx_Parameter := vx_ParameterList.extract(vs_XPath || '[' || vn_ParameterIndex || ']');
vs_Value := vx_Parameter.extract('//typesNS0:getABCD/text()','xmlns:typesNS0="http://xxx.xx/xxxxxx/xxxxxxxxx/x/"').GetStringVal();
vn_ParameterIndex := vn_ParameterIndex + 1;
dbms_output.put_line(vs_Value);
END LOOP;
END;

PL/SQL Numeric or value error: character to number conversion error

Procedure Code:
CREATE OR REPLACE PROCEDURE GetOrder(
p_Order IN NUMBER,
p_OrderDate OUT DATE,
p_CusNbr OUT NUMBER,
p_OrderOut OUT NUMBER,
p_Name OUT VARCHAR2,
p_Address OUT VARCHAR2,
p_City OUT VARCHAR2,
p_State OUT CHAR,
p_Zip OUT NUMBER,
p_Itm OUT NUMBER,
p_Desc OUT VARCHAR2,
p_Qty OUT NUMBER,
p_Price OUT NUMBER)
AS
v_OrderDate ord.ord_date%type;
v_CusNbr ord.Cus_nbr%type;
v_Order ord.Ord_nbr%type;
v_Name cus.cus_nme%type;
v_Address cus.str_adr%type;
v_City zip.city%type;
v_state zip.st%type;
v_Zip zip.zip%type;
v_Itm itm.itm_nbr%type;
v_Desc itm.ITM_DSC%TYPE;
v_Qty ord_itm.ord_Qty%type;
v_Price itm.ord_itm_price%type;
BEGIN
SELECT O.ORD_NBR, C.CUS_NBR, O.ORD_DATE, C.CUS_NME, C.STR_ADR, Z.CITY, Z.ST, I.ITM_NBR, I.ITM_DSC, OI.ORD_QTY, I.ORD_ITM_PRICE
INTO
v_Order, v_CusNbr, v_OrderDate, v_Name, v_Address, v_Zip, v_state, v_Itm, v_Desc, v_Qty, v_Price
FROM ITM I JOIN ORD_ITM OI ON (I.ITM_NBR = OI.ITM_NBR)
JOIN ORD O ON (OI.ORD_NBR = O.ORD_NBR)
JOIN CUS C ON (O.CUS_NBR = C.CUS_NBR)
JOIN ZIP Z ON (C.ZIP_ADR = Z.ZIP)
WHERE O.ORD_NBR = p_Order;
DBMS_OUTPUT.PUT_LINE('ILLINOIS COMPUTERS');
DBMS_OUTPUT.PUT_LINE('CUSTOMER ORDER FORM');
DBMS_OUTPUT.PUT_LINE(v_Order || v_OrderDate || v_CusNbr);
DBMS_OUTPUT.PUT_LINE('Name: ' || v_Name);
DBMS_OUTPUT.PUT_LINE('Address: ' || v_Address);
DBMS_OUTPUT.PUT_LINE('City: ' || v_City || ' State : ' || v_state || ' Zip: ' || v_Zip);
DBMS_OUTPUT.PUT_LINE('Item No.' || 'Description' || 'Quantity' || 'Price');
DBMS_OUTPUT.PUT_LINE(v_Itm || v_Desc || v_Qty || v_Price);
END GetOrder;
/
And when I call the procedure:
DECLARE
p_OrderNbr NUMBER := &ordernumber;
p_OrderDateOutput DATE;
p_CusNbrOutput NUMBER;
p_OrderNbrOutput NUMBER;
p_NameOutput cus.cus_nme%type;
p_AddressOutput cus.str_adr%type;
p_CityOutput zip.city%type;
p_StateOutput CHAR;
p_ZipOutput NUMBER;
p_ItmOutput NUMBER;
p_DescOutput itm.itm_dsc%type;
p_QtyOutput NUMBER;
p_PriceOutput NUMBER;
begin
GetOrder(p_OrderNbr,
p_OrderDateOutput,
p_CusNbrOutput,
p_OrderNbrOutput,
p_NameOutput,
p_AddressOutput,
p_CityOutput,
p_StateOutput,
p_ZipOutput,
p_ItmOutput,
p_DescOutput,
p_QtyOutput,
p_PriceOutput);
END;
/
I'm getting
ORA-06502 error: numeric or value error: character to number
conversion error.
I believe all my parameters are in order so I'm not getting conflicting variable assignments.
I ran this query seperately to make sure I'm getting the right output and it works fine.
SELECT O.ORD_NBR, C.CUS_NBR, O.ORD_DATE, C.CUS_NME, C.STR_ADR, Z.CITY, Z.ST, I.ITM_NBR, I.ITM_DSC, OI.ORD_QTY, I.ORD_ITM_PRICE
--INTO
--v_Order, v_CusNbr, v_OrderDate, v_Name, v_Address, v_Zip, v_state, v_Itm, v_Desc, v_Qty, v_Price
FROM ITM I JOIN ORD_ITM OI ON (I.ITM_NBR = OI.ITM_NBR)
JOIN ORD O ON (OI.ORD_NBR = O.ORD_NBR)
JOIN CUS C ON (O.CUS_NBR = C.CUS_NBR)
JOIN ZIP Z ON (C.ZIP_ADR = Z.ZIP)
WHERE O.ORD_NBR = 100;
Select list says
... C.STR_ADR, Z.CITY, Z.ST, ...
Into list says
... v_Address, v_Zip, v_state ...
Seems you got the wrong field in Z table.
Unless Z.CITY is the numeric zip code?

owa_util.mime_header Issue - Trying to extract a Excel File from a query

'm having a problem with my one procedure that suppose to generate an Excel file, the procedure is this one:
CREATE OR REPLACE PROCEDURE GENERATE_REPORT_P(P_CONTRACT_NUM IN VARCHAR2 DEFAULT NULL,
P_CUSTOMER_NAME IN VARCHAR2 DEFAULT NULL,
P_CUSTOMER_NUM IN VARCHAR2 DEFAULT NULL,
P_UPDATE_DATE_START IN DATE DEFAULT NULL,
P_UPDATE_DATE_END IN DATE DEFAULT NULL,
P_ORDER_NUM IN VARCHAR2 DEFAULT NULL) IS
vRecords BOOLEAN := FALSE;
BEGIN
FOR i IN (SELECT
a.contract_num,
d.customer_abt_number,
d.customer_name,
c.doms_order_num order_num,
e.service_tag asset,
e.sku,
e.field,
e.previous_value,
e.new_value,
e.update_date, --to_char(i.update_date, 'DD-MON-RRRR HH12:MI:SS')
e.updated_by
FROM contracts a,
order_extension b,
orders c,
customers d,
abt_cisi_log e
WHERE a.id = b.contract_id
AND c.id = b.order_id
AND d.id = a.customer_id
AND c.id = e.order_id
--parameters
AND a.contract_num LIKE NVL(p_contract_num, a.contract_num)
AND d.customer_abt_number LIKE
NVL(p_customer_num, d.customer_abt_number)
AND d.customer_name LIKE
NVL(p_customer_name, d.customer_name)
AND e.update_date between
NVL(to_date(p_update_date_start, 'dd-mon-rrrr'), '01-JAN-1900') AND
NVL(to_date(p_update_date_end, 'dd-mon-rrrr'), '31-DEC-2199')
AND c.doms_order_num LIKE
NVL(p_order_num, c.doms_order_num)
ORDER BY a.contract_num,
c.doms_order_num,
e.service_tag,
e.sku) LOOP
IF NOT vRecords THEN
vRecords := TRUE;
owa_util.mime_header(ccontent_type => 'application/vnd.ms-excel');
htp.htmlopen;
htp.bodyopen;
htp.tableopen(cattributes => 'border=1');
htp.tablerowopen;
htp.p('<td><b>Contract</b></td>');
htp.p('<td><b>Customer Number</b></td>');
htp.p('<td><b>Customer</b></td>');
htp.p('<td><b>Order#</b></td>');
htp.p('<td><b>Asset</b></td>');
htp.p('<td><b>SKU</b></td>');
htp.p('<td><b>Field</b></td>');
htp.p('<td><b>Previous Value</b></td>');
htp.p('<td><b>New Value</b></td>');
htp.p('<td><b>Update Date</b></td>');
htp.p('<td><b>Updated By</b></td>');
htp.tablerowclose;
END IF;
htp.tablerowopen;
htp.p('<td>' || i.contract_num || '</td>');
htp.p('<td>' || i.customer_abt_number || '</td>');
htp.p('<td>' || i.customer_name || '</td>');
htp.p('<td>' || i.order_num || '</td>');
htp.p('<td>' || i.asset || '</td>');
htp.p('<td>' || i.sku || '</td>');
htp.p('<td>' || i.field || '</td>');
htp.p('<td>' || i.previous_value || '</td>');
htp.p('<td>' || i.new_value || '</td>');
htp.p('<td>' || to_char(i.update_date, 'DD-MON-RRRR HH12:MI:SS') ||
'</td>');
htp.p('<td>' || i.updated_by || '</td>');
htp.tablerowclose;
END LOOP;
IF vRecords THEN
htp.tableclose;
htp.bodyclose;
htp.htmlclose;
END IF;
END GENERATE_REPORT_P;
But when i execute this procedure by
Begin
GENERATE_REPORT_P();
END;
I ALWAYS get this error:
I was trying to figure it out, but not much luclk until now, I've tryed to comment all the code into the procedure and just call the owa_util.mime_header(ccontent_type => 'application/vnd.ms-excel'); To see what happens, and checked the data type from my table, well, pretty much all the basic stuff.
If anyone could give me some help, ou tip, i really appreciate that.
My Regards!
Note: I extract the procedure from a packge, and ajust one or two things just to place at StackOverflow, so, i u see the error message and figere it out, my bad!
To resolve this particular issue put this piece of code somewhere before the call to generate_report_p:
owa.num_cgi_vars := NVL(owa.num_cgi_vars, 0);