I have been trying to get this CURSOR & WHILE LOOP working for about 2 hours now with no luck. I read a few SO threads on this and the while loop page from Microsoft, perhaps you could let me know what I am doing wrong.
I have a table which looks like this:
+-------------------------+-------------------------+
| Case_CreatedDate | Case_UpdatedDate |
+-------------------------+-------------------------+
| 2013-05-28 12:54:21.250 | 2013-07-02 08:24:02.000 |
| 2010-10-31 19:17:16.000 | 2011-02-02 14:08:04.000 |
| 2010-11-04 11:38:36.000 | 2011-01-18 12:40:15.000 |
| 2011-01-06 16:18:53.000 | 2011-01-06 16:30:25.000 |
| 2011-01-07 10:55:56.000 | 2011-01-14 09:01:40.000 |
| 2011-01-07 11:36:42.000 | 2011-01-13 11:24:03.000 |
| 2011-01-07 12:24:15.000 | 2011-01-17 16:56:41.000 |
| 2011-01-07 14:10:00.000 | 2011-02-14 09:17:55.000 |
| 2011-01-07 14:20:28.000 | 2011-01-14 10:37:20.000 |
| 2011-01-07 14:42:56.000 | 2011-01-14 14:27:41.000 |
| 2011-01-07 15:10:28.000 | 2011-01-21 11:07:50.000 |
| 2011-01-07 15:28:08.000 | 2011-01-26 11:04:27.000 |
| 2011-01-07 15:57:34.000 | 2011-01-20 15:41:43.000 |
| 2011-01-10 08:37:30.000 | 2011-01-14 09:02:43.000 |
| 2011-01-10 08:51:44.000 | 2011-01-13 15:50:26.000 |
| 2011-01-10 08:58:53.000 | 2011-01-26 11:10:54.000 |
| 2011-01-10 09:06:17.000 | 2011-01-14 09:03:33.000 |
| 2011-01-10 09:13:37.000 | 2011-01-19 15:12:07.000 |
| 2011-01-10 09:19:24.000 | 2011-01-26 11:12:37.000 |
| 2011-01-10 09:28:00.000 | 2011-03-08 16:49:59.000 |
+-------------------------+-------------------------+
I then have a function which calculates the date difference between those two dates.
I would like to use this function in conjunction with a while loop and a cursor so it will give me the results of every row when I execute it all.
At the moment, the only result I see is: "Command(s) completed successfully.
" - No actual data is displaying to me.
Here is my Current query, could you please let me know where I am going wrong?
declare @CreatedDate datetime
declare @UpdatedDate datetime
declare dates CURSOR Local for
SELECT c.Case_CreatedDate, c.Case_UpdatedDate FROM PILOT.dbo.Cases c
WHERE CONVERT(date, c.Case_CreatedDate, 103) >= CONVERT(date, GETDATE(), 103)
open dates
fetch next from dates into @CreatedDate, @UpdatedDate
while @@FETCH_STATUS = 0 BEGIN
--Execute my Function
SELECT dbo.BusinessTurnaroundTime(@CreatedDate, @UpdatedDate)
fetch next from dates into @CreatedDate, @UpdatedDate
END
close dates
deallocate dates
I am using SQL Server 2008.
Thanks so much, Mike
EDIT: also, dont worry about the dates not being bigger that getdate() the table I am showing you is only the top 20 records.