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.
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 = 0BEGINSELECT days AS '@num1' FROM [MasterConsole].[dbo].[sla] WHERE [sla].Customer = @CusIDSELECT 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 = 0BEGINSELECT @num1 = days AS '@num1' FROM [MasterConsole].[dbo].[sla] WHERE [sla].Customer = @CusIDSELECT @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.CheersMIK |
|
|
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 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-05-08 : 13:02:18
|
something likeUPDATE mm.SLA = c2.MinDateFROM [MasterConsole].[dbo].[main] mINNER JOIN [MasterConsole].[dbo].[sla] sON s.Customer = m.CustomerINNER JOIN [MasterConsole].[dbo].Calender c1ON c1.DATE = m.[Date Received]INNER JOIN (SELECT [Order],[COUNTED], Min([Date]) AS MinDate FROM [MasterConsole].[dbo].Calender GROUP BY [Order],[COUNTED] )c2ON c2.[Order] = m.[Order]AND c2.[COUNTED] = s.Days + c1.COUNTEDWHERE m.SLA IS NULL ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
vincent kelly
Starting Member
3 Posts |
Posted - 2013-05-10 : 10:52:22
|
with a little modification worked just fine, many thanks visakh16nelliegoat |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-05-13 : 02:22:42
|
welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
|
|
|
|
|