January 20, 2017 at 9:50 am
Hi I have the following query with multiple exists run against the same table.
Can I merger all these exists statements into one?
Following is the sample code.
IF EXISTS (
SELECT TOP 1 EmplyeeNumber
FROM EmplyeeCodes
WHERE EmplyeeNumber = @EmplyeeNumber
AND AmountRemaining > 0
AND ItemNumber IN('ACCT','ACCT-STR','ACCT-SLR','ACCT-UDCC','ACCT-VLIT')
)
BEGIN
SET @DeptCodes = @DeptCodes + 'ACCTEmplyeeBillpartiallyPAid,'
END
IF EXISTS (
SELECT TOP 1 EmplyeeNumber
FROM EmplyeeCodes
WHERE EmplyeeNumber = @EmplyeeNumber
AND DATEDIFF(DAY, DatePaid, GETDATE()) <= 17
AND AmountRemaining = 0
AND TotalAmount > 0
AND ItemNumber IN('ACCT','ACCT-MBRSHP','ACCT-SLR','ACCT-UDCC','ACCT-STL')
)
BEGIN
SELECT @DeptCodes = @DeptCodes + 'ACCTBillFullyPaid,'
END
IF EXISTS (SELECT TOP 1 mo.EmplyeeId
FROM EmplyeeCodes i
JOIN ACCT.Emplyeeship.Emplyeedept mo on i.EmplyeeID = mo.EmplyeeID and i.deptNumber = mo.deptID
where mo.deptID = @deptNumber and
i.AmountRemaining > 0 and
i.ItemNumber IN('ACCT','ACCT-MBRSHP','ACCT-SLR','ACCT-STL','ACCT-IDR','ACCT-NTR','ACCTN-RDT')
)
BEGIN
SELECT @DeptCodes = @DeptCodes + 'ACCTEmplyeeBillPending,'
END
Thanks.
January 20, 2017 at 11:40 am
sql_2005_fan - Friday, January 20, 2017 9:50 AMHi I have the following query with multiple exists run against the same table.Can I merger all these exists statements into one?
Following is the sample code.
IF EXISTS (
SELECT TOP 1 EmplyeeNumber
FROM EmplyeeCodes
WHERE EmplyeeNumber = @EmplyeeNumber
AND AmountRemaining > 0
AND ItemNumber IN('ACCT','ACCT-STR','ACCT-SLR','ACCT-UDCC','ACCT-VLIT')
)
BEGIN
SET @DeptCodes = @DeptCodes + 'ACCTEmplyeeBillpartiallyPAid,'
ENDIF EXISTS (
SELECT TOP 1 EmplyeeNumber
FROM EmplyeeCodes
WHERE EmplyeeNumber = @EmplyeeNumber
AND DATEDIFF(DAY, DatePaid, GETDATE()) <= 17
AND AmountRemaining = 0
AND TotalAmount > 0
AND ItemNumber IN('ACCT','ACCT-MBRSHP','ACCT-SLR','ACCT-UDCC','ACCT-STL')
)
BEGIN
SELECT @DeptCodes = @DeptCodes + 'ACCTBillFullyPaid,'
ENDIF EXISTS (SELECT TOP 1 mo.EmplyeeId
FROM EmplyeeCodes i
JOIN ACCT.Emplyeeship.Emplyeedept mo on i.EmplyeeID = mo.EmplyeeID and i.deptNumber = mo.deptID
where mo.deptID = @deptNumber and
i.AmountRemaining > 0 and
i.ItemNumber IN('ACCT','ACCT-MBRSHP','ACCT-SLR','ACCT-STL','ACCT-IDR','ACCT-NTR','ACCTN-RDT')
)
BEGIN
SELECT @DeptCodes = @DeptCodes + 'ACCTEmplyeeBillPending,'
ENDThanks.
Why? They have different functionality. It seems right for them to be different.
January 20, 2017 at 11:58 am
The only thing I would suggest is using END ELSE IF BEGIN as, unless I am mistaken, in your logic only one option will ever be valid. This means if a prior IF requirement is met, the others won't be checked.
Thom~
Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
Larnu.uk
January 23, 2017 at 5:48 am
Maybe this one:
;WITH cte as (
SELECT TOP 1 'ACCTEmplyeeBillpartiallyPAid,' as string, 1 as orderby
FROM EmplyeeCodes
WHERE EmplyeeNumber = @EmplyeeNumber
AND AmountRemaining > 0
AND ItemNumber IN('ACCT','ACCT-STR','ACCT-SLR','ACCT-UDCC','ACCT-VLIT')
union all
SELECT TOP 1 'ACCTBillFullyPaid,', 2 as orderby
FROM EmplyeeCodes
WHERE EmplyeeNumber = @EmplyeeNumber
AND DATEDIFF(DAY, DatePaid, GETDATE()) <= 17
AND AmountRemaining = 0
AND TotalAmount > 0
AND ItemNumber IN('ACCT','ACCT-MBRSHP','ACCT-SLR','ACCT-UDCC','ACCT-STL')
union all
SELECT TOP 1 'ACCTEmplyeeBillPending,', 3 as orderby
FROM EmplyeeCodes i
JOIN ACCT.Emplyeeship.Emplyeedept mo on i.EmplyeeID = mo.EmplyeeID and i.deptNumber = mo.deptID
where mo.deptID = @deptNumber and
i.AmountRemaining > 0 and
i.ItemNumber IN('ACCT','ACCT-MBRSHP','ACCT-SLR','ACCT-STL','ACCT-IDR','ACCT-NTR','ACCTN-RDT')
)
select
@DeptCodes = @DeptCodes + string
from cte
order by orderby
January 24, 2017 at 4:42 am
The reason why I wanted to do this is.. I wanted to minimize the usage of the view 'EmplyeeCodes'.
Each time i used 'exists' this object is queried once... I was thinking if there is way I can check all the condition with one select.
Unfortunately, I was not able to think a way of doing this.
Thanks.
January 24, 2017 at 4:48 am
sql_2005_fan - Tuesday, January 24, 2017 4:42 AMThe reason why I wanted to do this is.. I wanted to minimize the usage of the view 'EmplyeeCodes'.
Each time i used 'exists' this object is queried once... I was thinking if there is way I can check all the condition with one select.Unfortunately, I was not able to think a way of doing this.
Thanks.
Your statements are all quite different, so 3 does seem the logical choice here. If you are meant to only get one result out of the 3, then using END ELSE IF BEGIN will cut them down.
Are you saying that your query runs slowly then? Perhaps the problem is not with doing 3 EXiSTS, and that being slow, but that your VIEW is, and that needs a review.
Thom~
Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
Larnu.uk
January 24, 2017 at 5:03 am
sql_2005_fan - Tuesday, January 24, 2017 4:42 AMThe reason why I wanted to do this is.. I wanted to minimize the usage of the view 'EmplyeeCodes'.
Each time i used 'exists' this object is queried once... I was thinking if there is way I can check all the condition with one select.Unfortunately, I was not able to think a way of doing this.
Thanks.
You can easily reduce the number of reads...
SELECT
DatePaid, TotalAmount, AmountRemaining, ItemNumber
INTO #EmployeeCodes
FROM EmplyeeCodes
WHERE EmplyeeNumber = @EmplyeeNumber
IF EXISTS (
SELECT 1
FROM #EmplyeeCodes
WHERE AmountRemaining > 0
AND ItemNumber IN ('ACCT','ACCT-STR','ACCT-SLR','ACCT-UDCC','ACCT-VLIT')
)
BEGIN
SET @DeptCodes = @DeptCodes + 'ACCTEmplyeeBillpartiallyPAid,'
END
IF EXISTS (
SELECT 1
FROM #EmplyeeCodes
WHERE DATEDIFF(DAY, DatePaid, GETDATE()) <= 17
AND TotalAmount > 0
AND AmountRemaining = 0
AND ItemNumber IN ('ACCT','ACCT-MBRSHP','ACCT-SLR','ACCT-UDCC','ACCT-STL')
)
BEGIN
SELECT @DeptCodes = @DeptCodes + 'ACCTBillFullyPaid,'
END
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply