Question : How to write stored procudure so it doesn't time out

Hi,
The stored procedure in the snippet below times out, if anyone can let me know how to change the syntax so it runs I would be grateful. I know cursors can be slow, but the recordset in this case is only 10 records.

Thanks,
Louise
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
ALTER PROCEDURE [dbo].[sp_sales_in_period]

(@month_id int)

AS

declare @t table(id int identity(1,1) primary key, MonthID int, EmployeeID int, Employee nvarchar(50), Sales float, DateEntered datetime)

declare @employee_id int
declare cur cursor fast_forward read_only for (select EmployeeID from vSalesEmployeeList)

declare @max int
set @max = 13

set @month_id = @month_id-3

open cur
fetch next from cur into @employee_id
while @@fetch_status = 0 

begin

	set @month_id = @month_id+1
	while @month_id <= @month_id + @max
	
	begin
	insert @t

	SELECT
	a.MonthID,
	e.EmployeeID,
	e.Employee,
	ISNULL(a.Actual, 0) AS Sales,
	a.DateEntered
	FROM dbo.tblSales AS a INNER JOIN
	dbo.vSalesEmployeeList AS e ON a.EmployeeID = e.EmployeeID
	WHERE (a.MonthID = @month_id)
	AND (a.EmployeeID = @employee_id)
	AND (
	a.DateEntered =
	(SELECT MAX(DateEntered) AS Expr1 FROM dbo.tblSales as p WHERE (p.MonthID = @month_id) AND (p.EmployeeID = @employee_id))
	)
	ORDER BY a.MonthID, a.DateEntered
	
	end

fetch next from cur into @employee_id
end

close cur
deallocate cur

begin select MonthID, EmployeeID, Employee, Sales, DateEntered from @t order by MonthID, Employee end

Answer : How to write stored procudure so it doesn't time out

Go to the Variables tab and type in the directory where your files are located and ensure that the file name is correct.  

Random Solutions  
 
programming4us programming4us