SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Addition within cursor
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

vincent kelly
Starting Member

Ireland
3 Posts

Posted - 05/08/2013 :  09:22:49  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

Pakistan
1054 Posts

Posted - 05/08/2013 :  10:25:28  Show Profile  Reply with Quote
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

Edited by - MIK_2008 on 05/08/2013 10:26:22
Go to Top of Page

vincent kelly
Starting Member

Ireland
3 Posts

Posted - 05/08/2013 :  12:17:24  Show Profile  Reply with Quote
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

India
52325 Posts

Posted - 05/08/2013 :  13:02:18  Show Profile  Reply with Quote
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

Ireland
3 Posts

Posted - 05/10/2013 :  10:52:22  Show Profile  Reply with Quote
with a little modification worked just fine, many thanks visakh16

nelliegoat
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 05/13/2013 :  02:22:42  Show Profile  Reply with Quote
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.09 seconds. Powered By: Snitz Forums 2000