mismatched input '(' expecting <EOF>(line 3, pos 28) - sql

My code looks like this, I do not know why it raising an error, the error is in line 3 after case when, Can anyone help on this? thanks
SELECT
CASE
WHEN
(
CASE
WHEN
(
ltrim(rtrim(status)) = 'CANCELLED'
AND ltrim(rtrim(COALESCE(iscancelledwithte, - ))) = '0'
)
THEN
CASE
WHEN
(
mincancellationdate IS NULL
)
THEN
CASE
WHEN
(
lastupdatedts IS NULL
)
THEN
'9999-12-31'
ELSE
lastupdatedts
END
ELSE
mincancellationdate
END
ELSE
CASE
WHEN
(
approvedforbillingts IS NULL
)
THEN
'9999-12-31'
ELSE
approvedforbillingts
END
END
)
= '9999-12-31'
THEN
status
ELSE
'Closed'
END
AS casestatusname
FROM
tblrequesttemp AS tblrequests

I've paste your sql request into SQL Syntax Checker (https://www.eversql.com/sql-syntax-check-validator/), and it return an error in your coalesce function. I believe you forget the quotes around the tiret character.
There is the fixed SQL Request:
SELECT
CASE
WHEN (
CASE
WHEN (Ltrim(Rtrim(status)) = 'CANCELLED' AND ltrim(rtrim(coalesce(iscancelledwithte, '-'))) = '0') THEN
CASE
WHEN (mincancellationdate IS NULL) THEN
CASE
WHEN (lastupdatedts IS NULL) THEN '9999-12-31'
ELSE lastupdatedts
end
ELSE mincancellationdate
end
ELSE
CASE
WHEN (approvedforbillingts IS NULL) THEN '9999-12-31'
ELSE approvedforbillingts
end
end) = '9999-12-31' THEN status
ELSE 'Closed'
end
AS casestatusname
FROM tblrequesttemp AS tblrequests

Related

join two cases to create a new field

I have a table with fields man_phone and head_phone, and in the selects there are such cases for obtaining them:
case
when p.dep_code <> 'MM' then
case
when p.man_id = '%%%%%%%' then
'Man Spec-100'
when m.man_l is not null then
m.man_phone
else
case
when m2.man_l is not null then
m2.man_phone
else
m3.man_phone
end
end
else
o.cont_phone
end as man_phone,
case
when p.dep_code <> 'MM' then
case
when p.man_id = '%%%%%%%' then
'Man Spec-100'
when m2.man_l is not null then
m2.man_phone
else
m3.man_phone
end
else
o.head_phone
end as head_phone
I want to add a new field called head_login with a case if man_phone = head_phone, then it will output man_l as head_login and I tried to push these two existing cases into only one case, what would be the right way to do this?
case
case
when p.dep_code <> 'MM' then
case
when p.man_id = '%%%%%%%' then
'Man Spec-100'
when m.man_l is not null then
m.man_phone
else
case
when m2.man_l is not null then
m2.man_phone
else
m3.man_phone
end
end
else
o.cont_phone
end as man_phone,
case
when p.dep_code <> 'MM' then
case
when p.man_id = '%%%%%%%' then
'Man Spec-100'
when m2.man_l is not null then
m2.man_phone
else
m3.man_phone
end
else
o.head_phone
end as head_phone
when p.department_code <> 'MM' then
case
when man_phone = head_phone
m2.man_l
else
m3.man_l
end
end as head_login
I want to add a new field called head_login with a case if man_phone = head_phone, then it will output man_l as head_login and I tried to push these two existing cases into only one case.
Do not try to merge the two cases into a third case. Evaluate them in a sub-query and then put the CASE which compares them into an outer query:
SELECT man_phone,
head_phone,
CASE
WHEN man_phone = head_phone
THEN man_l2
ELSE man_l3
END AS head_login
FROM (
SELECT CASE <your case> END AS man_phone,
CASE <your case> END AS head_phone,
m2.man_l AS man_l2,
m3.man_l AS man_l3
FROM table_name
)

'Case' inside another value condition

I'm trying to do a CASE inside another CASE expression. Actually, the situation is:
(SELECT cmp_tubo
FROM qt_qts.res_tubo_serra
WHERE res_tubo_serra.cod_ordem_producao = ro.cod_ordem_producao AND res_tubo_serra.idc_tubo = ro.idc_tubo
AND res_tubo_serra.idc_serra != 0
) AS cmp_corte_ponta
What I need:
IF the value of "cmp_corte_ponta" = 0 THEN I'll need to do this search and replace the value of the "cmp_corte_ponta":
Select
(CASE serra.tip_corte
WHEN '1' THEN to_char(SERRA.CMP_CORTE)
ELSE 'NO'
END
) cmp_corte_ponta
from fl_qts.res_tubo_serra_feixe serra
IF the value of "cmp_corte_ponta" != 0, then the value must still the same.
What kind of syntax should I use?
Thx!
* EDITED CODE *
select
pla.cod_pedido,
pla.cod_aqa,
coalesce (
NULLIF(
(SELECT cmp_tubo
FROM qt_qts.res_tubo_serra
WHERE res_tubo_serra.cod_ordem_producao = ro.cod_ordem_producao
AND res_tubo_serra.idc_tubo = ro.idc_tubo
AND res_tubo_serra.idc_serra != 0)
, 0),
(SELECT
(CASE serra.tip_corte
WHEN '1' THEN SERRA.CMP_CORTE
ELSE 0
END)
FROM fl_qts.res_tubo_serra_feixe serra)) as cmp_corte_ponta,
pla.another_collum
FROM qt_qts.res_tubo_secao_od ro...
And the error now is:
"ORA-01427 - "single-row subquery returns more than one row".
ORIGINAL but messed up datatypes:
SELECT COALESCE(
NULLIF(
(SELECT cmp_tubo
FROM qt_qts.res_tubo_serra
WHERE res_tubo_serra.cod_ordem_producao = ro.cod_ordem_producao
AND res_tubo_serra.idc_tubo = ro.idc_tubo
AND res_tubo_serra.idc_serra != 0)
, 0),
SELECT
(CASE serra.tip_corte
WHEN '1' THEN to_char(SERRA.CMP_CORTE)
ELSE 'NO'
END) cmp_corte_ponta
FROM fl_qts.res_tubo_serra_feixe serra
)
EDIT corrected datatypes. NULLIF() and COALESCE() both need all argument types to be the same (perhaps coercion will take place):
SELECT COALESCE(
NULLIF(
(SELECT to_char(cmp_tubo)
FROM qt_qts.res_tubo_serra
WHERE res_tubo_serra.cod_ordem_producao = ro.cod_ordem_producao
AND res_tubo_serra.idc_tubo = ro.idc_tubo
AND res_tubo_serra.idc_serra != 0)
, '0'),
SELECT
(CASE serra.tip_corte
WHEN '1' THEN to_char(SERRA.CMP_CORTE)
ELSE 'NO'
END) cmp_corte_ponta
FROM fl_qts.res_tubo_serra_feixe serra
)
I might try some nested decode statements given the small amount of values you are dealing with:
(SELECT decode(cmp_tubo,
0, (SELECT decode(serra.tip_corte,
'1', to_char(serra.cmp_corte),
'NO')
FROM fl_qts.res_tubo_serra_feixe serra),
cmp_tubo)
FROM qt_qts.res_tubo_serra
WHERE res_tubo_serra.cod_ordem_producao = ro.cod_ordem_producao AND res_tubo_serra.idc_tubo = ro.idc_tubo
AND res_tubo_serra.idc_serra != 0
) AS cmp_corte_ponta

ORA-22835: Buffer too small for CLOB to CHAR or BLOB to RAW conversion (actual: 4448, maximum: 4000)

I am trying to Migrate client's data and this error happened during the migration procedure. I have searched around and the possible solution is to trim or use DBMS_LOB.SUBSTR, but i do not know where to put them in. It could be this part (TO_CHAR(AIMSCMDDL_AT.BIOS_SEQ.nextval) but i am still new to SQL and not sure what could be the cause of the problem. Can anyone guide me on how to solve this problem? Thank you!
Update
AIMSCMDDL_AT.AI_OPENNET_SVC_RPT
SET
CUST_AGREEMENT_SIGNATURE =
CASE
WHEN TO_CHAR(CUST_AGREEMENT_SIGNATURE) = null
THEN null
ELSE 'NULL'
END
, CUST_DECLARATION_AUTH_ID =
CASE
WHEN CUST_DECLARATION_AUTH_ID = null
THEN null
ELSE
CASE WHEN LENGTH(CUST_DECLARATION_AUTH_ID)<>9
THEN TO_CHAR(AIMSCMDDL_AT.BIOS_SEQ.nextval)
ELSE
CASE WHEN LENGTH(CUST_DECLARATION_AUTH_ID)=9
and REGEXP_LIKE(substr(CUST_DECLARATION_AUTH_ID,1,1),'[A-Za-z]')
and REGEXP_LIKE(substr(CUST_DECLARATION_AUTH_ID,-1,1), '[A-Za-z]')
and AIMSCMDDL_AT.is_number(substr(CUST_DECLARATION_AUTH_ID,2,7)) = 1
THEN substr(CUST_DECLARATION_AUTH_ID,1,1)||LPAD(TO_CHAR(AIMSCMDDL_AT.BIOS_SEQ.nextval),7,'0')||substr(CUST_DECLARATION_AUTH_ID,-1,1)
ELSE TO_CHAR(AIMSCMDDL_AT.BIOS_SEQ.nextval)
END
END
END
, CUST_DECLARATION_AUTH_NM =
CASE
WHEN CUST_DECLARATION_AUTH_NM = null
THEN null
ELSE REGEXP_REPLACE(CUST_DECLARATION_AUTH_NM,'[[:alpha:]^[:digit:]^[:punct:]^]','*')
END
, CUSTOMER_SIGNATURE =
CASE
WHEN TO_CHAR(CUSTOMER_SIGNATURE) = null
THEN null
ELSE 'NULL'
END
, INSTALLER_NM =
CASE
WHEN INSTALLER_NM = null
THEN null
ELSE REGEXP_REPLACE(INSTALLER_NM,'[[:alpha:]^[:digit:]^[:punct:]^]','*')
END
, INSTALLER_SIGNATURE =
CASE
WHEN TO_CHAR(INSTALLER_SIGNATURE) = null
THEN null
ELSE 'NULL'
END
, REJ_CUSTOMER_ID =
CASE
WHEN REJ_CUSTOMER_ID = null
THEN null
ELSE
CASE WHEN LENGTH(REJ_CUSTOMER_ID)<>9
THEN TO_CHAR(AIMSCMDDL_AT.BIOS_SEQ.nextval)
ELSE
CASE WHEN LENGTH(REJ_CUSTOMER_ID)=9
and REGEXP_LIKE(substr(REJ_CUSTOMER_ID,1,1),'[A-Za-z]')
and REGEXP_LIKE(substr(REJ_CUSTOMER_ID,-1,1), '[A-Za-z]')
and AIMSCMDDL_AT.is_number(substr(REJ_CUSTOMER_ID,2,7)) = 1
THEN substr(REJ_CUSTOMER_ID,1,1)||LPAD(TO_CHAR(AIMSCMDDL_AT.BIOS_SEQ.nextval),7,'0')||substr(REJ_CUSTOMER_ID,-1,1)
ELSE TO_CHAR(AIMSCMDDL_AT.BIOS_SEQ.nextval)
END
END
END
, REJ_CUSTOMER_NM =
CASE
WHEN REJ_CUSTOMER_NM = null
THEN null
ELSE REGEXP_REPLACE(REJ_CUSTOMER_NM,'[[:alpha:]^[:digit:]^[:punct:]^]','*')
END
, REJ_CUSTOMER_SIGNATURE =
CASE
WHEN TO_CHAR(REJ_CUSTOMER_SIGNATURE) = null
THEN null
ELSE 'NULL'
END
, REJ_INSTALLER_NM =
CASE
WHEN REJ_INSTALLER_NM = null
THEN null
ELSE REGEXP_REPLACE(REJ_INSTALLER_NM,'[[:alpha:]^[:digit:]^[:punct:]^]','*')
END
, REJ_INSTALLER_SIGNATURE =
CASE
WHEN TO_CHAR(REJ_INSTALLER_SIGNATURE) = null
THEN null
ELSE 'NULL'
END
;
Most likely it comes from one of these expressions:
CASE
WHEN TO_CHAR(INSTALLER_SIGNATURE) = null
THEN null
ELSE 'NULL'
END
First, they don't work. ... = NULL never yields TRUE. Is IS NULL instead.
There is no need to use TO_CHAR(), IS NULL works for any data type.
And you can write it shorter as
INSTALLER_SIGNATURE = NVL2(INSTALLER_SIGNATURE, 'NULL', NULL)
NB,
REGEXP_LIKE(substr(CUST_DECLARATION_AUTH_ID,1,1),'[A-Za-z]')
and REGEXP_LIKE(substr(CUST_DECLARATION_AUTH_ID,-1,1), '[A-Za-z]')
can be written as
REGEXP_LIKE(CUST_DECLARATION_AUTH_ID,'^[A-Za-z].*[A-Za-z]$')

Nested CASE statement in UPDATE SET clause

I am attempting to switch a value based on a set of conditions and I have noticed that where I have a nested CASE statement within the SET clause of my UPDATE expression, the columns are not updating.
When a simple CASE expression existis the columns appear to be updating. However, for the OVERRIDDEN_CHECK_NUMBER and OVERRIDDEN_AMOUNT in this example, the columns are not updating.
The OVERRIDDEN_DATE, OVERRIDDEN_USER_ID, CHECK_NO and AMOUNT columns in the UPDATE are updating without issue.
Can anyone tell me why the OVERRIDDEN_CHECK_NUMBER and OVERRIDDEN_AMOUNT will not update in this UPDATE statement?
Are nested CASE statements not tallowed in the SET clause of the UPDATE expression?
SQL EXAMPLE
UPDATE WAREHOUSE.BANK_STATEMENT_ACTIVITY
SET OVERRIDDEN_CHECK_NO = --(CASE :btn
(CASE WHEN (:btn = '1') THEN CASE WHEN '141973' = '141973' THEN NULL
WHEN '141973' != '999999' THEN LPAD(TRIM(141973), 12, '0')
END
WHEN (:btn = '2') THEN '1740 - Previously Paid Warrant'
ELSE NULL
END),
OVERRIDDEN_AMOUNT = --(CASE :btn
(CASE WHEN (:btn = '1') THEN CASE WHEN 253.20 = 253.20 THEN NULL
WHEN 253.20 != 999.99 THEN LPAD(TRIM(253.20), 12, '0')
END
WHEN (:btn = '2') THEN NULL
ELSE NULL
END),
OVERRIDDEN_DATE = SYSDATE,
OVERRIDDEN_USER_ID = 1009,
CHECK_NO = (CASE :btn
WHEN '1' THEN LPAD(TRIM(999999), 12, '0')
WHEN '2' THEN '142775'
ELSE NULL
END),
AMOUNT = (CASE :btn
WHEN '1' THEN TRIM(78.60)
WHEN '2' THEN TRIM(253.20)
ELSE NULL
END)
WHERE TO_NUMBER(CHECK_NO) = (CASE :btn
WHEN '1' THEN '141973'
WHEN '2' THEN '142775'
ELSE NULL
END)
AND AMOUNT = (CASE :btn
WHEN '1' THEN 78.60
WHEN '2' THEN 253.20
ELSE NULL
END)
AND TRUNC(LOAD_DATE) = (CASE :btn
WHEN '1' THEN TRUNC(LOAD_DATE)
WHEN '2' THEN (SELECT (MAX(LOAD_DATE)) FROM WAREHOUSE.BANK_STATEMENT_ACTIVITY)
ELSE NULL
END)
AND BANKACCTNO = (SELECT LONGDESC
FROM TCMS.COMPLEMENTARY_VALIDATIONS
WHERE TEXTCODE = 'BANKACCT'
AND CODE = 80);
EDIT
After fixing the logival issue that #mathguy pointed out the update query via the editor worked. However, when executing this via the PL/SQL pacakge the table update fails.
Here is the before update:
Before - result
Her is the after result:
After - result
They are identical.
This is the actual package procedure:
PROCEDURE UpdateBankStatementActivity( btn IN VARCHAR2)
AS
BEGIN
DBMS_OUTPUT.PUT_LINE( 'UpdateBankStatementActivity - btn: ' || btn );
DBMS_OUTPUT.PUT_LINE( 'UpdateBankStatementActivity - bsa_rec.OVERRIDDEN_CHECK_NO: ' || bsa_rec.OVERRIDDEN_CHECK_NO || CHR(10) ||
'bsa_rec.OVERRIDDEN_AMOUNT: ' || bsa_rec.OVERRIDDEN_AMOUNT || CHR(10) ||
'bsa_rec.CHECK_NO: ' || bsa_rec.CHECK_NO || CHR(10) ||
'bsa_rec.AMOUNT: ' || bsa_rec.AMOUNT || CHR(10) ||
'bsa_rec.LOAD_DATE:' || bsa_rec.LOAD_DATE );
UPDATE WAREHOUSE.BANK_STATEMENT_ACTIVITY
SET OVERRIDDEN_CHECK_NO = --(CASE :btn
(CASE WHEN (btn = '1') THEN CASE WHEN bsa_rec.OVERRIDDEN_CHECK_NO = bsa_rec.CHECK_NO THEN NULL
WHEN bsa_rec.OVERRIDDEN_CHECK_NO != bsa_rec.CHECK_NO THEN LPAD(TRIM(bsa_rec.OVERRIDDEN_CHECK_NO), 12, '0')
END
WHEN (btn = '2') THEN '1740 - Previously Paid Warrant'
ELSE NULL
END),
OVERRIDDEN_AMOUNT = --(CASE :btn
(CASE WHEN (btn = '1') THEN CASE WHEN bsa_rec.OVERRIDDEN_AMOUNT = bsa_rec.AMOUNT THEN NULL
WHEN bsa_rec.OVERRIDDEN_AMOUNT != bsa_rec.AMOUNT THEN LPAD(TRIM(bsa_rec.OVERRIDDEN_AMOUNT), 12, '0')
END
WHEN (btn = '2') THEN NULL
ELSE NULL
END),
OVERRIDDEN_DATE = SYSDATE,
OVERRIDDEN_USER_ID = bsa_rec.OVERRIDDEN_USER_ID,
CHECK_NO = (CASE btn
WHEN '1' THEN LPAD(TRIM(bsa_rec.CHECK_NO), 12, '0')
WHEN '2' THEN LPAD(TRIM(bsa_rec.CHECK_NO), 12, '0')
ELSE NULL
END),
AMOUNT = (CASE btn
WHEN '1' THEN TRIM(bsa_rec.OVERRIDDEN_AMOUNT)
WHEN '2' THEN TRIM(bsa_rec.AMOUNT)
ELSE NULL
END)
WHERE TO_NUMBER(CHECK_NO) = (CASE btn
WHEN '1' THEN bsa_rec.OVERRIDDEN_CHECK_NO
WHEN '2' THEN bsa_rec.CHECK_NO
ELSE NULL
END)
AND AMOUNT = (CASE btn
WHEN '1' THEN bsa_rec.OVERRIDDEN_AMOUNT
WHEN '2' THEN bsa_rec.AMOUNT
ELSE NULL
END)
AND TRUNC(LOAD_DATE) = (CASE btn
WHEN '1' THEN TRUNC(bsa_rec.LOAD_DATE)
WHEN '2' THEN (SELECT MAX(LOAD_DATE) FROM WAREHOUSE.BANK_STATEMENT_ACTIVITY)
ELSE NULL
END)
AND BANKACCTNO = (SELECT LONGDESC
FROM TCMS.COMPLEMENTARY_VALIDATIONS
WHERE TEXTCODE = 'BANKACCT'
AND CODE = 80);
COMMIT;
END UpdateBankStatementActivity;
The parameter values in the first SQL CODE snippet are consistent with the parameters in the PL/SQL block.
What do you mean by "not updating"?
There is a clear logical flaw in your nested case expressions, when :btn = '1'. Namely, the first branch of the "inner" case expression always evaluates to TRUE, so the "update value" will be null every time you pass in :btn = '1'. Is that the problem you are noticing? Then: For overridden_amount the result will always be null because of this; for overridden_check_no it will not be null if and only if :btn = '2'.
Note that you could write the "outer" case expressions with the same syntax you use for the simple expressions: case :btn when '1' then case .... end else ... end.
EDIT: Answering your modified question (perhaps). I am not sure I can follow the logic in its entirety; but what is happening in the example (assuming you called the procedure with btn = '1' is this:
Both the overridden_check_no and the overridden_amount are null in the original table. In the nested (inner) case expression for both columns you check two columns, with either = or !=. Neither of the tests is true when one of the terms is null! So the case evaluations falls through to the else clause, or when else is not included, to the default, which is null. The update actually did work, it just updated the values to null because none of the "actual branches" in the inner case statements evaluated to true.
You say, though, that this works OK if you just run it as a stand-alone SQL update, directly from your editor. I wonder how that is possible, unless - again - the code in the procedure is different from what you have in your editor.

Concatenating multiple CASE statements into one alias

After some previous help on how to approach a problem I am having with some legacy code, it seems like the best approach for my issue is to concatenate case statements to return a value I can parse out in PHP.
I am trying to do something like this, but it is returning many rows, and eventually getting this error:
Maximum stored procedure, function, trigger, or view nesting level
exceeded (limit 32).
SELECT org.org_id,
org.org_name_1,
Datename(YEAR, member.enroll_date) AS enroll_year,
Max(CASE
WHEN board.member_from IS NULL THEN 0
ELSE 1
END) AS board_member,
CASE
WHEN ( org.delete_reason = 'OUT'
AND org.org_delete_flag = 'Y'
AND org.org_status_flag = 'C' ) THEN 'out_of_business|'
ELSE ''
END + CASE
WHEN ( stat.carrier = 'BS'
AND stat.status_id IS NOT NULL
AND stat.termination_date IS NULL
AND stat.flat_dues > 0 ) THEN 'insurance_member|'
ELSE ''
END + CASE
WHEN ( stat.carrier = 'BS'
AND stat.status_id IS NOT NULL
AND stat.termination_date IS NULL
AND stat.flat_dues = 0
AND member.status_flag IN( 'C', 'P' ) ) THEN 'insurance_product|'
ELSE ''
END + CASE
WHEN ( member.enroll_date IS NOT NULL
AND member.status_flag NOT IN( 'C', 'P' ) ) THEN 'member_since|'
ELSE ''
END + CASE
WHEN ( org.org_relationship_parent = 'Y'
AND org.dues_category = 'MBR'
AND org.org_status_flag = 'R' ) THEN 'subsidiary_member|'
ELSE ''
END + CASE
WHEN ( org.org_misc_data_9 = 'PAC' ) THEN 'pac|'
ELSE ''
END + CASE
WHEN ( org.dues_category = 'PART' ) THEN 'partner_member|'
ELSE ''
END + CASE
WHEN ( org.dues_category = 'FREE'
AND org.org_status_flag = 'P' ) THEN 'associate_member|'
ELSE ''
END
--ELSE 'non_member'
--END
AS org_status,
60 AS expires_in,
CASE
WHEN stat.dues_type = 'M' THEN
CASE
WHEN ( stat.termination_date IS NULL ) THEN ( stat.flat_dues )
ELSE 0
END
ELSE
CASE
WHEN ( member.payments = 0 ) THEN member.dues_billed_annual
ELSE member.payments
END
END AS dues_level,
CASE
WHEN ( org.affiliate_code = 'PCCE'
AND org.dues_category = 'MBR'
AND org.org_status_flag = 'R' ) THEN 1
ELSE 0
END AS pcce_membr,
-- '$'+CONVERT(VARCHAR,#dues) AS dues_level,
Ltrim(#product_level) AS product_level,
Ltrim(#involve_level) AS involvement_level
FROM organiz AS org
LEFT JOIN affilbil AS member
ON member.status_id = org.org_id
AND member.dues_category = 'MBR'
LEFT JOIN individu AS ind
ON ind.org_id = org.org_id
LEFT JOIN commembr AS board
ON board.status_id = ind.ind_id
AND board.committee_code = '5'
AND board.member_to IS NULL
LEFT JOIN statinsmorn AS stat
ON stat.status_id = org.org_id
AND stat.carrier = 'BS'
AND stat.planz = 'PCI'
WHERE org.org_id = #org_id
GROUP BY org.org_id,
org.org_name_1,
member.enroll_date,
org.delete_reason,
org.org_status_flag,
org.org_delete_flag,
stat.status_id,
stat.flat_dues,
stat.dues_type,
stat.termination_date,
org.org_misc_data_9,
org_relationship_parent,
org.dues_category,
member.status_flag,
member.dues_billed_annual,
member.payments,
stat.carrier,
org.Affiliate_Code
Well, this is embarrassing.
When I was making my changes to the stored procedure, I had inadvertently placed a call to the same procedure at the bottom. So I was recursively calling the same procedure over and over again. DOH.