Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Addition within cursor

Author  Topic 

vincent kelly
Starting Member

3 Posts

Posted - 2013-05-08 : 09:22:49
Problem: (@num1 + @num2) return nothing, even though there is underlying data:


DECLARE contact CURSOR FOR SELECT [Order], [Customer] FROM [MasterConsole].[dbo].[main] WHERE (((main.SLA) Is Null))
Declare @CusID varchar(100)
DECLARE @Ord INT;
DECLARE @OrderDate DATE;
DECLARE @num1 int;
DECLARE @num2 int;

OPEN contact;
FETCH NEXT FROM contact INTO @Ord, @CusID;
WHILE @@FETCH_STATUS = 0
BEGIN

SELECT days AS '@num1' FROM [MasterConsole].[dbo].[sla] WHERE [sla].Customer = @CusID

SELECT COUNTED AS '@num2' FROM [MasterConsole].[dbo].Calender WHERE Calender.DATE = (SELECT Main.[Date Received] FROM [MasterConsole].[dbo].[main] WHERE main.[Order] = @Ord AND SLA Is Null)

UPDATE [MasterConsole].[dbo].[main] SET main.SLA = (SELECT Min([Date]) FROM [MasterConsole].[dbo].Calender WHERE [COUNTED] = (@num1 + @num2) AND main.[Order]= @Ord);


END;
CLOSE contact;

nelliegoat

MIK_2008
Master Smack Fu Yak Hacker

1054 Posts

Posted - 2013-05-08 : 10:25:28
quote:
Originally posted by vincent kelly

Problem: (@num1 + @num2) return nothing, even though there is underlying data:


DECLARE contact CURSOR FOR SELECT [Order], [Customer] FROM [MasterConsole].[dbo].[main] WHERE (((main.SLA) Is Null))
Declare @CusID varchar(100)
DECLARE @Ord INT;
DECLARE @OrderDate DATE;
DECLARE @num1 int;
DECLARE @num2 int;

OPEN contact;
FETCH NEXT FROM contact INTO @Ord, @CusID;
WHILE @@FETCH_STATUS = 0
BEGIN

SELECT @num1 = days AS '@num1' FROM [MasterConsole].[dbo].[sla] WHERE [sla].Customer = @CusID

SELECT @num2=COUNTED AS '@num2' FROM [MasterConsole].[dbo].Calender WHERE Calender.DATE = (SELECT Main.[Date Received] FROM [MasterConsole].[dbo].[main] WHERE main.[Order] = @Ord AND SLA Is Null)

UPDATE [MasterConsole].[dbo].[main] SET main.SLA = (SELECT Min([Date]) FROM [MasterConsole].[dbo].Calender WHERE [COUNTED] = (@num1 + @num2) AND main.[Order]= @Ord);


END;
CLOSE contact;

nelliegoat



Try to use a SET based approch for update/inserts (if possible - which does in most cases) and avoid the use of Cursors. Because SET based approach is way faster than Cursors.

Cheers
MIK
Go to Top of Page

vincent kelly
Starting Member

3 Posts

Posted - 2013-05-08 : 12:17:24
MIK_2008: Your answer works great for the first order, but it doesn't step onto the next ones. Can you give me some idea of how to use an alternative to the cursor (I can see how slow it works) to accomplish what I am trying here (I am very new to TSQL!)

nelliegoat
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-05-08 : 13:02:18
something like


UPDATE m
m.SLA = c2.MinDate
FROM [MasterConsole].[dbo].[main] m
INNER JOIN [MasterConsole].[dbo].[sla] s
ON s.Customer = m.Customer
INNER JOIN [MasterConsole].[dbo].Calender c1
ON c1.DATE = m.[Date Received]
INNER JOIN (SELECT [Order],[COUNTED], Min([Date]) AS MinDate
FROM [MasterConsole].[dbo].Calender
GROUP BY [Order],[COUNTED]
)c2
ON c2.[Order] = m.[Order]
AND c2.[COUNTED] = s.Days + c1.COUNTED
WHERE m.SLA IS NULL


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

vincent kelly
Starting Member

3 Posts

Posted - 2013-05-10 : 10:52:22
with a little modification worked just fine, many thanks visakh16

nelliegoat
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-05-13 : 02:22:42
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -