March 25, 2014 at 4:09 pm
Hi
Is it possible to rewrite this function which is using cursor with set based operations ? any example how to do will be greatly appreciated.
here is the function:
ALTER FUNCTION [dbo].[fn_GetPointsForBid]
(
@bid_Id uniqueidentifier
)
RETURNS nvarchar(4000)
AS
BEGIN
-- Declare the return variable here
DECLARE @bidPoints nvarchar(4000);
DECLARE @criteriaID uniqueidentifier;
DECLARE my_cursor CURSOR FOR
SELECT CRITERIA_ID
FROM [dbo].[vw_Bid_Criterias]
WHERE BID_ID = @bid_Id
ORDER BY [dbo].[vw_Bid_Criterias].CRITERIA_NUMBER;
SET @bidPoints = '';
OPEN my_cursor
FETCH NEXT FROM my_cursor
INTO @criteriaID
IF(@@FETCH_STATUS = 0)
BEGIN
WHILE @@FETCH_STATUS = 0
BEGIN
DECLARE @textTmp nvarchar(100);
DECLARE @valueTmp decimal(8,2);
DECLARE @maxValueTmp decimal(8,2);
SELECT @textTmp = CRITERIA_NAME FROM [dbo].[vw_Bid_Criterias] WHERE CRITERIA_ID = @criteriaID and BID_ID = @bid_Id;
SELECT @maxValueTmp = Max_points FROM [dbo].[vw_Bid_Criterias] WHERE CRITERIA_ID = @criteriaID and BID_ID = @bid_Id;
SELECT @valueTmp = Calc_Points FROM [dbo].[vw_Bid_Criterias] WHERE CRITERIA_ID = @criteriaID and BID_ID = @bid_Id;
SET @bidPoints = @bidPoints + LTRIM(RTRIM(@textTmp)) + ' - ' + CONVERT(varchar(10),@valueTmp ) + '/' + CONVERT(varchar(10),@maxValueTmp) + CHAR(13);
FETCH NEXT FROM my_cursor
INTO @criteriaID
END
SET @bidPoints = LTRIM(RTRIM(@bidPoints));
END
CLOSE my_cursor;
DEALLOCATE my_cursor;
RETURN ISNULL(@bidPoints, '');
END
March 25, 2014 at 4:28 pm
I hope that you're ready for a great boost on performance. You didn't give any sample data, so there's a possibility that you won't get exactly what you're looking for but if you understand the code, you'll be able to adapt it.
There are 2 articles that you should read to understand this solution:
This one is to remove the cursor: http://www.sqlservercentral.com/articles/comma+separated+list/71700/
This one is to make the function run faster: http://www.sqlservercentral.com/articles/T-SQL/91724/
Here's what I propose:
ALTER FUNCTION [dbo].[fn_GetPointsForBid]
(
@bid_Id uniqueidentifier
)
RETURNS TABLE
AS
RETURN
SELECT bidPoints = (
SELECT LTRIM(RTRIM(CRITERIA_NAME)) + ' - '
+ CONVERT(varchar(10),Calc_Points ) + '/'
+ CONVERT(varchar(10),Max_points) + CHAR(13)
FROM [dbo].[vw_Bid_Criterias]
WHERE BID_ID = @bid_Id
ORDER BY CRITERIA_NUMBER
FOR XML PATH(''), TYPE).value('.','nvarchar(4000)');
The code is a lot shorter than the cursor and will perform a lot better. The way to call the function will change as well and you'll need to use CROSS APPLY, but it's worth it.
If you have questions after reading the articles, feel free to ask.
March 25, 2014 at 5:06 pm
Hello, I have tried my best to understand your code and come up with a solution which should work. I used xml path and few string manipulation.
Note: I could not test my sql due to the lack of test data. In future, it will be good if you include one.
-- Please provide a valid bid id
DECLARE @bid_Id UNIQUEIDENTIFIER = '????????????????????????????????????' ;
;WITH xmlListCTE(Value) AS
(
SELECT Value = LTRIM(RTRIM(bidC.CRITERIA_NAME)) + ' - ' + CONVERT(VARCHAR(10), bidC.Calc_Points) + '/' + CONVERT(VARCHAR(10), bidC.Max_points)
FROM [dbo].[vw_Bid_Criterias] bidC
WHERE bidC.BID_ID = @bid_Id
ORDER BY bidC.CRITERIA_NUMBER
FOR XML PATH ('')
)
SELECT Result = ISNULL(REPLACE(REPLACE(REPLACE(cte.Value, '</Value><Value>', CHAR(13)), '</Value>', ''), '<Value>', ''), '')
FROM xmlListCTE cte ;
March 25, 2014 at 5:09 pm
Aditya,
If you don't give an alias to the column, you don't need the nested REPLACEs. 😉
March 26, 2014 at 4:25 am
Luis Cazares (3/25/2014)
Aditya,If you don't give an alias to the column, you don't need the nested REPLACEs. 😉
how do you mean ? can you explain a bit ?
March 26, 2014 at 9:29 am
I'll try to explain it.
First, I'll create some sample data to follow along and keep it short.
CREATE TABLE #SampleData(
Criteria_Numberint,
Criteria_Namevarchar(50),
Calc_Pointsint,
Max_Pointsint);
INSERT #SampleData
SELECT 1, 'Criteria A', 5, 10 UNION ALL
SELECT 1, 'Criteria B', 13, 20 UNION ALL
SELECT 1, 'Criteria C', 70, 100;
If we run the following query using FOR XML PATH ('') and assign an alias to the column, we'll end up with the tags <Value></Value> for each row of our table.
SELECT bidPoints = (
SELECT Value = LTRIM(RTRIM(CRITERIA_NAME)) + ' - '
+ CONVERT(varchar(10),Calc_Points ) + '/'
+ CONVERT(varchar(10),Max_points) + CHAR(13)
FROM #SampleData
ORDER BY CRITERIA_NUMBER
FOR XML PATH(''));
--Result
--<Value>Criteria A - 5/10& #x0D;</Value><Value>Criteria B - 13/20& #x0D;</Value><Value>Criteria C - 70/100& #x0D;</Value>
If we remove the alias from the column, the tags will be gone as well because SQL Server won't know what field is it as it is an unnamed column
SELECT bidPoints = (
SELECT LTRIM(RTRIM(CRITERIA_NAME)) + ' - '
+ CONVERT(varchar(10),Calc_Points ) + '/'
+ CONVERT(varchar(10),Max_points) + CHAR(13)
FROM #SampleData
ORDER BY CRITERIA_NUMBER
FOR XML PATH(''));
--Result
--Criteria A - 5/10& #x0D;Criteria B - 13/20& #x0D;Criteria C - 70/100& #x0D;
Note that we still have a code representing the CHAR(13) (I added a space to it in this post to be able to show it). To remove the XML codes as this one or the ones for <, >, & and others, we use the TYPE and .value to avoid this characters being tokenized. Resulting on:
SELECT bidPoints = (
SELECT LTRIM(RTRIM(CRITERIA_NAME)) + ' - '
+ CONVERT(varchar(10),Calc_Points ) + '/'
+ CONVERT(varchar(10),Max_points) + CHAR(13)
FROM #SampleData
ORDER BY CRITERIA_NUMBER
FOR XML PATH(''), TYPE).value('.','nvarchar(4000)');
--Result
--Criteria A - 5/10
--Criteria B - 13/20
--Criteria C - 70/100
--
To avoid the final CHAR(13) we can change the order of the elements in our string concatenation and use STUFF to remove the first character.
SELECT bidPoints = STUFF((
SELECT CHAR(13) + LTRIM(RTRIM(CRITERIA_NAME)) + ' - '
+ CONVERT(varchar(10),Calc_Points ) + '/'
+ CONVERT(varchar(10),Max_points)
FROM #SampleData
ORDER BY CRITERIA_NUMBER
FOR XML PATH(''), TYPE).value('.','nvarchar(4000)'), 1, 1, '');
I hope that this becomes easier to understand.
March 26, 2014 at 5:20 pm
@SSCrazy
Thanks for explanation this was extremely helpful, couldn't find and ask for better explanation, thanks again you are awesome bro, great example!
March 26, 2014 at 5:40 pm
priestxandar (3/26/2014)
@SSCrazyThanks for explanation this was extremely helpful, couldn't find and ask for better explanation, thanks again you are awesome bro, great example!
Thank you for the feedback. It's important that you understand every piece of the code because you'll have to support it.
Just note that the name is above the avatar and SSCrazy is my "rank" at this moment. 😉
March 27, 2014 at 5:24 pm
Its me again with another cursor (more difficult i think, at least for me 🙂 ) which i need to handle it to set-based, for any help, code suggestion I'll will be grateful. This is new area for me and i'm trying to learn as much as possible, seems to be hard to understand how to rewrite, you need to know how thing works.
i'm not able to provide testing data because is hard to reproduce, since lot of tables, views and joins come in play. I know that is hard to test without data, but previous examples i got here on this thread was just excellent, exactly what i need with great explanation.
I spend my whole day today working on this to make it work but without success. So here is the code:
ALTER FUNCTION [dbo].[fn_GetSumOfPointsForBid]
(
@bid_Id uniqueidentifier
)
RETURNS decimal (8, 2)
AS
BEGIN
DECLARE @bidPoints decimal(8,2);
DECLARE @criteriaID uniqueidentifier;
DECLARE @isDirect bit;
DECLARE @cftID uniqueidentifier;
SELECT @cftID = call_for_tender_ID FROM BIDS WHERE ID = @bid_Id;
SELECT @isDirect = IsDirectAuction FROM vw_CallForTender WHERE ID = @cftID;
IF(@isDirect = 0)
BEGIN
DECLARE my_cursor CURSOR FOR
SELECT CRITERIA_ID
FROM [dbo].[vw_Bid_Criterias]
WHERE BID_ID = @bid_Id
--New section for OtherCriteria
union
select drtc.CRITERIA_ID from BID_CALCULATIONS bc
inner join CRITERIAS c on bc.criteria_ID=c.ID
inner join BIDS b on b.ID=bc.bid_ID
inner join CALL_FOR_TENDERS cft on cft.id=b.call_for_tender_ID
inner join DECISIONS_ON_REQUESTS_FOR_TENDERS_CRITERIAS drtc on drtc.decision_for_tender_id=cft.decision_on_request_for_tender_ID and drtc.criteria_id=c.ID
where bid_ID=@bid_Id and drtc.points>0 and number>13
--END --New section for OtherCriteria
SET @bidPoints = 0.00;
OPEN my_cursor
FETCH NEXT FROM my_cursor
INTO @criteriaID
IF(@@FETCH_STATUS = 0)
BEGIN
WHILE @@FETCH_STATUS = 0
BEGIN
DECLARE @valueTmp decimal(8,2);
SELECT @valueTmp = Calc_Points FROM [dbo].[vw_Bid_Criterias] WHERE CRITERIA_ID = @criteriaID and BID_ID = @bid_Id;
SET @bidPoints = @bidPoints + @valueTmp;
FETCH NEXT FROM my_cursor
INTO @criteriaID
END
END
CLOSE my_cursor;
DEALLOCATE my_cursor;
END
IF(@isDirect = 1)
BEGIN
DECLARE my_cursor CURSOR FOR
SELECT crt.ID
FROM
bids b INNER JOIN tbl_EntityInstanceCallForTender e ON b.call_for_tender_ID = e.CallForTenderID
INNER JOIN tbl_ProcessCriterias pc ON e.EntityID = pc.EntityID
INNER JOIN tbl_Criterias c ON pc.ID = c.ProcessID
INNER JOIN tbl_CriteriaTypes ct ON c.OrderNumber = ct.ID
INNER JOIN CRITERIAS crt ON ct.OrderNumber = crt.number
WHERE b.id = @bid_Id and c.IsDeleted=0
UNION
SELECT crt.ID
FROM
bids b INNER JOIN tbl_EntityInstanceLotsCallForTender e ON b.call_for_tender_ID = e.CallForTenderID
INNER JOIN tbl_ProcessCriterias pc ON e.EntityID = pc.EntityID
INNER JOIN tbl_Criterias c ON pc.ID = c.ProcessID
INNER JOIN tbl_CriteriaTypes ct ON c.OrderNumber = ct.ID
INNER JOIN CRITERIAS crt ON ct.OrderNumber = crt.number
WHERE b.id = @bid_Id and c.IsDeleted=0
--New section for OtherCriteria
union
select drtc.CRITERIA_ID from BID_CALCULATIONS bc
inner join CRITERIAS c on bc.criteria_ID=c.ID
inner join BIDS b on b.ID=bc.bid_ID
inner join CALL_FOR_TENDERS cft on cft.id=b.call_for_tender_ID
inner join DECISIONS_ON_REQUESTS_FOR_TENDERS_CRITERIAS drtc on drtc.decision_for_tender_id=cft.decision_on_request_for_tender_ID and drtc.criteria_id=c.ID
where bid_ID=@bid_Id and drtc.points>0 and number>13
SET @bidPoints = 0.00;
OPEN my_cursor
FETCH NEXT FROM my_cursor
INTO @criteriaID
IF(@@FETCH_STATUS = 0)
BEGIN
WHILE @@FETCH_STATUS = 0
BEGIN
DECLARE @valueTmp2 decimal(8,2);
SELECT @valueTmp2 = points FROM [dbo].[BID_CALCULATIONS] WHERE CRITERIA_ID = @criteriaID and BID_ID = @bid_Id;
SET @bidPoints = @bidPoints + @valueTmp2;
FETCH NEXT FROM my_cursor
INTO @criteriaID
END
END
CLOSE my_cursor;
DEALLOCATE my_cursor;
END
RETURN ISNULL(@bidPoints, 0.00);
END
Let me know guys if you need some more details i can explain, show etc...
Thanks again.
March 27, 2014 at 5:41 pm
Talk about RBAR on steroids and overcomplicating things. These cursors are just going row by row adding a value. This is totally unnecessary if you know about aggregate functions. In this case, you're looking for SUM().
Please show us what have you tried.
March 27, 2014 at 8:43 pm
Well, hello there.
Luis - thanks for linking to my article. However, I'm afraid that it doesn't do much for helping to learn how to remove cursors. For that task, my favorite articles can be found here[/url] - it should be obvious which two articles I'm referring to.
priestxandar - This latest cursor-ridden function just makes we want to say "OMG". For several reasons.
1. It's a scalar function. These don't scale well (nor do multi-statement table-valued functions). Please read this blog post[/url] for more information. If you can convert these scalar functions to inline table-valued functions, your server will be able to take a deep sigh of relief.
2. We don't have the definitions for the views that are referenced. Please script out and post the DDL for the views vw_CallForTender, [vw_Bid_Criterias] and any other views referenced in this function.
3. How are these functions being used? If you can include those queries, I bet we can make the entire process even better!
4. Luis and I both have links in our signatures for how to post data to get better help. It really does make a difference. Please do this. Or...
5. Consider hiring someone (like myself) to look at your system and re-write this code. It seems like you are in over your head, and this may well be the better method.
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
March 28, 2014 at 9:10 am
WayneS (3/27/2014)
Well, hello there.Luis - thanks for linking to my article. However, I'm afraid that it doesn't do much for helping to learn how to remove cursors. For that task, my favorite articles can be found here[/url] - it should be obvious which two articles I'm referring to.
I pointed to your article because it really helped with the first cursor. I agree that Barry's articles are great to learn how to remove cursors (it's a shame that he didn't complete the series).
Something that changes minds towards set based programming is the following phrase:
"Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."
March 28, 2014 at 5:26 pm
WayneS (3/27/2014)
Well, hello there.Luis - thanks for linking to my article. However, I'm afraid that it doesn't do much for helping to learn how to remove cursors. For that task, my favorite articles can be found here[/url] - it should be obvious which two articles I'm referring to.
priestxandar - This latest cursor-ridden function just makes we want to say "OMG". For several reasons.
1. It's a scalar function. These don't scale well (nor do multi-statement table-valued functions). Please read this blog post[/url] for more information. If you can convert these scalar functions to inline table-valued functions, your server will be able to take a deep sigh of relief.
2. We don't have the definitions for the views that are referenced. Please script out and post the DDL for the views vw_CallForTender, [vw_Bid_Criterias] and any other views referenced in this function.
3. How are these functions being used? If you can include those queries, I bet we can make the entire process even better!
4. Luis and I both have links in our signatures for how to post data to get better help. It really does make a difference. Please do this. Or...
5. Consider hiring someone (like myself) to look at your system and re-write this code. It seems like you are in over your head, and this may well be the better method.
1. First to say thanks you are trying to help me, this articles help me to understand lot of things. If i understood good, this function can not be rewritten to inline table valued because is using parameters and can not be done in single select statement.
2. As i mention before its over complicated to include test data, those views have nested views and again nested views, also and lot of joins ( very very bad design). ( i could try to script out the views, and the nested views 🙂 also and the stored procedure if that helps but i doubt)
3. This function is called in stored procedure in select statement, this function should get back SUM based on some criteria (if you look on the joins in function you will notice that).
This is the part where i'm confused most.
4. I'm new to this forum and also to sql server, i'm starting my career now, and i'm trying to learn as much as possible.
5. I'm not in position to hire anybody, i need to learn as much as i can and apply that knowledge.
it will be good if someone could make some general query/idea how could be rewritten based on the info we have right now, i will try to script out those views and proc in monday.
thanks again for being nice and helpful.
March 28, 2014 at 6:06 pm
This function can certainly be converted into an inline table-valued function. It's tricky, but it can be done and it has nothing to do with parameters as the previous one used parameters as well.
I wanted you to show what have you done even if it's incorrect to be sure that you're understanding what's going on and have some feedback of your progression. I could give you the fish, but I prefer to teach you how to fish. 😉
March 31, 2014 at 4:50 am
Luis Cazares (3/28/2014)
This function can certainly be converted into an inline table-valued function. It's tricky, but it can be done and it has nothing to do with parameters as the previous one used parameters as well.I wanted you to show what have you done even if it's incorrect to be sure that you're understanding what's going on and have some feedback of your progression. I could give you the fish, but I prefer to teach you how to fish. 😉
Here is the code i wrote:
--DECLARE @bid_Id UNIQUEIDENTIFIER;
--SET @bid_Id = '475C1532-20CC-437A-9642-B2A87D79036A'
ALTER FUNCTION [dbo].[fn_GetSumOfPointsForBidTEST1]
(
-- Add the parameters for the function here
@bid_Id UNIQUEIDENTIFIER
)
RETURNS DECIMAL(8, 2)
AS
BEGIN
DECLARE @bidPoints DECIMAL(8, 2);
DECLARE @criteriaID UNIQUEIDENTIFIER;
DECLARE @isDirect BIT;
DECLARE @cftID UNIQUEIDENTIFIER;
SELECT @cftID = call_for_tender_ID
FROM BIDS
WHERE ID = @bid_Id;
SELECT @isDirect = IsDirectAuction
FROM vw_CallForTender
WHERE ID = @cftID;
IF ( @isDirect = 0 )
BEGIN
SET @bidPoints = 0.00
DECLARE @valueTmp decimal(8,2);
SELECT @valueTmp = Calc_Points FROM [dbo].[vw_Bid_Criterias]
WHERE BID_ID = @bid_Id AND
CRITERIA_ID IN ( SELECT CRITERIA_ID
FROM [dbo].[vw_Bid_Criterias]
WHERE BID_ID = @bid_Id
--New section for OtherCriteria
UNION
SELECT drtc.CRITERIA_ID
FROM BID_CALCULATIONS bc
INNER JOIN CRITERIAS c ON bc.criteria_ID = c.ID
INNER JOIN BIDS b ON b.ID = bc.bid_ID
INNER JOIN CALL_FOR_TENDERS cft ON cft.id = b.call_for_tender_ID
INNER JOIN DECISIONS_ON_REQUESTS_FOR_TENDERS_CRITERIAS drtc ON drtc.decision_for_tender_id = cft.decision_on_request_for_tender_ID
AND drtc.criteria_id = c.ID
WHERE bid_ID = @bid_Id
AND drtc.points > 0
AND number > 13
)
SELECT @bidPoints = @bidPoints + @valueTmp
END
IF ( @isDirect = 1 )
BEGIN
SET @bidPoints = 0.00
DECLARE @valueTmp2 decimal(8,2);
SELECT @valueTmp2 = points FROM [dbo].[BID_CALCULATIONS]
WHERE bid_ID = @bid_Id AND
CRITERIA_ID IN ( SELECT crt.ID
FROM bids b
INNER JOIN tbl_EntityInstanceCallForTender e ON b.call_for_tender_ID = e.CallForTenderID
INNER JOIN tbl_ProcessCriterias pc ON e.EntityID = pc.EntityID
INNER JOIN tbl_Criterias c ON pc.ID = c.ProcessID
INNER JOIN tbl_CriteriaTypes ct ON c.OrderNumber = ct.ID
INNER JOIN CRITERIAS crt ON ct.OrderNumber = crt.number
WHERE b.id = @bid_Id
AND c.IsDeleted = 0
UNION
SELECT crt.ID
FROM bids b
INNER JOIN tbl_EntityInstanceLotsCallForTender e ON b.call_for_tender_ID = e.CallForTenderID
INNER JOIN tbl_ProcessCriterias pc ON e.EntityID = pc.EntityID
INNER JOIN tbl_Criterias c ON pc.ID = c.ProcessID
INNER JOIN tbl_CriteriaTypes ct ON c.OrderNumber = ct.ID
INNER JOIN CRITERIAS crt ON ct.OrderNumber = crt.number
WHERE b.id = @bid_Id
AND c.IsDeleted = 0
--New section for OtherCriteria
UNION
SELECT drtc.CRITERIA_ID
FROM BID_CALCULATIONS bc
INNER JOIN CRITERIAS c ON bc.criteria_ID = c.ID
INNER JOIN BIDS b ON b.ID = bc.bid_ID
INNER JOIN CALL_FOR_TENDERS cft ON cft.id = b.call_for_tender_ID
INNER JOIN DECISIONS_ON_REQUESTS_FOR_TENDERS_CRITERIAS drtc ON drtc.decision_for_tender_id = cft.decision_on_request_for_tender_ID
AND drtc.criteria_id = c.ID
WHERE bid_ID = @bid_Id
AND drtc.points > 0
AND number > 13
)
SELECT @bidPoints = @bidPoints + @valueTmp2
END
RETURN ISNULL(@bidPoints, 0.00);
END
code is working but i'm not getting same results comparing with the cursor. I've tested 50 values and only one was different.
example:
cursor value was 85 points
set-based value was 170 points
(this looks to me like somehow points are calculated twice)
anyway, am i to right path ? or i'm doing some non-sense
Thanks
UPDATE!
I found the issue about that, it was calculating and non-active bids so i've added filter to look only for active ( but the strange thing is that only on that particular bid_id was calculating and non-active values.)
anyway let me know if this is the right way i'm doing, because i've made some tests with statistic client ON and i dont see performance benefit.
Viewing 15 posts - 1 through 15 (of 20 total)
You must be logged in to reply to this topic. Login to reply