January 16, 2019 at 2:59 pm
I'm trying to figure out if there's a way to get rid of a cursor in a stored procedure that I'm working on.
The procedure does the following steps:
I think I could figure out how to steps 1 and 2 within the cursor in a single operation. What I can't figure out how to do is step 3 outside of the cursor given that I need to pull a different number of records for each operator.
This is in SQL 2008 R2.
Any suggestions would be welcome. I'm not looking for exact code (that's my job), but just any clues as to how to do this differently.
Sorry, this is so long.
January 16, 2019 at 3:59 pm
Yes, you can. You want to use a CROSS APPLY with a TOP clause and use a CASE expression in the TOP clause. Post table definitions and sample data, and someone will be able to work up some tested code. The first link in my signature tells you how.
Drew
J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
January 16, 2019 at 6:39 pm
You don't need a temporary table to store distinct OperatorIds, you can do it all with CTE. Though sometimes it will give you better performance if you create a temporary table and put indexes on it.Something like this should do it:
DECLARE @ProportionToDisplay decimal(3,2) = 0.02 /* 2% */
;WITH CTE AS
(
SELECT OperatorId, Col1, Col2, Col3
FROM dbo.myTable
WHERE 1=1 --[Search Criteria For Rows]
),
CTE2 AS
(
SELECT OperatorId, Count(*) NumberOfRows
FROM CTE
GROUP BY OperatorId
)
SELECT A.OperateId , B.Col1, B.Col2, B.Col3
FROM CTE2 A
CROSS APPLY(SELECT TOP(CASE WHEN CONVERT(int,B.NumberOfRows*@ProportionToDisplay) < 5 THEN 5 ELSE CONVERT(int,B.NumberOfRows*@ProportionToDisplay) END)
B.Col1, B.Col2, B.Col3
FROM CTE B
WHERE B.OperatorId = A.OperatorId) B
January 16, 2019 at 6:41 pm
I think the above two replies are enough to get me going. Unfortunately, I have to work on another urgent problem. But once I get back to this and try it out, I'll let you know how things go.
Thanks much to both of you.
August 31, 2019 at 7:04 pm
I realize this is a very, very late reply. I wasn't able to get back to the stored procedure right away. However, your answers did help me get rid of the cursor. I ended up using a CTE and a cross apply.
I meant to get back to you but then I ended up getting really sick and then got overwhelmed at work trying to catch up.
So, this is a very belated thank you. I really did appreciate your help.
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply