Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 循环中的SQL函数问题_Sql Server_Function_While Loop - Fatal编程技术网

Sql server 循环中的SQL函数问题

Sql server 循环中的SQL函数问题,sql-server,function,while-loop,Sql Server,Function,While Loop,我一直在尝试让这个光标&在循环中工作了大约2个小时,但没有成功。我读了一些微软的文章,也许你可以告诉我我做错了什么 我有一张这样的桌子: +-------------------------+-------------------------+ | Case_CreatedDate | Case_UpdatedDate | +-------------------------+-------------------------+ | 2013-05-28 12:54:

我一直在尝试让这个
光标
&
在循环中
工作了大约2个小时,但没有成功。我读了一些微软的文章,也许你可以告诉我我做错了什么

我有一张这样的桌子:

+-------------------------+-------------------------+
|    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 |
+-------------------------+-------------------------+
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
然后我有一个
函数
,它计算这两个日期之间的
日期差

我想将这个函数与while循环和游标结合使用,这样当我执行它时,它会给出每一行的结果

目前,我看到的唯一结果是:
“命令成功完成。
“
-没有实际数据显示给我

这是我目前的问题,你能告诉我哪里出了问题吗

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
我正在使用
SQLServer2008

非常感谢, 迈克


编辑:另外,不要担心日期不会比getdate()大。我向您展示的表只是前20条记录。

我问您为什么要使用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)

谢谢。

Nesuke为您的问题提供了一个更好的解决方案,以防您将来需要使用游标/while,下面是您的查询的错误之处。您没有指定要对结果执行的操作。您可以将循环的每个迭代插入临时表,如下所示:

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

狗娘养的!我只是在谷歌上搜索如何实现我想要的东西,那些
光标和
而循环是第一个弹出的东西。谢谢你,我要给它一个狂欢。见鬼去吧。。。这么简单的解决方案,我整个上午都在把它复杂化!非常感谢Nesuke。很高兴为您服务。非常感谢您仍然为我找到答案+1:)