how to declare a variable in pl sql the right way? - oracle

here is my code
vJS VARCHAR2(3500); --25065
gMaxDays s_criteria%rowtype := get_criteria_rec('MAX_DAYS_BOOKING');
BEGIN
IF NOT Sec_Pkg.chk_sec('ATLAS_INV_OVERBOOK') THEN
-- Exit procedure if security did not pass
RETURN;
END IF;
--
vJS := ' gMaxDays;'||CHR(10)||
'function checkfields() {' ||CHR(13) ||
' // Setting the target here' ||CHR(13) ||
' document.frmInvSelect.target="_top"' ||CHR(13) ||
i created gMaxDays,at first i had it hardcoded but now its on a table called s_criteria and MAX_DAYS_BOOKING is part of s_criteria. im i calling it the right way?
this is how it use to look and it work
vJS VARCHAR2(3500); --25065
BEGIN
IF NOT Sec_Pkg.chk_sec('ATLAS_INV_OVERBOOK') THEN
-- Exit procedure if security did not pass
RETURN;
END IF;
--
vJS := 'var gMaxDays = 366;'||CHR(10)||
'function checkfields() {' ||CHR(13) ||
' // Setting the target here' ||CHR(13) ||
' document.frmInvSelect.target="_top"' ||CHR(13) ||
[/code]

Err ... Are you saying that
You used to have a code snippet that "worked":
vJS := 'var gMaxDays = 366;'||CHR(10)||
'function checkfields() {' ||CHR(13) ||
' // Setting the target here' ||CHR(13) ||
' document.frmInvSelect.target="_top"' ||CHR(13) ||
When you modified the snippet to be:
vJS := ' gMaxDays;'||CHR(10)||
'function checkfields() {' ||CHR(13) ||
' // Setting the target here' ||CHR(13) ||
' document.frmInvSelect.target="_top"' ||CHR(13) ||
It "works" no more ?
I guess you're trying to generate a JavaScript code snippet. Maybe this is what you're looking for:
declare
vJS VARCHAR2(3500);
gMaxDays s_criteria%rowtype := get_criteria_rec('MAX_DAYS_BOOKING');
begin
-- here you have to call the correct field from the record
-- in this example I assume it's max_day
vJS := 'var gMaxDays = ' || gMaxDays.max_day || ';' || CHR(10)||
'function checkfields() {' || CHR(10) ||
' // Setting the target here' || CHR(10) ||
' document.frmInvSelect.target="_top"' || CHR(10) ||
' // Add here something that makes this a valid JavaScript.'
end;
Check what the documentation says about PL/SQL record variables.

Related

CSV File generation in Oracle

I am new to oracle and here I am trying generate a CSV file from the values from my table and mail it. I am able to generate the csv file but I am unable to add a heading to my file and my date values are being shown as ########. I want the CSV file to be generated as the expected one .
The expected CSV :
The generated CSV :
Here is my code :
l_attach_text_h :=
'RECORD_ID ,INPUTTED DATE ,INPUTTED BY ,BROKER .....
FOR employee_rec in c1
LOOP
l_attach_text := '"' ||
employee_rec.FC_ED_RECORD_ID || '","' ||
employee_rec.FC_ED_UPLOADTIME || '","' ||
employee_rec.FC_ED_USER_ID || '","' ||
employee_rec.FC_ED_BROKER || '","' ||
........
l_clob := l_clob||chr(10)||l_attach_text;
END LOOP;
l_clob := l_attach_text_h ||chr(13)|| l_clob;
DBMS_OUTPUT.put_line(' Dtls processing completed...');
v_Mail_Conn := utl_smtp.Open_Connection(v_Mail_Host, 25);
utl_smtp.Helo(v_Mail_Conn, v_Mail_Host);
utl_smtp.Mail(v_Mail_Conn, v_From);
utl_smtp.Rcpt(v_Mail_Conn, v_Recipient);
utl_smtp.Data(v_Mail_Conn,
'Date: ' || to_char(sysdate, 'Dy, DD Mon YYYY hh24:mi:ss') || crlf ||
'From: ' || v_From || crlf ||
'Subject: '|| v_Subject || crlf ||
'To: ' || v_Recipient || crlf ||
'MIME-Version: 1.0'|| crlf || -- Use MIME mail standard
'Content-Type: multipart/mixed;'|| crlf ||
' boundary="-----SECBOUND"'|| crlf ||
crlf ||
'-------SECBOUND'|| crlf ||
'Content-Type: text/plain;'|| crlf ||
'Content-Transfer_Encoding: 7bit'|| crlf ||
crlf ||
'Please find the following in the attachments :'|| crlf || -- Message body
'Entry details & Entry details'|| crlf ||
crlf ||
'-------SECBOUND'|| crlf ||
'Content-Type: text/plain;'|| crlf ||
' name="Files.csv"'|| crlf ||
'Content-Transfer_Encoding: 8bit'|| crlf ||
'Content-Disposition: attachment;'|| crlf ||
' filename="Files.csv"'|| crlf ||
crlf ||
l_clob || crlf || -- Content of attachment
crlf ||
'-------SECBOUND--' -- End MIME mail
);
utl_smtp.Quit(v_mail_conn);
DBMS_OUTPUT.put_line('mail send completed...');
can anyone help me with this?
As of the heading: create a new comma-separated line which contains all headings, e.g.
SQL> select '"Record ID"' ||','|| '"Inputted Date"' ||','|| '"Inputted By"' as heading from dual;
HEADING
-----------------------------------------
"Record ID","Inputted Date","Inputted By"
SQL>
and include it as the 1st line in the output file.
As of the ###### issue: are you sure it is a problem? What happens when you double-click in between columns B and C in Excel? That operation should adjust column B width so that it matches data width (so I'd expect actual data to be seen afterwards).

Converting IIF statement to Decode oracle

How do I convert the following IIF statement to oracle decode statement :
IIF(lkp_LAST_NAME1 = 'Unknown', lkp_LAST_NAME1,
IIF(lkp_FIRST_NAME1 =' ' OR lkp_MIDDLE_NAME1 = ' ',lkp_LAST_NAME1,
IIF(ISNULL(lkp_LAST_NAME1), lkp_FIRST_NAME1 || ' ' || lkp_MIDDLE_NAME1,
IIF((ISNULL(lkp_FIRST_NAME1) AND ISNULL(lkp_MIDDLE_NAME1)), lkp_LAST_NAME1,
lkp_LAST_NAME1 || ', ' || lkp_FIRST_NAME1 || ' ' || lkp_MIDDLE_NAME1))))
CASE should take care of it
case
when lkp_LAST_NAME1 = 'Unknown' then lkp_LAST_NAME1
else
case when lkp_FIRST_NAME1 =' ' OR lkp_MIDDLE_NAME1 = ' ' then lkp_LAST_NAME1
else
case when lkp_LAST_NAME1 is null then lkp_FIRST_NAME1 || ' ' || lkp_MIDDLE_NAME1
else
case when lkp_FIRST_NAME1 is null and lkp_MIDDLE_NAME1 is null then lkp_LAST_NAME1
else lkp_LAST_NAME1 || ', ' || lkp_FIRST_NAME1 || ' ' || lkp_MIDDLE_NAME1
end
end
end
end

using a loop for fetching the output but i don't want multiple outputs I need only a single output

Can I get a single output by using loop in pl/sql instead of multiple outputs?
Below is my query and the output I am getting.
declare
CURSOR VALIGN_DAILY IS
SELECT AUDT.WKFL_NM,
AUDT.JOB_STAT,
AUDT.STRT_DT,
AUDT.END_DT,
AUDT.SRC_REC_CNT,
AUDT.TGT_REC_CNT,
AUDT.ERR_REC_CNT
FROM CRMF_AUDT_PROC_LOG AUDT
WHERE AUDT.WKFL_NM IN ('wf_ALIGNMENT_TERR_FIELDFORCE_EZFLEX_INB','wf_ALIGNMENT_USER_TERR_EZFLEX_INB','wf_ALIGNMENT_FIELDFORCE_STG_INB','wf_ALIGNMENT_TERR_STG_INB','wf_ALIGNMENT_USER_TERR_STG_INB')
AND (AUDT.JOB_STAT = 'S' OR AUDT.JOB_STAT = 'P' OR AUDT.JOB_STAT = 'E' OR AUDT.JOB_STAT = 'F')
AND TRUNC(AUDT.STRT_DT)=TRUNC(SYSDATE);
VALIGNSTATUS VALIGN_DAILY%ROWTYPE;
BEGIN
FOR VALIGNSTATUS IN VALIGN_DAILY LOOP
CASE VALIGNSTATUS.JOB_STAT
WHEN 'S' THEN DBMS_OUTPUT.PUT_LINE('VALIGN_DAILY' || ' ' || 'COMPLETED' || ' ' || VALIGNSTATUS.STRT_DT || ' ' || VALIGNSTATUS.END_DT || ' ' || VALIGNSTATUS.WKFL_NM);
WHEN 'P' THEN DBMS_OUTPUT.PUT_LINE('VALIGN_DAILY' || ' ' || 'IN_PROGRESS' || ' ' || VALIGNSTATUS.STRT_DT || ' ' || VALIGNSTATUS.END_DT || ' ' || VALIGNSTATUS.WKFL_NM);
WHEN 'E' THEN DBMS_OUTPUT.PUT_LINE('VALIGN_DAILY' || ' ' || 'ERRED' || ' ' || VALIGNSTATUS.STRT_DT || ' ' || VALIGNSTATUS.END_DT || ' ' || VALIGNSTATUS.WKFL_NM);
WHEN 'F' THEN DBMS_OUTPUT.PUT_LINE('VALIGN_DAILY' || ' ' || 'FAILED' || ' ' || VALIGNSTATUS.STRT_DT || ' ' || VALIGNSTATUS.END_DT || ' ' || VALIGNSTATUS.WKFL_NM);
ELSE
DBMS_OUTPUT.PUT_LINE ('VALIGN_DAILY' || ' ' || 'NOT_STARTED' || ' ' || 'N/A' || ' ' || 'N/A');
END CASE;
END LOOP;
end;
And my output is like--
VALIGN_DAILY COMPLETED 28-MAY-2018 28-MAY-2018 wf_ALIGNMENT_FIELDFORCE_STG_INB
VALIGN_DAILY COMPLETED 28-MAY-2018 28-MAY-2018 wf_ALIGNMENT_USER_TERR_EZFLEX_INB
VALIGN_DAILY COMPLETED 28-MAY-2018 28-MAY-2018 wf_ALIGNMENT_TERR_FIELDFORCE_EZFLEX_INB
VALIGN_DAILY COMPLETED 28-MAY-2018 28-MAY-2018 wf_ALIGNMENT_USER_TERR_STG_INB
VALIGN_DAILY COMPLETED 28-MAY-2018 28-MAY-2018 wf_ALIGNMENT_TERR_STG_INB

SQL query returning rows while executing it on SQL dedveloper but is not working in the PLSQL procedure

This is the procedure
PROCEDURE NON_BLOCKING_STATUS
IS
cursor badcur is
select query;
BEGIN
dbms_output.put_line('sch_id sch_revision SCD_SEQUENCE SCH_STATUS scD_status scd_action scd_object SCD_NAME scl_text');
for badrow in badcur LOOP
dbms_output.put_line ( badrow.sch_id || ' ' || badrow.sch_revision || ' ' || badrow.SCD_SEQUENCE || ' ' || badrow.SCH_STATUS || ' ' || badrow.scD_status || ' ' || badrow.scd_action || ' ' || badrow.scd_object || ' ' || badrow.SCD_NAME || ' ' || badrow.scl_text);
end loop;
end;
The select sql query is returning rows while running it on SQL developer but while using it in the above procedure, its not working.
Thanks in Advance.

Invalid reference of variable 'v_date_extraction'

CREATE OR REPLACE PROCEDURE bcy_genera_file_cedacri_G2 (p_errbuff OUT VARCHAR2, p_errcode OUT NUMBER, p_data_lancio IN date, p_directory IN VARCHAR2) is
file_csv utl_file.file_type;
v_src_file BFILE;
v_content BLOB;
g_application varchar2(3) := 'OIC';
g_ambient varchar2(4) := 'TEST';
g_ret_code_exception_value number := 2;
g_ret_code_exception number := 0;
v_date_extraction varchar2(15); --yyyymmddhhMM ..
creazione_file_except exception;
v_p_date_launching date;
cursor cur_csv is
SELECT flag_pubblicato, data_pubblicazione
FROM XXBCYIN.bcy_supporto_garante_2 bsg2;
BEGIN
v_date_extraction:= to_char (nvl(p_date_launching, sysdate), 'yyyymmddhhMM');
BEGIN
--to create file csv
file_csv := utl_file.fopen ('p_directory', 'GARANTE2||g_application||g_ambient||v_date_extraction.csv', 'W');
FOR r IN cur_csv
LOOP
utl_file.put_line (
file_csv,
--cur_csv.ID || ';' ||
--cur_csv.TRX_TYPE || ';' ||
'USER_ID' || ';' ||
'USERNAME' || ';' ||
'DOMINIO' || ';' ||
'TIMESTAMP' || ',' ||
'CODICE_POSTAZIONE_1' || ';' ||
'CODICE_POSTAZIONE_2' || ';' ||
'CODICE_POSTAZIONE_3' || ';' ||
'CODICE_POSTAZIONE_4' || ';' ||
'CODICE_POSTAZIONE_5'|| ';' ||
'NDG_CLIENTE' || ';' ||
'CODICE_SERVIZIO' || ';' ||
'CODICE_ABI' || ';' ||
'CODICE_ISTITUTO' || ';' ||
'CODICE_CAB_OPERATORE' || ';' ||
'CODICE_OPERATORE' || ';' ||)
END LOOP;
utl_file.fclose (file_csv);
v_src_file:= 'GARANTE2'||g_application||g_ambient||v_date_extraction.csv;
At the last line I get the error :
PLS-00487 invalid reference to variable 'v_date_extraction'.
This variable is defined
v_date_extraction:= to_char (nvl(p_date_launching, sysdate), 'yyyymmddhhMM');
where v_date_extraction is a varchar2 and p_data_lancio and sysdate are date. This may be the problem?
The problem appears to be that you failed to put ".csv" in quotes. I believe the line raising the error should be:
v_src_file:= 'GARANTE2'||g_application||g_ambient||v_date_extraction||'.csv';

Resources