2014-07-01 58 views
0

我一直在試圖獲得這個CURSOR & WHILE LOOP現在工作了大約2個小時,沒有運氣。我閱讀了一些關於這個的SO線程和微軟的while loop頁面,也許你可以讓我知道我做錯了什麼。循環問題中的SQL函數

我有一個表,看起來像這樣:

+-------------------------+-------------------------+ 
| 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 | 
+-------------------------+-------------------------+ 

然後我有一個function它計算這兩個日期之間的date difference

我想將此函數與while循環和遊標一起使用,這樣當我執行所有操作時,它會給出每行的結果。

目前,我看到的唯一結果是:"Command(s) completed successfully. " - 沒有實際的數據顯示給我。

這是我當前的查詢,請問我能告訴我哪裏出錯了嗎?

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 

我正在使用SQL Server 2008

非常感謝, 邁克

編輯:另外,不要擔心不是更大的是GETDATE()我顯示你的表是隻有前20個記錄的日期。

回答

1

爲什麼你用這個while循環我問我?用簡單的查詢可以達到相同的結果:

SELECT dbo.BusinessTurnaroundTime(c.Case_CreatedDate, c.Case_UpdatedDate) 
FROM PILOT.dbo.Cases c 
WHERE CONVERT(date, c.Case_CreatedDate, 103) >= CONVERT(date, GETDATE(), 103) 

謝謝。

+0

槍的兒子!我只是谷歌如何實現我以後的事情,那些'遊標'和'while循環'是第一件事情彈出。謝謝你,我會給它一個bash。 – Mike

+0

該死的一切都是地獄......這樣一個簡單的解決方案,我一直在複雜化整個早上!非常感謝這個Nesuke。 – Mike

+0

很高興爲您服務。 –

1

Nesuke爲您的問題提供了一個更好的解決方案,以防萬一您需要在未來再次使用遊標時,以下是您的查詢出了什麼問題。你不指定你想要的結果。您可以將循環的每次迭代插入臨時表中,如下所示:

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) 
create table #BusinessTurnaroundTime (
    BusinessTurnaroundTime int null) 

open dates 

fetch next from dates into @CreatedDate, @UpdatedDate 

while @@FETCH_STATUS = 0 BEGIN 

    --Execute my Function 
    insert #BusinessTurnaroundTime 
     dbo.BusinessTurnaroundTime(@CreatedDate, @UpdatedDate) 


    fetch next from dates into @CreatedDate, @UpdatedDate 
END 

close dates 
deallocate dates 

SELECT * FROM #BusinessTurnaroundTime 
+0

非常感謝您對我的評價+1 :) – Mike