SQL Server condition not working - sql

Below is part of a SQL Server query which is not working as expected. I don't know why, but condition falls through this part, like nothing is inserted (like all three conditions are set to NULL). What am I doing wrong here?
Input parameters for the stored procedure:
#cStockPacked varchar(1) = NULL,
#cWrittenDown varchar(1) = NULL,
#cInPreparation varchar(1) = NULL
Part of the query which is not working:
(
(#cStockPacked IS NULL OR
(#cStockPacked = '1' AND MEST.MEST_STA = '1')
)
OR
(#cInPreparation IS NULL OR
(#cInPreparation = '1' AND MEST.MEST_STA = '2')
)
OR
(#cWrittenDown IS NULL OR
(#cWrittenDown = '1' AND MEST.MEST_STA = '4')
)
)
I've tried to hard code conditions, everything worked as expected:
(
MEST.MEST_STA = '1' OR MEST.MEST_STA = '2' OR MEST.MEST_STA = '4'
)
I've tried with one condition (hardcoded), then with two and later with all three. All was good. I want to achieve the same result with query pasted above, but something is obviously wrong. What am I doing wrong?
Whole procedure:
ALTER PROCEDURE [dbo].[sp_CAMERC_HLP]
( , #cStockPacked varchar(1) = NULL
, #cWrittenDown varchar(1) = NULL
, #cInPreparation varchar(1) = NULL
)
AS
BEGIN
SELECT
MEST.MEST_QUA, MERC_STA, MERC_NME,
MERC_DES, MERC_NTO, MERC_UNI, MERC_LPR,
MERC.UNIT_KEY,
COUNT_ALL_ROWS = COUNT(*) OVER()
FROM
CAMERC MERC
INNER JOIN
CAMEGR CAMEGR ON MERC.MEGR_KEY = CAMEGR.MEGR_KEY
INNER JOIN
CAMEST MEST ON MERC.MERC_KEY = MEST.MERC_KEY
WHERE
(#cMERC_NME IS NULL OR MERC_NME LIKE '%' + #cMERC_NME + '%')
AND
(#iMEGR_KEY IS NULL OR MERC.MEGR_KEY IN (SELECT MEGR_KEY FROM CTE))
AND
(#cMERC_CDO1 IS NULL OR MERC_CDO1 LIKE '%' + #cMERC_CDO1 + '%')
AND
and
(
(#cStockPacked IS NULL OR #cStockPacked = '1' AND MEST.MEST_STA = '1')
OR
(#cInPreparation IS NULL OR (#cInPreparation = '1' AND MEST.MEST_STA = '2'))
OR
(#cWrittenDown IS NULL OR (#cWrittenDown = '1' AND MEST.MEST_STA = '4'))
)
END

You may looking for this
(
(
(#cStockPacked IS NOT NULL AND #cStockPacked = '1' AND MEST.MEST_STA = '1') OR
(#cInPreparation IS NOT NULL AND #cInPreparation = '1' AND MEST.MEST_STA = '2') OR
(#cWrittenDown IS NOT NULL AND #cWrittenDown = '1' AND MEST.MEST_STA = '4')
)
OR
( #cStockPacked IS NULL OR #cInPreparation IS NULL OR #cWrittenDown IS NULL )
)

I don't understand why you accepted that answer as correct, it does not make much sense to test var for NULL with those conditions.
All you need - to make these conditions same as others. Just like this:
AND (#cMERC_NME IS NULL OR MERC_NME LIKE '%' + #cMERC_NME + '%')
update them to:
AND (#cStockPacked IS NULL OR (#cStockPacked = '1' AND MEST.MEST_STA = '1'))
AND (#cInPreparation IS NULL OR (#cInPreparation = '1' AND MEST.MEST_STA = '2'))
AND (#cWrittenDown IS NULL OR (#cWrittenDown = '1' AND MEST.MEST_STA = '4'))
that's it.

Related

Filter result using alias

I have a query like this
SELECT
Type = TXL.Descript, Jurisdiction = CASE WHEN TX.State = 'KD' THEN 'FD'
WHEN TX.County > '' AND TX.County IS NOT NULL THEN 'Local' ELSE 'Country' END,
State = CASE WHEN TX.State = 'FD' OR (TX.County > '' AND TX.County IS NOT NULL) THEN '' ELSE (select top 1 istIntlStateDesc from IntStateM ISM where ISM.istIntlStateCode = TX.MtcState) END,
Local = CASE WHEN TX.County > '' AND TX.County IS NOT NULL THEN TX.MtcCounty ELSE '' END,
TaxCode = TX.TaxCode, TaxDescription = TX.TaxCodeDesc, EffectiveDate = ET.EtxEffectiveDate,
Taxable = ''
FROM
ETaX ET WITH (NOLOCK)
INNER JOIN TxCast TX on ET.Code = TX.TaxCode
INNER JOIN TxLMast TXL on TXL.TCode = TX.Tax
WHERE
ET.TDate <= GETDATE()
AND ET.SDate > GETDATE()
In the UI I have a table and I want to filer each column by passing the column name and what my code is doing is adding an AND condition to the WHERE clause while filtering but if I am passing the alias name (Ex. Jurisdiction) I am getting an error like - Invalid column name 'Jurisdiction', I also can't pass the actual column name as I am using case statement in the query, for example alias Jurisdiction value is coming by combing 2 column name so, is there any way it will work even passing the alias name or any better way to do it like dynamic SQL or any other workaround?
If I understand correctly, you want to filter by your "aliased" fields. To do so, you have 2 options:
Use subquery writing a SELECT FROM your entire query and then filtering by Jurisdiction:
SELECT * FROM
(SELECT
Type = TXL.Descript, Jurisdiction = CASE WHEN TX.State = 'KD' THEN 'FD'
WHEN TX.County > '' AND TX.County IS NOT NULL THEN 'Local' ELSE 'Country' END,
State = CASE WHEN TX.State = 'FD' OR (TX.County > '' AND TX.County IS NOT NULL) THEN '' ELSE (select top 1 istIntlStateDesc from IntStateM ISM where ISM.istIntlStateCode = TX.MtcState) END,
Local = CASE WHEN TX.County > '' AND TX.County IS NOT NULL THEN TX.MtcCounty ELSE '' END,
TaxCode = TX.TaxCode, TaxDescription = TX.TaxCodeDesc, EffectiveDate = ET.EtxEffectiveDate,
Taxable = ''
FROM
ETaX ET WITH (NOLOCK)
INNER JOIN TxCast TX on ET.Code = TX.TaxCode
INNER JOIN TxLMast TXL on TXL.TCode = TX.Tax
WHERE
ET.TDate <= GETDATE()
AND ET.SDate > GETDATE()) AS AUX_QUERY
WHERE
Jurisdiction = -your condition here-
(Note now Jurisdiction is a result field from AUX_QUERY)
Repeat the full CASE WHEN TX.State = 'KD' THEN 'FD' WHEN TX.County > '' AND TX.County IS NOT NULL THEN 'Local' ELSE 'Country' END that you called Jurisdiction in the WHERE clause (or wherever you want to use those calculated fields:
SELECT
Type = TXL.Descript, Jurisdiction = CASE WHEN TX.State = 'KD' THEN 'FD'
WHEN TX.County > '' AND TX.County IS NOT NULL THEN 'Local' ELSE 'Country' END,
State = CASE WHEN TX.State = 'FD' OR (TX.County > '' AND TX.County IS NOT NULL) THEN '' ELSE (select top 1 istIntlStateDesc from IntStateM ISM where ISM.istIntlStateCode = TX.MtcState) END,
Local = CASE WHEN TX.County > '' AND TX.County IS NOT NULL THEN TX.MtcCounty ELSE '' END,
TaxCode = TX.TaxCode, TaxDescription = TX.TaxCodeDesc, EffectiveDate = ET.EtxEffectiveDate,
Taxable = ''
FROM
ETaX ET WITH (NOLOCK)
INNER JOIN TxCast TX on ET.Code = TX.TaxCode
INNER JOIN TxLMast TXL on TXL.TCode = TX.Tax
WHERE
ET.TDate <= GETDATE()
AND ET.SDate > GETDATE()) AS AUX_QUERY
AND
CASE WHEN TX.State = 'KD' THEN 'FD'
WHEN TX.County > '' AND TX.County IS NOT NULL THEN 'Local' ELSE 'Country' END = -your condition here-
Aliases cannot be used like that, look at this example
declare #table1 table (id int, text1 varchar(50), text2 varchar(50))
insert into #table1 values (1, 'one', 'two')
select t1.id,
fullname = t1.text1 + ' ' + t1.text2
from #table1 t1
where fullname = 'one two'
This will throw this exception
Invalid column name 'fullname'
The most simple solution (imho) is this
select t.id,
t.fullname
from ( select t1.id,
fullname = t1.text1 + ' ' + t1.text2
from #table1 t1
) t
where t.fullname = 'one two'
This allows the database to fully evaluate the aliased columns, and therefore it will known them so you can use the aliased columns just like other columns

Assigning SYSDATE to prompt

So I have this line of code in my SQL:
AND ((:3 IS NOT NULL
AND A.ADM_CREATION_DT BETWEEN :3 AND SYSDATE)
OR :3 IS NULL
AND :3 = SYSDATE)
What I want it to do is that if Prompt 3 or :3 has a value it gets all the value of ADM_CREATION_DT that is in between the value of :3 and SYSDATE - this works fine. What doesnt work is the part where when :3 is blank it should set the value to SYSDATE which is the current date.
I get the error below:
Error in running query because of SQL Error, Code=8006, Message=Invalid datatype specified (50,380)
I think it has something to do with assigning SYSDATE to prompt, where their datatype is different.
Anyone who knows how to fix this? Thank you so much.
Full Query provided below:
SELECT A.EMPLID, B.NAME, A.ACAD_CAREER, G.STDNT_CAR_NBR, A.ADM_APPL_NBR, A.INSTITUTION, TO_CHAR(A.ADM_CREATION_DT,'YYYY-MM-DD'), TO_CHAR(A.ADM_UPDATED_DT,'YYYY-MM-DD'), A.ADM_APPL_COMPLETE, TO_CHAR(A.ADM_APPL_CMPLT_DT,'YYYY-MM-DD'), D.SRVC_IND_CD, D.SRVC_IND_REASON, E.UM_CTXT_WP_FLAG, E.UM_CTXT_WPPLS_FLAG
FROM (PS_ADM_APPL_DATA A LEFT OUTER JOIN PS_SAD_UC_APPLREC C ON A.EMPLID = C.EMPLID AND C.INSTITUTION = A.INSTITUTION ), PS_PERSON_NAME B, PS_SRVC_IND_DATA D, PS_UM_CTXT_PERCTXT E, PS_SAD_UC_DEC_MAT F, PS_ADM_APPL_PROG G
WHERE (( A.EMPLID = B.EMPLID
AND A.EMPLID = D.EMPLID
AND C.INSTITUTION = E.INSTITUTION
AND C.SAD_UC_APPCODE = E.SAD_UC_APPCODE
AND C.SAD_UC_PERS_ID = E.SAD_UC_PERS_ID
AND E.EFFDT =
(SELECT MAX(E_ED.EFFDT) FROM PS_UM_CTXT_PERCTXT E_ED
WHERE E.INSTITUTION = E_ED.INSTITUTION
AND E.SAD_UC_APPCODE = E_ED.SAD_UC_APPCODE
AND E.SAD_UC_PERS_ID = E_ED.SAD_UC_PERS_ID
AND E_ED.EFFDT <= SYSDATE)
AND E.EFFSEQ =
(SELECT MAX(E_ES.EFFSEQ) FROM PS_UM_CTXT_PERCTXT E_ES
WHERE E.INSTITUTION = E_ES.INSTITUTION
AND E.SAD_UC_APPCODE = E_ES.SAD_UC_APPCODE
AND E.SAD_UC_PERS_ID = E_ES.SAD_UC_PERS_ID
AND E.EFFDT = E_ES.EFFDT)
AND C.INSTITUTION = F.INSTITUTION
AND C.SAD_UC_APPCODE = F.SAD_UC_APPCODE
AND C.SAD_UC_PERS_ID = F.SAD_UC_PERS_ID
AND A.EMPLID = G.EMPLID
AND A.ACAD_CAREER = G.ACAD_CAREER
AND A.STDNT_CAR_NBR = G.STDNT_CAR_NBR
AND A.ADM_APPL_NBR = G.ADM_APPL_NBR
AND G.EFFDT =
(SELECT MAX(G_ED.EFFDT) FROM PS_ADM_APPL_PROG G_ED
WHERE G.EMPLID = G_ED.EMPLID
AND G.ACAD_CAREER = G_ED.ACAD_CAREER
AND G.STDNT_CAR_NBR = G_ED.STDNT_CAR_NBR
AND G.ADM_APPL_NBR = G_ED.ADM_APPL_NBR
AND G.APPL_PROG_NBR = G_ED.APPL_PROG_NBR
AND G_ED.EFFDT <= SYSDATE)
AND G.EFFSEQ =
(SELECT MAX(G_ES.EFFSEQ) FROM PS_ADM_APPL_PROG G_ES
WHERE G.EMPLID = G_ES.EMPLID
AND G.ACAD_CAREER = G_ES.ACAD_CAREER
AND G.STDNT_CAR_NBR = G_ES.STDNT_CAR_NBR
AND G.ADM_APPL_NBR = G_ES.ADM_APPL_NBR
AND G.APPL_PROG_NBR = G_ES.APPL_PROG_NBR
AND G.EFFDT = G_ES.EFFDT)
AND 1 = 1 AND F.SAD_UC_MAT_TEXT <> 'SATISFIED'
AND E.UM_CTXT_WP_FLAG = 'Y'
AND E.UM_CTXT_WPPLS_FLAG = 'Y'
AND D.SRVC_IND_CD = 'MC1'
AND D.SRVC_IND_CD = 'MDS'
AND A.INSTITUTION = :1
AND ( A.ACAD_CAREER = :2
OR ' ' = :2)
AND 1 = 1 AND (( A.EMPLID = :4
OR ' ' = :4
AND ( A.INSTITUTION = :1
AND A.ACAD_CAREER = :2)) )
AND 1 = 1 AND ((TO_DATE(:3,'YYYY-MM-DD') IS NOT NULL
AND A.ADM_CREATION_DT BETWEEN TO_DATE(:3,'YYYY-MM-DD') AND SYSDATE)
OR TO_DATE(:3,'YYYY-MM-DD') IS NULL
AND TO_DATE(:3,'YYYY-MM-DD') = SYSDATE) ));
Based on the assumption (you said it was correct in the question comments) that when the date is :3 is not provided you want all records till the current date, the solution is to simply use a anchor date before which you are guaranteed to never have any records. For e.g. 1-Jan-1900
So the WHERE clause becomes
A.ADM_CREATION_DT BETWEEN NVL(:3,TO_DATE('01-01-1900','DD-MM-YYYY')) AND SYSDATE
Hope that helps?
This may help.
What I understood:
1) if :3 is not null, then it should populate ADM_CREATION_DT from the :3(entered date and sysdate)
2) But in case if :3 is not null, then ADM_CREATION_DT should set to SYSDATE or less than SYSDATE, If this is the case, then please follow this.
select NVL(A.date1,SYSDATE) dt from dummytab A
where
(TO_DATE(:3,'YYYY-MM-DD') IS NOT NULL
AND A.date1 BETWEEN TO_DATE(:3,'YYYY-MM-DD') AND SYSDATE)
OR (TO_DATE(:3,'YYYY-MM-DD') IS NULL
AND TO_DATE(A.date1,'YYYY-MM-DD') <= TO_DATE(SYSDATE,'YYYY-MM-DD'));
Or
2) But in case if :3 is not null, then ADM_CREATION_DT should set to SYSDATE, If this is the case, then please follow this.
select NVL(A.date1,SYSDATE) dt from dummytab A
where
(TO_DATE(:3,'YYYY-MM-DD') IS NOT NULL
AND A.date1 BETWEEN TO_DATE(:3,'YYYY-MM-DD') AND SYSDATE)
OR (TO_DATE(:3,'YYYY-MM-DD') IS NULL);
Here date1 is your ADM_CREATION_DATE.

How to use bitwise operator in existing sql query?

Here is my sql query. I have column name "ExpenseBucketCoverage" in claim table in which I am storing bitwise operators store multiple values in one column like below
MED_COPAY = 1, MED_DED= 10, MED_COINS = 100, RX_COPAY = 1, RX_DED= 10, RX_COINS = 100
I want to replace hard coded value like MED_COPAY, MED_COINS, MED_DED, RX_DED, RX_COINS & RX_COPAY in query by using ExpenseBucketCoverage column value. Can some one please tell me how can I do that?
Someone has suggested me below soultion
retrieve data from claim and left joining the first matched record in eligibility. And then add custom code to loop through the datarows to split the rows by covered expense bucket, and set the service category code in-memory column based on the ExpenseBucketCoverage value for the claim.
SELECT
e.categoryid,
c.servicetype,
'II' AS RepordType,
e.TPAId AS TPA_Id,
e.EmployerCode,
e.SubscriberId,
e.MemberID,
c.ServiceFrom,
c.ServiceTo,
CASE
WHEN e.categoryid IN( 'MED_DED', 'RX_DED' ) THEN
deductible
WHEN e.categoryid IN( 'MED_COINS', 'RX_COINS' ) THEN
isnull(coins,0)
WHEN e.categoryid IN( 'MED_COPAY', 'RX_COPAY' ) THEN
copay
ELSE 0
END AS ClaimAmount,
'' AS AccountTypeCode,
'1' ClaimsCrossoverAutoPay,
e.CategoryId,
CASE c.ServiceType
WHEN 'H' THEN
CASE e.PayeeIndicator
WHEN 'N' THEN '0'
WHEN 'Y' THEN '1'
END
WHEN 'P' THEN '0'
END AS PayProvider,
CASE c.ServiceType
WHEN 'H' THEN
CASE PayeeIndicator
WHEN 'N' THEN '0'
WHEN 'Y' THEN '1'
END
WHEN 'P' THEN '0'
END AS ReimbursementMethod,
CASE c.ServiceType
WHEN 'H' THEN c.Provider
WHEN 'P' THEN ''
END AS ProviderId,
'1' EnforceAccountEffectiveDates,
c.Id,
c.ClaimNumber + e.CategoryId as 'ExternalClaimNumber',
c.ProviderName,
c.CarrierId + ';' + c.SourceClaimNumber AS Notes
FROM Claim c
INNER JOIN Eligibility e ON e.TPAId = c.TPAId AND e.EIN = c.EIN AND
c.Processed = 'Y' AND e.FilterType = 'Eligibility'
AND c.TPAId='PrimePay'
AND (c.ServiceFrom >= e.BenefitEffectiveDate
AND c.ServiceFrom <=e.BenefitTermDate)
AND ( ( c.PayorID = c.PatientSSN
AND e.SubscriberSSN = c.PatientSSN
AND (c.EmployeeFirstName = c.PatientFirstName
AND c.EmployeeLastName = c.PatientLastName)
AND(e.MemberSSN = '' OR e.MemberSSN = NULL)
AND(e.MemberFirstName = '' OR e.MemberFirstName = NULL)
AND(e.MemberLastName = '' OR e.MemberLastName = NULL))
OR((c.PayorID != c.PatientSSN AND e.MemberSSN = c.PatientSSN
AND e.MemberFirstName = c.PatientFirstName
AND e.MemberLastName = c.PatientLastName)
OR(c.PayorID != c.PatientSSN AND e.MemberFirstName = c.PatientFirstName
AND e.MemberLastName= c.PatientLastName)))
AND (( c.Servicetype ='P'
AND e.CategoryID IN('RX_COINS','RX_COPAY', 'RX_DED' ))
OR ( c.Servicetype = 'H'
AND e.CategoryID IN( 'MED_COINS','MED_COPAY', 'MED_DED' )))

Select query on SQL where parameter1 OR parameter2

CREATE PROCEDURE [dbo].[SPPeople]
(
#Name varchar(50) = null,
#Status1 char(1) = '0',
#Status2 char(1) = '0',
#Status3 char(1) = '0'
)
as
SELECT
People.Name, People.Age
FROM
People
WHERE
(People.Name = CASE WHEN #Name is null THEN People.Name ELSE #Name END)
AND
((People.Status1 = CASE WHEN #Status1 = '0' THEN People.Status1 ELSE #Status1 END)
OR
(People.Status2 = CASE WHEN #Status2 = '0' THEN People.Status2 ELSE #Status2 END)
OR
(People.Status3 = CASE WHEN #Status3 = '0' THEN People.Status3 ELSE #Status3 END))
If parameters #Status1 and #Status2 are equal to 1, the query should return registers that necessarily have Status1 OR Status2 equal to 1, not depending on Status3.
But the query above returns all registers when I pass #Status1 and #Status2 as 1.
Try using this logic instead:
SELECT p.Name, p.Age
FROM People p
WHERE (#Name is null OR p.Name = #Name) AND
(#status1 <> '0' and p.Status1 = #status1 or
#status2 <> '0' and p.Status2 = #status2 or
#status3 <> '0' and p.Status3 = #status3
);
Most people find it more difficult to understand case expressions in the where clause than boolean logic.
I think you can modify your WHERE condition to compare against the parameter directly like
WHERE
People.Name = #Name
AND
(
People.Status1 = #Status1
OR
People.Status2 = #Status2
OR
People.Status3 = #Status3
)

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.