PL/SQL VARCHAR2 Max Length - oracle11g

I can't seem to use a VARCHAR2 of more than 4000 chars in 11G, but I see that I should be able to get to 32776.
PROCEDURE prcName
(piCoSite IN CHAR
,piRaciId IN NUMBER
,piDept IN VARCHAR2
,poRecordset OUT SYS_REFCURSOR) AS
locSql1 VARCHAR2(32000 CHAR);
BEGIN
locSql1 :=
'SELECT' ||
' H.TABLE_HEAD_ID ' ||
' ,H.TABLE_HEAD_DEPT ' ||
' ,H.TABLE_HEAD_DESCRIPTION ' ||
' ,H.TABLE_HEAD_STATUS ' ||
' ,H.TABLE_HEAD_SCOPE ' ||
' ,H.TABLE_HEAD_ORIGINATOR ' ||
' ,H.TABLE_HEAD_SUB_CATEGORY ' ||
' ,H.TABLE_HEAD_HIGH_RISK ' ||
' ,TRIM(C0.CNTCT_FIRSTNAME) || '' '' || TRIM(C0.CNTCT_SURNAME) wsRaisedBy ' ||
' ,(SELECT COUNT(*) FROM TABLE_EMPLOYEES E WHERE E.TABLE_EMPS_CO_SITE = H.TABLE_HEAD_CO_SITE AND E.TABLE_EMPS_ID = H.TABLE_HEAD_ID) wsNoEmployees ' ||
' ,(SELECT COUNT(*) FROM TABLE_TASKS T WHERE T.TABLE_TASK_CO_SITE = H.TABLE_HEAD_CO_SITE AND T.TABLE_TASK_ID = H.TABLE_HEAD_ID) wsNoActions ' ||
' FROM TABLE_HEADER H ' ||
' LEFT JOIN CONTACTS C0 ' ||
' ON C0.CNTCT_CO_SITE = H.TABLE_HEAD_CO_SITE ' ||
' AND C0.CNTCT_CODE = H.TABLE_HEAD_ORIGINATOR ' ||
' WHERE H.TABLE_HEAD_CO_SITE = ''' || piCoSite || '''' ||
' AND (H.TABLE_HEAD_ID = ''' || piRaciId || '''' || ' OR ''' || piRaciId || ''' = 0) ' ||
' AND (H.TABLE_HEAD_DEPT IN (''' || piDept || ''' ) OR ''' || piDept || ''' IS NULL ) ' ||
' AND (H.TABLE_HEAD_ORIGINATOR IN (' || piUser || ' ) OR ''' || piUser || ''' IS NULL ) ' ||
' AND (H.TABLE_HEAD_STATUS IN (' || piStatus || ' ) OR ''' || piStatus || ''' IS NULL ) ' ||
' ORDER BY TABLE_HEAD_ID ';
dbms_output.put_line(locSql1);
OPEN poRecordset FOR locSql1;
When I copy/paste the locSql1 variable it's nowhere near 32000 chars, but it's getting truncated.
Is there something within the Database to change or am I missing something?
Thanks.

Related

Dynamic Cursor with parameterised schema name and using for BULK INSERT in target table

I have source_table in different 22 schemas and need procedure to create for bulk collect and insert into same target table in oracle stored procedure.
I'm trying and not getting records inserted getting error ORA-00911: invalid character but there is all column from select cursor and traget_table are same in order.
CREATE OR REPLACE PROCEDURE proc_bulk_circle(p_limit IN PLS_INTEGER DEFAULT 10000,
p_activity_date IN DATE,
p_circle IN VARCHAR2) AS
CURSOR act_cur IS
SELECT activity_date,
circle
FROM circle_load_control
WHERE activity_date = p_activity_date
AND circle = circle;
TYPE type_i6 IS TABLE OF act_cur%ROWTYPE INDEX BY BINARY_INTEGER;
i_tab6 type_i6;
v_count NUMBER := 0;
lv_circle VARCHAR2(2);
lv_schema VARCHAR2(20);
TYPE rc IS REF CURSOR;
con_sap_cur rc;
TYPE con_sap_resp IS TABLE OF target_table%ROWTYPE INDEX BY BINARY_INTEGER;
i_tab1 con_sap_resp;
lv_sql_stmt VARCHAR2(32767);
BEGIN
IF p_circle = 'MUM'
THEN
lv_circle := 'MU';
lv_schema := 'MUMBAI';
ELSIF p_circle = 'MAH'
THEN
lv_circle := 'MH';
lv_schema := 'MHRSTR';
ELSE
lv_circle := NULL;
END IF;
FOR myindex IN act_cur
LOOP
i_tab6(v_count) := myindex;
v_count := v_count + 1;
END LOOP;
FOR myindex IN i_tab6.first .. i_tab6.last
LOOP
IF i_tab6(myindex).activity_date = p_activity_date
AND i_tab6(myindex).circle = p_circle
THEN
BEGIN
lv_sql_stmt := 'SELECT acc_id code,
cust_id c_id,
addr_1 address2,
addr_2 address3,
addr_3 address4,
(SELECT SUM(abc) FROM ' || lv_schema || '.details WHERE <some condition with t1> GROUP BY <columns>) main_charges,
(SELECT SUM(extra_charge) FROM ' || lv_schema || '.details WHERE <some condition with t1> GROUP BY <columns>) extra_charges
FROM ' || lv_schema || '.main_source_details t1
WHERE t1.activity_date = ''' || p_activity_date || ''';';
OPEN con_sap_cur FOR lv_sql_stmt;
LOOP
FETCH con_sap_cur BULK COLLECT
INTO i_tab1 LIMIT p_limit;
FORALL i IN 1 .. i_tab1.count
INSERT INTO target_table (column list....)
VALUES(I_TAB1(i).col1,......;
EXIT WHEN con_sap_cur%NOTFOUND;
END LOOP;
COMMIT;
CLOSE con_sap_cur;
EXCEPTION
WHEN OTHERS THEN
dbms_output.put_line('ERR target_table: ' || SQLCODE || '-' || SQLERRM);
END;
ELSE
dbms_output.put_line(p_activity_date || ' DATE IS NOT MATCH');
END IF;
END LOOP;
EXCEPTION
WHEN OTHERS THEN
dbms_output.put_line(SQLCODE || ' ' || SQLERRM);
END proc_bulk_circle;
/
I believe this comes down to you having a ; in your definition of the sql (see below line)
WHERE t1.activity_date = ''' || p_activity_date || ''';';
when you are defining SQL for dynamic use (and opening a cursor this way is dynamic) you do not include the ;
To show this I have done a shorter example. The below will error in the same way as yours.
declare
v_sql varchar2(100) default 'select ''X'' from dual;';
TYPE rc IS REF CURSOR;
v_cur rc;
type l_tab_type is table of varchar2(1);
l_tab l_tab_type;
begin
open v_cur for v_sql;
loop
fetch v_cur bulk collect into l_tab;
exit;
end loop;
CLOSE v_cur;
end;
/
but simply remove the ; from the line
v_sql varchar2(100) default 'select ''X'' from dual;';
end it all works fine, fixed example below.
declare
v_sql varchar2(100) default 'select ''X'' from dual';
TYPE rc IS REF CURSOR;
v_cur rc;
type l_tab_type is table of varchar2(1);
l_tab l_tab_type;
begin
open v_cur for v_sql;
loop
fetch v_cur bulk collect into l_tab;
exit;
end loop;
CLOSE v_cur;
end;
/
You're doing an awful lot of work here, if your purpose is to insert some rows.
Instead, you could do the insert and select in one go, something like:
CREATE OR REPLACE PROCEDURE proc_bulk_circle(p_activity_date IN DATE,
p_circle IN VARCHAR2) AS
lv_circle VARCHAR2(2);
lv_schema VARCHAR2(20);
v_query CLOB;
e_table_does_not_exist EXCEPTION;
PRAGMA EXCEPTION_INIT(e_table_does_not_exist, -00942);
BEGIN
IF p_circle = 'MUM'
THEN
lv_circle := 'MU';
lv_schema := 'MUMBAI';
ELSIF p_circle = 'MAH'
THEN
lv_circle := 'MH';
lv_schema := 'MHRSTR';
END IF;
IF lv_schema IS NOT NULL
THEN
-- asserting the schema name to avoid sql injection
-- also using a bind variable for the activity_daate predicates
v_query := 'INSERT INTO target_table (<column list>)' || CHR(10) ||
' WITH main_dets AS (SELECT acc_id,' || CHR(10) ||
' cust_id,' || CHR(10) ||
' addr_1,' || CHR(10) ||
' addr_2,' || CHR(10) ||
' addr_3,' || CHR(10) ||
' (SELECT SUM(abc) FROM ' || dbms_assert.simple_sql_name(lv_schema) || '.details WHERE <some condition with t1>) main_charges,' || CHR(10) || -- no need for the group by
' (SELECT SUM(extra_charge) FROM ' || dbms_assert.simple_sql_name(lv_schema) || '.details WHERE <some condition with t1>) extra_charges' || CHR(10) || -- no need for the group by
' FROM ' || dbms_assert.simple_sql_name(lv_schema) || '.main_source_details t1' || CHR(10) ||
' WHERE activity_date = :p_activity_date)' || CHR(10) ||
' circles AS (SELECT activity_date,' || CHR(10) ||
' circle' || CHR(10) ||
' FROM circle_load_control' || CHR(10) ||
' WHERE activity_date = :p_activity_date' || CHR(10) ||
' AND circle = circle)' || CHR(10) || -- did you really mean circle = circle here? This is equivalent to 1=1 (unless circle is null) and is therefore pretty irrelevant! If you want to exclude rows with null values, use "circle is not null" instead
' SELECT md.acc_id,' || CHR(10) ||
' md.cust_id,' || CHR(10) ||
' md.addr_1,' || CHR(10) ||
' md.addr_2,' || CHR(10) ||
' md.addr_3,' || CHR(10) ||
' md.main_charges,' || CHR(10) ||
' md.extra_charges' || CHR(10) ||
' FROM main_dets md' || CHR(10) ||
' CROSS JOIN circles c';
EXECUTE v_query USING p_activity_date, p_activity_date;
COMMIT;
ELSE
raise_application_error(-20001, 'Invalid circle specified: "' || p_circle || '"');
END IF;
END proc_bulk_circle;
/
(N.B. untested.)
I've assumed that activity_date and circle in circle_load_control aren't unique; if they are, you could avoid the cross join and just have an implicit cursor to fetch the row prior to doing the IF p_circle = ... checks.

ORA-06502: PL/SQL: numeric or value error: character string buffer

I got the following error when running my ColdFusion 2016 application:
The error says:
[Macromedia][Oracle JDBC Driver][Oracle]ORA-06502: PL/SQL: numeric or value error: character string buffer too small ORA-06512: at "PROC_CHECKDATA", line 1064 ORA-06512: at line 1
It's has something to do with calling a stored procedure and running it. This codes works for years when I ran it in ColdFusion 8 but once I moved to ColdFusion 2016 it errors out. I don't quite understand what is this error saying. I appreciate any help I can get. Thank you so much.
Here is my procedure call:
<cfstoredproc procedure="PROC_CHECKDATA" datasource="#application.DSN#">
<cfprocparam type="in" cfsqltype="CF_SQL_CHAR" DBVARNAME="ins" value="#url.ins#" MAXLENGTH="2">
<cfprocparam type="in" cfsqltype="CF_SQL_CHAR" DBVARNAME="ly" value="#url.ly#" MAXLENGTH="4">
<cfprocparam type="inout" cfsqltype="CF_SQL_VARCHAR" DBVARNAME="summary" variable="summary" value="">
<cfprocparam type="inout" cfsqltype="CF_SQL_VARCHAR" DBVARNAME="continue" variable="continue" value="">
<cfprocresult name="errors" resultset="1">
</cfstoredproc>
And this is the stored procedure (in Oracle 11g):
create or replace PROCEDURE proc_checkparentdata (
v_institution IN CHAR DEFAULT NULL,
v_load_year IN CHAR DEFAULT NULL,
v_summary OUT VARCHAR2, /* DEFAULT ' '*/
v_continue OUT VARCHAR2, /* DEFAULT ' '*/
cv_1 OUT SYS_REFCURSOR)
AS
v_rowcount NUMBER (10, 0);
v_errorcount NUMBER (5, 0);
BEGIN
-- clear comment
UPDATE um_parent_temp
SET comments = ' '
WHERE load_year = v_load_year AND institution = v_institution;
-- clear status
UPDATE um_parent_temp
SET status = ' '
WHERE load_year = v_load_year AND institution = v_institution;
-- campus code
UPDATE um_parent_temp
SET comments =
(CASE comments WHEN ' ' THEN ' ' ELSE comments || '<br>' END)
|| 'Campus code: '
|| institution
WHERE institution NOT IN ('BC', 'CP', 'ES', 'FS', 'SU', 'TU', 'UC')
AND load_year = v_load_year
AND institution = v_institution;
/* parent 1 */
-- firstname
UPDATE um_parent_temp
SET comments =
(CASE comments WHEN ' ' THEN ' ' ELSE comments || '<br>' END)
|| 'Parent firstname1 is missing'
WHERE NVL (trim(parent_fn1), ' ') = ' '
AND load_year = v_load_year
AND institution = v_institution;
-- lastname
UPDATE um_parent_temp
SET comments =
(CASE comments WHEN ' ' THEN ' ' ELSE comments || '<br>' END)
|| 'Parent_lastname1 is missing'
WHERE NVL (trim(parent_ln1), ' ') = ' '
AND load_year = v_load_year
AND institution = v_institution;
-- lastname1 too short
UPDATE um_parent_temp
SET comments =
(CASE comments WHEN ' ' THEN ' ' ELSE comments || '<br>' END)
|| 'Parent_lastname1 is too short'
WHERE Length(trim(parent_ln1)) = 1
AND load_year = v_load_year
AND institution = v_institution;
-- maximum label name = 60; includes three spaces
UPDATE um_parent_temp
SET comments =
(CASE comments WHEN ' ' THEN ' ' ELSE comments || '<br>' END)
|| 'Parent mailname1 is over 60 characters'
WHERE lengthc (nvl(trim(parent_prefix1),'') ||
nvl(trim(parent_fn1),'') || nvl(trim(parent_mn1),'') ||
nvl(trim(parent_ln1),'')) > 37
AND load_year = v_load_year
AND institution = v_institution;
-- prefix
UPDATE um_parent_temp
SET comments =
(CASE comments WHEN ' ' THEN ' ' ELSE comments || '<br>' END)
|| 'Prefix1: '
|| parent_prefix1
WHERE NVL (trim(parent_prefix1), ' ') = ' '
OR (NVL (trim(parent_prefix1), ' ') <> ' '
AND parent_prefix1 NOT IN
(SELECT gl_prefixes.campprefix FROM gl_prefixes)
AND load_year = v_load_year
AND institution = v_institution);
-- suffixPers
UPDATE um_parent_temp
SET comments =
(CASE comments WHEN ' ' THEN ' ' ELSE comments || '<br>' END)
|| 'Personal suffix1: '
|| parent_suffix_pers1
WHERE NVL (trim(parent_suffix_pers1), ' ') <> ' '
AND parent_suffix_pers1 NOT IN
(SELECT gl_suffixes.campsuffix FROM gl_suffixes)
AND load_year = v_load_year
AND institution = v_institution;
-- suffixProf
UPDATE um_parent_temp
SET comments =
(CASE comments WHEN ' ' THEN ' ' ELSE comments || '<br>' END)
|| 'Profession suffix1: '
|| parent_suffix_prof1
WHERE NVL (trim(parent_suffix_prof1), ' ') <> ' '
AND parent_suffix_prof1 NOT IN
(SELECT gl_suffixes.campsuffix FROM gl_suffixes)
AND load_year = v_load_year
AND institution = v_institution;
-- race
UPDATE um_parent_temp
SET comments =
(CASE comments WHEN ' ' THEN ' ' ELSE comments || '<br>' END)
|| 'Race1: '
|| race1
WHERE NVL (trim(race1), ' ') <> ' '
AND race1 NOT IN (SELECT campcode
FROM gl_races
WHERE campuscode = v_institution)
AND load_year = v_load_year
AND institution = v_institution;
-- sex
UPDATE um_parent_temp
SET comments =
(CASE comments WHEN ' ' THEN ' ' ELSE comments || '<br>' END)
|| 'Sex code1: '
|| sex1
WHERE NVL (trim(sex1), ' ') NOT IN ('M', 'F')
AND load_year = v_load_year
AND institution = v_institution;
UPDATE um_parent_temp
SET comments =
(CASE comments WHEN ' ' THEN ' ' ELSE comments || '<br>' END)
|| 'Mismatching gender1 and prefix1: '
|| parent_prefix1
|| ' <---> '
|| sex1
WHERE ( (sex1 = 'M'
AND trim(parent_prefix1) IN ('Mrs.', 'Mrs', 'Miss.', 'Miss', 'Ms.',
'Ms'))
OR (trim(sex1) = 'F' AND trim(parent_prefix1) IN ('Mr.', 'Mr')))
AND load_year = v_load_year
AND institution = v_institution;
-- address
UPDATE um_parent_temp
SET comments =
(CASE comments WHEN ' ' THEN ' ' ELSE comments || '<br>' END)
|| 'Preferred address flag1: '
|| pref_addr1
|| '<br>'
|| 'note: must also have unlisted flag set for preferred addr'
WHERE trim(pref_addr1) NOT IN ('H', 'B')
AND load_year = v_load_year
AND institution = v_institution;
UPDATE um_parent_temp
SET comments =
(CASE comments WHEN ' ' THEN ' ' ELSE comments || '<br>' END)
|| 'Home address1_1 is empty'
WHERE pref_addr1 = 'H'
AND NVL (trim(home_addr1_1), ' ') = ' '
AND load_year = v_load_year
AND institution = v_institution;
UPDATE um_parent_temp
SET comments =
(CASE comments WHEN ' ' THEN ' ' ELSE comments || '<br>' END)
|| 'Business address1 is empty'
WHERE pref_addr1 = 'B'
AND NVL (trim(busn_addr1_1), ' ') = ' '
AND load_year = v_load_year
AND institution = v_institution;
-- city
UPDATE um_parent_temp
SET comments =
(CASE comments WHEN ' ' THEN ' ' ELSE comments || '<br>' END)
|| 'Home city1 is empty'
WHERE NVL (trim(home_addr1_1), ' ') <> ' '
AND NVL (trim(home_city1), ' ') = ' '
AND load_year = v_load_year
AND institution = v_institution;
UPDATE um_parent_temp
SET comments =
(CASE comments WHEN ' ' THEN ' ' ELSE comments || '<br>' END)
|| 'Business city1 is empty'
WHERE NVL (trim(busn_addr1_1), ' ') <> ' '
AND NVL (trim(busn_city1), ' ') = ' '
AND load_year = v_load_year
AND institution = v_institution;
-- state
UPDATE um_parent_temp
SET comments =
(CASE comments WHEN ' ' THEN ' ' ELSE comments || '<br>' END)
|| 'Home state code1: '
|| home_state1
WHERE NVL (trim(home_addr1_1), ' ') <> ' '
AND trim(home_country1) IN ('US', 'USA', ' ')
AND trim(home_state1) NOT IN (SELECT tms_states.state_code FROM
tms_states)
AND load_year = v_load_year
AND institution = v_institution;
UPDATE um_parent_temp
SET comments =
(CASE comments WHEN ' ' THEN ' ' ELSE comments || '<br>' END)
|| 'Business state code1: '
|| busn_state1
WHERE NVL (trim(busn_addr1_1), ' ') <> ' '
AND trim (busn_country1) IN ('US', 'USA', ' ')
AND busn_state1 NOT IN (SELECT tms_states.state_code FROM tms_states)
AND load_year = v_load_year
AND institution = v_institution;
-- zip
UPDATE um_parent_temp
SET comments =
(CASE comments WHEN ' ' THEN ' ' ELSE comments || '<br>' END)
|| 'Invalid home zip code1: '
|| home_zip1
WHERE trim(home_country1) IN ('US', 'USA', ' ')
AND INSTR (home_zip1, '-') > 0
AND load_year = v_load_year
AND institution = v_institution;
UPDATE um_parent_temp
SET comments =
(CASE comments WHEN ' ' THEN ' ' ELSE comments || '<br>' END)
|| 'Invalid business zip code1: '
|| busn_zip1
WHERE trim (busn_country1) IN ('US', 'USA', ' ')
AND INSTR (busn_zip1, '-') > 0
AND load_year = v_load_year
AND institution = v_institution;
-- country
UPDATE um_parent_temp
SET comments =
(CASE comments WHEN ' ' THEN ' ' ELSE comments || '<br>' END)
|| 'Home country code1: '
|| home_country1
WHERE NVL (trim(home_country1), ' ') <> ' '
AND NVL (trim(home_country1), ' ') NOT IN (select campcountry from
gl_countries
WHERE gradloadcode = v_institution)
AND load_year = v_load_year
AND institution = v_institution;
UPDATE um_parent_temp
SET comments =
(CASE comments WHEN ' ' THEN ' ' ELSE comments || '<br>' END)
|| 'Business country code1: '
|| busn_country1
WHERE NVL (trim(busn_country1), ' ') <> ' '
AND busn_country1 NOT IN (select campcountry from gl_countries
WHERE gradloadcode = v_institution)
AND load_year = v_load_year
AND institution = v_institution;
-- unlisted flag
UPDATE um_parent_temp
SET comments =
(CASE comments WHEN ' ' THEN ' ' ELSE comments || '<br>' END)
|| 'Invalid home unlisted flag 1'
WHERE pref_addr1 = 'H'
AND home_unlisted_flag1 NOT IN ('N', 'Y')
AND load_year = v_load_year
AND institution = v_institution;
UPDATE um_parent_temp
SET comments =
(CASE comments WHEN ' ' THEN ' ' ELSE comments || '<br>' END)
|| 'Invalid business unlisted flag 1'
WHERE pref_addr1 = 'B'
AND busn_unlisted_flag1 NOT IN ('N', 'Y')
AND load_year = v_load_year
AND institution = v_institution;
/* parent 2 */
-- firstname
UPDATE um_parent_temp
SET comments =
(CASE comments WHEN ' ' THEN ' ' ELSE comments || '<br>' END)
|| 'Parent firstname2 is missing'
WHERE NVL (trim(parent_fn2), ' ') = ' '
AND NVL (trim(parent_ln2), ' ') <> ' '
AND load_year = v_load_year
AND institution = v_institution;
-- lastname
UPDATE um_parent_temp
SET comments =
(CASE comments WHEN ' ' THEN ' ' ELSE comments || '<br>' END)
|| 'Parent lastname2 is missing'
WHERE NVL (trim(parent_ln2), ' ') = ' '
AND NVL (trim(parent_fn2), ' ') <> ' '
AND load_year = v_load_year
AND institution = v_institution;
-- lastname2 too short
UPDATE um_parent_temp
SET comments =
(CASE comments WHEN ' ' THEN ' ' ELSE comments || '<br>' END)
|| 'Parent_lastname2 is too short'
WHERE Length(trim(parent_ln2)) = 1
AND NVL (trim(parent_fn2), ' ') <> ' '
AND load_year = v_load_year
AND institution = v_institution;
-- maximum label name = 460; includes three spaces
UPDATE um_parent_temp
SET comments =
(CASE comments WHEN ' ' THEN ' ' ELSE comments || '<br>' END)
|| 'Parent mailname2 is over 60 characters'
WHERE NVL (trim(parent_fn2), ' ') <> ' '
AND lengthc (parent_prefix2 || parent_fn2 || parent_mn2 ||
parent_ln2) > 57
AND load_year = v_load_year
AND institution = v_institution;
-- prefix
UPDATE um_parent_temp
SET comments =
(CASE comments WHEN ' ' THEN ' ' ELSE comments || '<br>' END)
|| 'Prefix2: '
|| parent_prefix2
WHERE NVL (trim(parent_prefix2), ' ') <> ' '
AND parent_prefix2 NOT IN
(SELECT gl_prefixes.campprefix FROM gl_prefixes)
AND load_year = v_load_year
AND institution = v_institution;
-- suffixPers
UPDATE um_parent_temp
SET comments =
(CASE comments WHEN ' ' THEN ' ' ELSE comments || '<br>' END)
|| 'Personal suffix2: '
|| parent_suffix_pers2
WHERE NVL (trim(parent_suffix_pers2), ' ') <> ' '
AND parent_suffix_pers2 NOT IN
(SELECT gl_suffixes.campsuffix FROM gl_suffixes)
AND load_year = v_load_year
AND institution = v_institution;
-- suffixProf
UPDATE um_parent_temp
SET comments =
(CASE comments WHEN ' ' THEN ' ' ELSE comments || '<br>' END)
|| 'Profession suffix2: '
|| parent_suffix_prof2
WHERE NVL (trim(parent_suffix_prof2), ' ') <> ' '
AND parent_suffix_prof2 NOT IN
(SELECT gl_suffixes.campsuffix FROM gl_suffixes)
AND load_year = v_load_year
AND institution = v_institution;
-- race
UPDATE um_parent_temp
SET comments =
(CASE comments WHEN ' ' THEN ' ' ELSE comments || '<br>' END)
|| 'Race2: '
|| race2
WHERE NVL (trim(race2), ' ') <> ' '
AND race2 NOT IN (SELECT campcode
FROM gl_races
WHERE campuscode = v_institution)
AND load_year = v_load_year
AND institution = v_institution;
-- sex
UPDATE um_parent_temp
SET comments =
(CASE comments WHEN ' ' THEN ' ' ELSE comments || '<br>' END)
|| 'Sex code2: '
|| sex2
WHERE NVL (trim(parent_fn2), ' ') <> ' '
AND sex2 NOT IN ('M', 'F')
AND load_year = v_load_year
AND institution = v_institution;
UPDATE um_parent_temp
SET comments =
(CASE comments WHEN ' ' THEN ' ' ELSE comments || '<br>' END)
|| 'Missmatching gender2 and prefix2: '
|| parent_prefix2
|| ' <---> '
|| sex2
WHERE ( (sex2 = 'M'
AND trim(parent_prefix2) IN ('Mrs.', 'Mrs', 'Miss.', 'Miss', 'Ms.',
'Ms'))
OR (sex2 = 'F' AND trim(parent_prefix2) IN ('Mr.', 'Mr')))
AND load_year = v_load_year
AND institution = v_institution;
-- address
UPDATE um_parent_temp
SET comments =
(CASE comments WHEN ' ' THEN ' ' ELSE comments || '<br>' END)
|| 'Preferred address flag2: '
|| pref_addr2
|| '<br>'
|| 'note: must also have unlisted flag set for preferred addr'
WHERE NVL (trim(parent_fn2), ' ') <> ' '
AND pref_addr2 NOT IN ('H', 'B')
AND load_year = v_load_year
AND institution = v_institution;
UPDATE um_parent_temp
SET comments =
(CASE comments WHEN ' ' THEN ' ' ELSE comments || '<br>' END)
|| 'Home address2_1 is empty'
WHERE NVL (trim(parent_fn2), ' ') <> ' '
AND pref_addr2 = 'H'
AND NVL (trim(home_addr2_1), ' ') = ' '
AND load_year = v_load_year
AND institution = v_institution;
UPDATE um_parent_temp
SET comments =
(CASE comments WHEN ' ' THEN ' ' ELSE comments || '<br>' END)
|| 'Business address2 is empty'
WHERE NVL (trim(parent_fn2), ' ') <> ' '
AND pref_addr2 = 'B'
AND NVL (trim(busn_addr2_1), ' ') = ' '
AND load_year = v_load_year
AND institution = v_institution;
-- city
UPDATE um_parent_temp
SET comments =
(CASE comments WHEN ' ' THEN ' ' ELSE comments || '<br>' END)
|| 'Home city2 is empty'
WHERE NVL (trim(home_addr2_1), ' ') <> ' '
AND NVL (trim(home_city2), ' ') = ' '
AND load_year = v_load_year
AND institution = v_institution;
UPDATE um_parent_temp
SET comments =
(CASE comments WHEN ' ' THEN ' ' ELSE comments || '<br>' END)
|| 'Business city2 is empty'
WHERE NVL (trim(busn_addr2_1), ' ') <> ' '
AND NVL (trim(busn_city2), ' ') = ' '
AND load_year = v_load_year
AND institution = v_institution;
-- state
UPDATE um_parent_temp
SET comments =
(CASE comments WHEN ' ' THEN ' ' ELSE comments || '<br>' END)
|| 'Home state 2: '
|| home_state2
WHERE NVL (trim(home_addr2_1), ' ') <> ' '
AND trim(home_country2) IN ('US', 'USA', ' ')
AND trim(home_state2) NOT IN (SELECT tms_states.state_code FROM
tms_states)
AND load_year = v_load_year
AND institution = v_institution;
I cannot enter any more code but the rest of it is similar
ColdFusion 2016 cannot use the DBVARNAME attribute of the <cfstoredproc> tag.
promoted from the comments
I ran the procedure in Oracle and the procedure ran just fine! But calling it through ColdFusion generates the error ORA-06502: PL/SQL: numeric or value error: character string buffer too small. It points to this line:
v_summary := '<b>Institution:</b> ' ||
v_institution ||
'<br><b>Load Code:</b> ' ||
v_load_year ||
'<br><b>Total records checked:</b> ' ||
TO_CHAR (v_rowcount, '99999') ||
'<br><b>Total records with errors:</b> ' ||
TO_CHAR (v_errorcount, '99999');
If I comment out this part, the procedure runs.

Invalid Time Literal in Teradata Dynamic SQL

The following stored procedure works fine when I use static sql to insert values into the DBCMNGR.ALERTREQUEST but does not work when trying to use the dynamic sql string.
I get invalid date literal and time literal when trying to call the below stored procedure.
CALL TESTDB.ALERT_REQUEST_INSERT('Test_JobName','Test_JobDescription',
'Test_ActionDestination','Test_JobFullMessage')
Need help to actually work this without any errors.
In the DBCMNGR.ALERTREQUEST the DATE and time are defined as follows:
ReqDate DATE FORMAT 'YYYY/MM/DD'
ReqTime INTEGER
REPLACE PROCEDURE TESTDB.ALERT_REQUEST_INSERT( IN p_JobName CHARACTER(60), IN p_JobDescription CHARACTER(120), IN p_ActionDestination CHARACTER(120), IN p_JobFullMessage CHARACTER(600) ) )
BEGIN
SET SQLSTR = 'INSERT INTO DBCMNGR.ALERTREQUEST ' || '(AlertRequest.ReqDate ' || ',AlertRequest.ReqTime ' || ',AlertRequest.JobName ' || ',AlertRequest.Description ' || ',AlertRequest.EventValue ' || ',AlertRequest.ActionCode ' || ',AlertRequest.RepeatPeriod ' || ',AlertRequest.Destination ' || ',AlertRequest.Message ' || ') ' || 'VALUES ( ' || ' DATE ' || ',TIME ' || ''',''' || TRIM(p_JobName) || ''',''' || TRIM(p_JobDescription) || ''',0 ' || ',''+'' ' || ',0 ' || ''',''' || TRIM(p_ActionDestination) || ',''' || TRIM(p_JobFullMessage) || ''');';
EXECUTE IMMEDIATE SQLSTR; END;

update query with OR condition in PL/sql

I have a update query in PL/SQL where I need to use OR condition based on itemsetid='XXXX or orgid ='YYYYY' this is because not all tables have these 2 fields so I need to use OR condition. I tried as below but it's not working ,
set serveroutput on size unlimited ;
declare
type item_type
is record (
maxSameas maxattribute.sameasattribute%TYPE,
maxTable maxattribute.objectname%TYPE,
maxAttrib maxattribute.attributename%TYPE
);
Type attribArray is table of item_type;
allAttribs attribArray;
cursor ITEM_ATTRIB_CURSOR is
select a.sameasattribute, a.objectname, a.attributename
from maxattribute a, maxobject b
where a.persistent = 1
and a.objectname = b.objectname
and b.persistent = 1
and ((a.sameasattribute is not null
and a.sameasattribute like 'ITEMNUM')
or (a.attributename = 'ITEMNUM'))
and a.objectname <> 'ITEMHIST'
-- and a.objectname <> 'ITEM'
and b.isView = '0'
order by a.objectname asc, a.attributename asc, a.sameasattribute desc;
type itemXrefType
is record (
currValue itemhist.ITEMNUM%type,
oldValue itemhist.OLDITEMNUM%type
);
type itemXrefTable is table of itemXrefType;
itemXref itemXrefTable;
cursor ITEM_VAL_CURSOR is
select itemnum, olditemnum
from itemhist
where olditemnum is not null and itemsetid='XXXXX';
updateStr varchar2 (4000);
queryStr varchar2 (4000);
tableName varchar2 (30);
attribName varchar2(50);
rowKount NUMBER;
begin
DBMS_OUTPUT.ENABLE(NULL);
-- Fetch Cross Reference Data
open item_val_cursor;
fetch item_val_cursor bulk collect into itemXref;
close item_val_cursor;
-- Fetch all Objects with ITEMNUM attribute
open ITEM_ATTRIB_CURSOR;
fetch ITEM_ATTRIB_CURSOR bulk collect into allAttribs;
close ITEM_ATTRIB_CURSOR;
-- Loop through every Object
for i in allAttribs.first..allAttribs.last loop
tableName := allAttribs(i).maxTable;
if (tableName = 'ITEM') then
attribName := 'ITEMNUM';
else
attribName := allAttribs(i).maxAttrib;
end if;
for j in itemXref.first .. itemXref.last loop
-- For each Item Num, update all objects
queryStr := 'select count (1) from ' || tableName ||
' where ' || attribName || '=' || '''' || itemXref(j).oldValue || '''';
-- Get Count before update
EXECUTE IMMEDIATE queryStr into RowKount;
updateStr := 'update ' || tableName ||
' set ' || attribName || ' = ' || '''' || itemXref(j).currValue
|| ''' where ' || attribName || '=' || '''' || itemXref(j).oldValue || ''' and (itemsetid = ''' || 'XXXX' || ''' or orgid = ''' || 'YYYYY' || ''' ) ''' '''';
--dbms_output.put_line (itemXref(j).currValue || ' ::::' || itemXref(j).oldValue);
dbms_output.put_line (updateStr || ':: Records to be updated is ' || rowKount);
-- Execute the Update
EXECUTE IMMEDIATE updateStr;
-- Commit changes
updateStr := 'Commit';
EXECUTE IMMEDIATE updateStr;
-- Get count after update - should be none!
EXECUTE IMMEDIATE queryStr into RowKount;
dbms_output.put_line (' Count of records after the update is ' || rowKount);
end loop;
end loop; --for i in allAttribs
end;
Thanks in advance!

Tuning Oracle SQL having NOT EXIST clause

I want to tune below query eliminating NOT EXIST clause specified in it. Can you please help.
GLT_temp_upload is temporary table where as DA_DUEDATE is partitioned table having huge data in it.
Please help
SELECT DISTINCT
batchid,
store_area,
STORE_AREA
|| ','
|| STORE_ID
|| ','
|| SMS_ID
|| ','
|| SMS_SERVICE
|| ','
|| SYNERGY_MODE_ID
|| ','
|| FREQUENCY
|| ','
|| DUEDATE
|| ','
|| STUDY_ID
|| ','
|| YEAR
|| ''
|| WEEK_ID
||',Not exist in Da_Duedate'
FROM GLT_temp_upload upload
WHERE upload.batchid = 1
AND NOT EXISTS
(SELECT due.week_id,
due.country_id,
due.year,
due.study_id,
due.store_id,
due.store_area,
due.synergy_mode_id,
upload.batchid,
due.due_date,
upload.sms_service
FROM DA_DUEDATE due
WHERE due.store_id = upload.store_id
AND due.study_id = upload.study_id
AND due.store_area = upload.store_area
AND due.frequency = upload.frequency
AND due.sms_service = upload.sms_service
AND due.week_id = upload.week_id
AND due.country_id = upload.country_id
AND due.year = upload.year
AND due.sms_id = upload.sms_id
AND due.synergy_mode_id =
upload.synergy_mode_id)
You may try NOT EXISTS / LEFT JOIN / NOT IN
In your NOT EXISTS it's enough to SELECT 1 instead of the list of columns
Sometimes LEFT JOIN can be more beneficial (depending on indexes, the size of the tables etc)
SELECT DISTINCT
batchid,
store_area,
STORE_AREA
|| ','
|| STORE_ID
|| ','
|| SMS_ID
|| ','
|| SMS_SERVICE
|| ','
|| SYNERGY_MODE_ID
|| ','
|| FREQUENCY
|| ','
|| DUEDATE
|| ','
|| STUDY_ID
|| ','
|| YEAR
|| ''
|| WEEK_ID
||',Not exist in Da_Duedate'
FROM GLT_temp_upload upload left join DA_DUEDATE due
ON due.store_id = upload.store_id
AND due.study_id = upload.study_id
AND due.store_area = upload.store_area
AND due.frequency = upload.frequency
AND due.sms_service = upload.sms_service
AND due.week_id = upload.week_id
AND due.country_id = upload.country_id
AND due.year = upload.year
AND due.sms_id = upload.sms_id
AND due.synergy_mode_id = upload.synergy_mode_id
WHERE upload.batchid = 1 and due.store_id is NULL;
I'd recommend you looking at the execution plan to find an optimal solution for your case.

Resources