Microsoft SQL Server 2005 函数,传递开始和结束时间列表
我想要将动态数量的一个开始/结束时间对作为输入参数传递给函数。然后,该函数将使用该列表,而不仅仅是选择语句中的一个开始时间和一个结束时间。
CREATE FUNCTION [dbo].[GetData]
(
@StartTime datetime,
@EndTime datetime
)
RETURNS int
AS
BEGIN
SELECT @EndTime = CASE WHEN @EndTime > CURRENT_TIMESTAMP THEN CURRENT_TIMESTAMP ELSE @EndTime END
DECLARE @TempStates TABLE
(StartTime datetime NOT NULL
, EndTime datetime NOT NULL
, StateIdentity int NOT NULL
)
INSERT INTO @TempStates
SELECT StartTime
, EndTime
, StateIdentity
FROM State
WHERE StartTime <= @EndTime AND EndTime >= @StartTime
RETURN 0
END
I'd like to do had a dynamic number of one start/end time pairs passed to a function as an input parameter. The function would then use the list instead of just one start, and one end time in a select statement.
CREATE FUNCTION [dbo].[GetData]
(
@StartTime datetime,
@EndTime datetime
)
RETURNS int
AS
BEGIN
SELECT @EndTime = CASE WHEN @EndTime > CURRENT_TIMESTAMP THEN CURRENT_TIMESTAMP ELSE @EndTime END
DECLARE @TempStates TABLE
(StartTime datetime NOT NULL
, EndTime datetime NOT NULL
, StateIdentity int NOT NULL
)
INSERT INTO @TempStates
SELECT StartTime
, EndTime
, StateIdentity
FROM State
WHERE StartTime <= @EndTime AND EndTime >= @StartTime
RETURN 0
END
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要一种方法来拆分和处理 TSQL 中的字符串,有很多方法可以做到这一点。本文涵盖了几乎每种方法的优点和缺点:
"数组和列表SQL Server 2005 及更高版本,当表值参数无法分割时”作者:Erland Sommarskog
您需要创建一个拆分函数。这是分割函数的使用方式:
我更喜欢数字表方法在 TSQL 中分割字符串,但在 SQL Server 中分割字符串的方法有很多种,请参阅前面的链接,其中解释了每种方法的优点和缺点。
要使 Numbers Table 方法发挥作用,您需要执行此一次时间表设置,这将创建一个包含 1 到 10,000 行的
Numbers
表:设置 Numbers 表后,创建此拆分函数:
测试拆分:
输出:
请注意输入字符串中缺少值:
在函数的结果集中创建了一个空字符串值,CONVERT 将其更改为 1900-01-01 00:00:00.000,您可以使用CASE 语句以不同的方式处理这些。
然后创建你的函数。这是基于问题中的代码。我不确定它的作用,因为它返回一个
int
,它始终为零,并且不对查询执行任何操作。但它来自 OPs 函数,所以它一定是他们正在做的事情的简单形式:You need a way to split and process the string in TSQL, there are many ways to do this. This article covers the PROs and CONs of just about every method:
"Arrays and Lists in SQL Server 2005 and Beyond, When Table Value Parameters Do Not Cut it" by Erland Sommarskog
You need to create a split function. This is how a split function can be used:
I prefer the number table approach to split a string in TSQL but there are numerous ways to split strings in SQL Server, see the previous link, which explains the PROs and CONs of each.
For the Numbers Table method to work, you need to do this one time table setup, which will create a table
Numbers
that contains rows from 1 to 10,000:Once the Numbers table is set up, create this split function:
test out the splitting:
OUTPUT:
note that the missing value in the input string:
created an empty string value in the function's result set, which the CONVERT changed to 1900-01-01 00:00:00.000, you could use a CASE statement to handle these in a different manner.
then create your function. This is based on the code int the question. I'm not sure what it does, because it returns an
int
, which is always zero, and doesn't do anything with the query. But it from the OPs function, so it must be a simple form of something they are doing:阅读 Erland Sommarskog 关于 SQL Server 2005 中的数组和列表的优秀(而且很长!)文章及其他 - 当 TVP 无法削减时。我打赌这不仅仅能解决您的问题:-)
Read Erland Sommarskog's excellent (and long!) article on Arrays and Lists in SQL Server 2005 and Beyond - When TVPs Do Not Cut it. I bet that'll more than cover your question(s) :-)
我可能会使用 OpenXML 来实现。如果您想了解其工作原理,我写了关于它的一篇博客文章 。
The way I would probably do it is using OpenXML. I wrote a blog article about it if you want to see an intro view on how it works.