SQL working strange - sql

When I execute this SQL code:
select id, opis, vidrabota, tipprov, hitnost, valuta, drzava, zbirnaprov, tip_zbirprov, kanal
from dev_1450autoebanktip
where opis is null or Opis like case when isnull('','') = '' then Opis else '%' + '' + '%' end
and VidRabota = case when isnull('','') = '' then VidRabota else '' end
and TipProv = case when isnull(0,0) = 0 then TipProv else 0 end
and Valuta = case when isnull('','') = '' then Valuta else '' end
and drzava is null or Drzava = case when isnull('','') = '' then Drzava else '' end
I get this set of results:
But when I add one more condition (last row):
select id, opis, vidrabota, tipprov, hitnost, valuta, drzava, zbirnaprov, tip_zbirprov, kanal
from dev_1450autoebanktip
where opis is null or Opis like case when isnull('','') = '' then Opis else '%' + '' + '%' end
and VidRabota = case when isnull('','') = '' then VidRabota else '' end
and TipProv = case when isnull(0,0) = 0 then TipProv else 0 end
and Valuta = case when isnull('','') = '' then Valuta else '' end
and drzava is null or Drzava = case when isnull('','') = '' then Drzava else '' end
and KANAL = case when isnull(0,0) = 0 then KANAL else 0 end
I am losing one row in the result. What is causing this change?

Kanal is NULL in the last row.
and KANAL = case when isnull(0,0) = 0 then KANAL else 0 end
boils down to
and KANAL = KANAL
But this is not true for NULL, because NULL is the unknown value. When comparing null with null the result is neither true nor false, but unknown. Thus the added criteria dismisses the last record.
There is one thing I'd like to add: Use parentheses when mixing AND and OR. For instance
a = b or a = c and d = e
means
a = b or (a = c and d = e)
because AND has precedence over OR and you may want the expression to mean
(a = b or a = c) and d = e
Use parentheses in order to avoid any mistakes.

Related

Concatenation by using CASE statement

I want to CONCAT between this on my SELECT statement in SQL Server.
All the columns are booleans.
The output will be like this if all of columns that are true
GGPNES
I tried to use this and it doesn't work
DECLARE #concat VARCHAR(40) ='';
SELECT
Smoke, Invoice,
Party, Summary,
MySelf, Export,
CASE
WHEN MyTable.Smoke = 1 OR MyTable.Invoice = 1
THEN #concat + 'GG'
WHEN MyTable.Party = 1
THEN #concat + 'P'
WHEN MyTable.Summary = 1
THEN #concat + 'N'
WHEN MyTable.MySelf = 1
THEN #concat + 'E'
WHEN MyTable.Export = 1
THEN #concat + 'S'
END
FROM
MyTable
I think that you want a concatenation of CASE expressions:
CASE WHEN Smoke = 1 OR Invoice = 1 THEN 'GG' ELSE '' END +
CASE WHEN Party = 1 THEN 'P' ELSE '' END +
CASE WHEN Summary = 1 THEN 'N' ELSE '' END +
CASE WHEN MySelf = 1 THEN 'E' ELSE '' END +
CASE WHEN Export = 1 THEN 'S' ELSE '' END

TSQL CONVERT + CASE functions

I'm running into a problem with my query below. I'm using case to input a value of -- when a field is left blank. I can update the value of -- to 0 instead but i'm still getting an error message when the query executes, I suspect it's because the addition cannot complete with null/blank values.
I'm having the same problem with UPDATE statements.
INSERT:
INSERT INTO AME_GridF18
(SubmissionID,TS_Added,ProjectNumber,ProjectName,TypeofWork,Mile,Toll,Park,MonRT,MonOT,TuesRT,TuesOT,WedsRT,WedsOT,ThursRT,ThursOT,FriRT,FriOT,Saturday,Sunday,Total)
VALUES
(
'[querystring:SubmissionID]'
,'[DateTime:Now]'
,case when '[ProjectNumber]' = '' then '--' else '[ProjectNumber]' end
,case when '[ProjectName]' = '' then '--' else '[ProjectName]' end
,case when '[TypeofWork]' = '' then '--' else '[TypeofWork]' end
,case when '[Mile]' = '' then '--' else '[Mile]' end
,case when '[Toll]' = '' then '--' else '[Toll]' end
,case when '[Park]' = '' then '--' else '[Park]' end
,case when '[MonRT]' = '' then '--' else '[MonRT]' end
,case when '[MonOT]' = '' then '--' else '[MonOT]' end
,case when '[TuesRT]' = '' then '--' else '[TuesRT]' end
,case when '[TuesOT]' = '' then '--' else '[TuesOT]' end
,case when '[WedsRT]' = '' then '--' else '[WedsRT]' end
,case when '[WedsOT]' = '' then '--' else '[WedsOT]' end
,case when '[ThursRT]' = '' then '--' else '[ThursRT]' end
,case when '[ThursOT]' = '' then '--' else '[ThursOT]' end
,case when '[FriRT]' = '' then '--' else '[FriRT]' end
,case when '[FriOT]' = '' then '--' else '[FriOT]' end
,case when '[Saturday]' = '' then '--' else '[Saturday]' end
,case when '[Sunday]' = '' then '--' else '[Sunday]' end
,CONVERT(Varchar, ([MonRT] + [MonOT] + [TuesRT] + [TuesOT] + [WedsRT] + [WedsOT] + [ThursRT] + [ThursOT] + [FriRT] + [FriOT] + [Saturday] + [Sunday])
))
UPDATE:
UPDATE AME_GridF18
SET
SubmissionID = '[querystring:SubmissionID]',
TS_Added = '[DateTime:Now]',
ProjectNumber = '[ProjectNumber]',
ProjectName = '[ProjectName]',
TypeofWork = '[TypeofWork]',
Mile = '[Mile]',
Toll = '[Toll]',
Park = '[Park]',
MonRT = '[MonRT]',
MonOT = '[MonOT]',
TuesRT = '[TuesRT]',
TuesOT = '[TuesOT]',
WedsRT = '[WedsRT]',
WedsOT = '[WedsOT]',
ThursRT = '[ThursRT]',
ThursOT = '[ThursOT]',
FriRT = '[FriRT]',
FriOT = '[FriOT]',
Saturday = '[Saturday]',
Sunday = '[Sunday]',
Total = CONVERT(Varchar, ([MonRT] + [MonOT] + [TuesRT] + [TuesOT] + [WedsRT] + [WedsOT] + [ThursRT] + [ThursOT] + [FriRT] + [FriOT] + [Saturday] + [Sunday]))
WHERE ID = [ID]
Maybe you're trying to do this?
DECLARE
#SubmissionID INT
,#TS_Added SMALLDATETIME
,#ProjectNumber VARCHAR(MAX)
,#ProjectName VARCHAR(MAX)
,#TypeofWork VARCHAR(MAX)
,#Mile FLOAT
,#Toll FLOAT
,#Park FLOAT
,#MonRT FLOAT
,#MonOT FLOAT
,#TuesRT FLOAT
,#TuesOT FLOAT
,#WedsRT FLOAT
,#WedsOT FLOAT
,#ThursRT FLOAT
,#ThursOT FLOAT
,#FriRT FLOAT
,#FriOT FLOAT
,#Saturday FLOAT
,#Sunday FLOAT
,#Total FLOAT
INSERT INTO AME_GridF18
(
SubmissionID
,TS_Added
,ProjectNumber
,ProjectName
,TypeofWork
,Mile
,Toll
,Park
,MonRT
,MonOT
,TuesRT
,TuesOT
,WedsRT
,WedsOT
,ThursRT
,ThursOT
,FriRT
,FriOT
,Saturday
,Sunday
,Total
)
SELECT
#SubmissionID
,GETDATE()
,CASE WHEN ISNULL(#ProjectNumber,'') = '' THEN '--' ELSE #ProjectNumber END
,CASE WHEN ISNULL(#ProjectName,'') = '' THEN '--' ELSE #ProjectName END
,CASE WHEN ISNULL(#TypeofWork,'') = '' THEN '--' ELSE #TypeofWork END
,CASE WHEN ISNULL(#Mile,0) =0 THEN '--' ELSE CAST(#Mile AS VARCHAR(30)) END
,CASE WHEN ISNULL(#Toll,0) = 0 THEN '--' ELSE CAST(#Toll AS VARCHAR(30)) END
,CASE WHEN ISNULL(#Park,0) = 0 THEN '--' ELSE CAST(#Park AS VARCHAR(30)) END
,CASE WHEN ISNULL(#MonRT,0) = 0 THEN '--' ELSE CAST(#MonRT AS VARCHAR(30)) END
,CASE WHEN ISNULL(#MonOT,0) = 0 THEN '--' ELSE CAST(#MonOT AS VARCHAR(30)) END
,CASE WHEN ISNULL(#TuesRT,0) = 0 THEN '--' ELSE CAST(#TuesRT AS VARCHAR(30)) END
,CASE WHEN ISNULL(#TuesOT,0) = 0 THEN '--' ELSE CAST(#TuesOT AS VARCHAR(30)) END
,CASE WHEN ISNULL(#WedsRT,0) = 0 THEN '--' ELSE CAST(#WedsRT AS VARCHAR(30)) END
,CASE WHEN ISNULL(#WedsOT,0) = 0 THEN '--' ELSE CAST(#WedsOT AS VARCHAR(30)) END
,CASE WHEN ISNULL(#ThursRT,0) = 0 THEN '--' ELSE CAST(#ThursRT AS VARCHAR(30)) END
,CASE WHEN ISNULL(#ThursOT,0) = 0 THEN '--' ELSE CAST(#ThursOT AS VARCHAR(30)) END
,CASE WHEN ISNULL(#FriRT,0) = 0 THEN '--' ELSE CAST(#FriRT AS VARCHAR(30)) END
,CASE WHEN ISNULL(#FriOT,0) = 0 THEN '--' ELSE CAST(#FriOT AS VARCHAR(30)) END
,CASE WHEN ISNULL(#Saturday,0) = 0 THEN '--' ELSE CAST(#Saturday AS VARCHAR(30)) END
,CASE WHEN ISNULL(#Sunday,0) = 0 THEN '--' ELSE CAST(#Sunday AS VARCHAR(30)) END
,CAST((#MonRT + #MonOT + #TuesRT + #TuesOT + #WedsRT + #WedsOT + #ThursRT + #ThursOT + #FriRT + #FriOT + #Saturday + #Sunday) AS VARCHAR(MAX))
The reason I used CASE is because if the user did not specify a value for any of the weekdays it needed to result in 0. My problem was during the calculation on INSERT if the user left any inputs blank the SQL would run like: 1+1++1+1 instead of 1+1+0+1+1 and would throw an error.
I ended up switching all my week fields to Integer and setup an auto-calculation on the table itself to calculate the total field and just disregarded that on my INSERT statement, which works as needed.

Combining SQL string alias into comma delimited string

I am working on this stored proc which check to see which BIT fields are true and depending on it assigns a text to it using alias name. I want to be able to combine these alias values (string) and have it comma delimited to I can display it on my SSRS report.
Below is part of my stored proc.
,CASE
WHEN sr.[ReservationAlreadySentAttached] = 1 THEN 'Reservation Already Sent'
END AS ReservationAttached
,CASE
WHEN sr.[HPOfficeAttached] = 1 THEN 'H&P Office'
END AS HPOfficeAttached
WHEN sr.[NotesAttached] = 1 THEN 'Notes'
END AS NotesAttached
,CASE
WHEN sr.[OpPermitAttached] = 1 THEN 'Op Permit'
END AS OpPermitAttached
,CASE
WHEN sr.[TestResultsAttached] = 1 THEN 'Test Results'
END AS TestResultsAttached
,CASE
WHEN sr.[ConsultationReportsAttached] = 1 THEN 'Consultation Reports'
END AS ConsultationReportsAttached
,CASE
WHEN sr.[OtherAttached] = 1 THEN 'Other'
END AS OtherAttached
so lets say in case where only NotesAttached and ReservationAlreadySentAttached were only true ones then I want the end result to come out as Notes, Reservation Already Sent.
How do i concatenate these aliases ?
Another option is to build your string and then just remove the leading comma with STUFF()
Example (abbreviated)
Declare #YourTable table (ReservationAlreadySentAttached bit,HPOfficeAttached bit,NotesAttached bit)
Insert Into #YourTable values
(1,0,1)
Select NewValue = stuff(
case when ReservationAlreadySentAttached = 1 then ', Reservation Already Sent' else '' end
+case when HPOfficeAttached = 1 then ', H&P Office' else '' end
+case when NotesAttached = 1 then ', Notes' else '' end
,1,2,'')
From #YourTable
Returns
NewValue
Reservation Already Sent, Notes
EDIT - Just the Expression
NewValue = stuff(
case when sr.[ReservationAlreadySentAttached] = 1 then ', Reservation Already Sent' else '' end
+case when sr.[HPOfficeAttached] = 1 then ', H&P Office' else '' end
+case when sr.[NotesAttached] = 1 then ', Notes' else '' end
,1,2,'')
Not the prettiest solution...
RIGHT((CASE
WHEN sr.[ReservationAlreadySentAttached] = 1
THEN ',Reservation Already Sent'
END + CASE
WHEN sr.[HPOfficeAttached] = 1
THEN ',H&P Office'
END + CASE
WHEN sr.[NotesAttached] = 1
THEN ',Notes'
END + CASE
WHEN sr.[OpPermitAttached] = 1
THEN ',Op Permit'
END + CASE
WHEN sr.[TestResultsAttached] = 1
THEN ',Test Results'
END + CASE
WHEN sr.[ConsultationReportsAttached] = 1
THEN ',Consultation Reports'
END + CASE
WHEN sr.[OtherAttached] = 1
THEN ',Other'
END)
,LEN(CASE
WHEN sr.[ReservationAlreadySentAttached] = 1
THEN ',Reservation Already Sent'
END + CASE
WHEN sr.[HPOfficeAttached] = 1
THEN ',H&P Office'
END + CASE
WHEN sr.[NotesAttached] = 1
THEN ',Notes'
END + CASE
WHEN sr.[OpPermitAttached] = 1
THEN ',Op Permit'
END + CASE
WHEN sr.[TestResultsAttached] = 1
THEN ',Test Results'
END + CASE
WHEN sr.[ConsultationReportsAttached] = 1
THEN ',Consultation Reports'
END + CASE
WHEN sr.[OtherAttached] = 1
THEN ',Other'
END) - 1 )
Edit: You may have to take into consideration when NONE of them are 1 so you don't get Invalid length parameter passed to the right function.
CASE
WHEN sr.[ReservationAlreadySentAttached] = 1 THEN ', Reservation Already Sent' ELSE ''
END
+ CASE
WHEN sr.[HPOfficeAttached] = 1 THEN ', H&P Office' ELSE ''
END
+ CASE etc...
And wrap this in some function to remove the first comma + space.

SQL add conversion of date to existing query

I'm would like to convert the date using this function. I'm using SQL server 2014
CONVERT(varchar, sc.StartDate,103) + '" + " " + "' + '" + " - " + "' + CONVERT(varchar, sc.EndDate,103) SIPDate
so it will display in dd/mm/yyyy - dd/mm/yyyy. How do i add it into the query? Thanks.
The query is:
SELECT CONCAT(sc.StartDate, sc.ENDDate) SIPDate,
COUNT (sj.LOComment) WeekReviewed,
COUNT(sjd.WeekNo) TotalWeek,
SUM(sjj.TotalDaysRecord) TotalDaysRecord,
COUNT(CASE sjd.JournalStatusCode WHEN 'D' THEN 1 ELSE NULL END) PENDingComplete
FROM StudentJournalDate sjd
LEFT JOIN StudentJournal sj
ON sjd.WeekNo = sj.WeekNo
LEFT OUTER JOIN
(
SELECT sj.WeekNo,
CASE WHEN RTRIM(sj.Day1Journal) = '' OR sj.Day1Journal IS NULL THEN 0 ELSE 1 END +
CASE WHEN RTRIM(sj.Day2Journal) = '' OR sj.Day2Journal IS NULL THEN 0 ELSE 1 END +
CASE WHEN RTRIM(sj.Day3Journal) = '' OR sj.Day3Journal IS NULL THEN 0 ELSE 1 END +
CASE WHEN RTRIM(sj.Day4Journal) = '' OR sj.Day4Journal IS NULL THEN 0 ELSE 1 END +
CASE WHEN RTRIM(sj.Day5Journal) = '' OR sj.Day5Journal IS NULL THEN 0 ELSE 1 END +
CASE WHEN RTRIM(sj.Day6Journal) = '' OR sj.Day6Journal IS NULL THEN 0 ELSE 1 END +
CASE WHEN RTRIM(sj.Day7Journal) = '' OR sj.Day7Journal IS NULL THEN 0 ELSE 1 END AS TotalDaysRecord
FROM StudentJournal sj
) AS sjj
ON sjj.WeekNo = sj.WeekNo
LEFT OUTER JOIN StudentSIP sc
ON sc.AdminNo = sjd.AdminNo
GROUP BY CONCAT(sc.StartDate, sc.ENDDate)
You can just SELECT and GROUP BY this formatted date expression, i.e.:
SELECT CONVERT(varchar, sc.StartDate, 103) + " - " + CONVERT(varchar, sc.EndDate, 103) AS SIPDate,
COUNT (sj.LOComment) WeekReviewed,
COUNT(sjd.WeekNo) TotalWeek,
SUM(sjj.TotalDaysRecord) TotalDaysRecord,
COUNT(CASE sjd.JournalStatusCode WHEN 'D' THEN 1 ELSE NULL END) PENDingComplete
FROM StudentJournalDate sjd
LEFT JOIN StudentJournal sj
ON sjd.WeekNo = sj.WeekNo
LEFT OUTER JOIN
(
SELECT sj.WeekNo,
CASE WHEN RTRIM(sj.Day1Journal) = '' OR sj.Day1Journal IS NULL THEN 0 ELSE 1 END +
CASE WHEN RTRIM(sj.Day2Journal) = '' OR sj.Day2Journal IS NULL THEN 0 ELSE 1 END +
CASE WHEN RTRIM(sj.Day3Journal) = '' OR sj.Day3Journal IS NULL THEN 0 ELSE 1 END +
CASE WHEN RTRIM(sj.Day4Journal) = '' OR sj.Day4Journal IS NULL THEN 0 ELSE 1 END +
CASE WHEN RTRIM(sj.Day5Journal) = '' OR sj.Day5Journal IS NULL THEN 0 ELSE 1 END +
CASE WHEN RTRIM(sj.Day6Journal) = '' OR sj.Day6Journal IS NULL THEN 0 ELSE 1 END +
CASE WHEN RTRIM(sj.Day7Journal) = '' OR sj.Day7Journal IS NULL THEN 0 ELSE 1 END AS TotalDaysRecord
FROM StudentJournal sj
) AS sjj
ON sjj.WeekNo = sj.WeekNo
LEFT OUTER JOIN StudentSIP sc
ON sc.AdminNo = sjd.AdminNo
GROUP BY CONVERT(varchar, sc.StartDate, 103) + " - " + CONVERT(varchar, sc.EndDate, 103)
Try this. since your SQL SERVER is 2014 you can use format to format the date
SELECT CONCAT(FORMAT(sc.StartDate,'dd/MM/yyy'),+ ' - ' +FORMAT(sc.ENDDate,'dd/MM/yyy')) SIPDate,
COUNT (sj.LOComment) WeekReviewed,
COUNT(sjd.WeekNo) TotalWeek,
SUM(sjj.TotalDaysRecord) TotalDaysRecord,
COUNT(CASE sjd.JournalStatusCode WHEN 'D' THEN 1 ELSE NULL END) PENDingComplete
FROM StudentJournalDate sjd
LEFT JOIN StudentJournal sj
ON sjd.WeekNo = sj.WeekNo
LEFT OUTER JOIN
(
SELECT sj.WeekNo,
CASE WHEN RTRIM(sj.Day1Journal) = '' OR sj.Day1Journal IS NULL THEN 0 ELSE 1 END +
CASE WHEN RTRIM(sj.Day2Journal) = '' OR sj.Day2Journal IS NULL THEN 0 ELSE 1 END +
CASE WHEN RTRIM(sj.Day3Journal) = '' OR sj.Day3Journal IS NULL THEN 0 ELSE 1 END +
CASE WHEN RTRIM(sj.Day4Journal) = '' OR sj.Day4Journal IS NULL THEN 0 ELSE 1 END +
CASE WHEN RTRIM(sj.Day5Journal) = '' OR sj.Day5Journal IS NULL THEN 0 ELSE 1 END +
CASE WHEN RTRIM(sj.Day6Journal) = '' OR sj.Day6Journal IS NULL THEN 0 ELSE 1 END +
CASE WHEN RTRIM(sj.Day7Journal) = '' OR sj.Day7Journal IS NULL THEN 0 ELSE 1 END AS TotalDaysRecord
FROM StudentJournal sj
) AS sjj
ON sjj.WeekNo = sj.WeekNo
LEFT OUTER JOIN StudentSIP sc
ON sc.AdminNo = sjd.AdminNo
GROUP BY CONCAT(FORMAT(sc.StartDate,'dd/MM/yyy'),+ ' - ' +FORMAT(sc.ENDDate,'dd/MM/yyy'))

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.