CREATE FUNCTION [dbo].[fn_InventoryPositionSet]
(
@PointInTime DATETIME,
@EOD BIT,
@AccountList Varchar(max)
)
RETURNS @OffersTable TABLE
(
Account VARCHAR(10),
PositionId VARCHAR(150),
VersionDate DATETIME,
ProductType VARCHAR(50),
XRef VARCHAR(50),
XRefType VARCHAR(20),
Desk VARCHAR(50)
)
AS
BEGIN
IF @EOD = 1
SET @PointInTime = dbo.COBTimestamp(@PointInTime)
IF @AccountList is not NULL
INSERT INTO @OffersTable
SELECT
P.Account,
P.UniquePositionId AS [PositionId],
P.Desk,
P.VersionEffective AS [VersionDate],
P.ProductType,
P.XRef,
P.XRefType
FROM MarkingInventory AS P WITH(nolock)
LEFT JOIN
Spirit.dbo.GetTableOfInputs(@AccountList) A
ON
(
(P.Account = A.Input)
AND
@AccountList IS NOT NULL
)
WHERE
@PointInTime >= P.VersionEffective AND @PointInTime < P.VersionExpiration and P.Account = A.Input
ELSE
INSERT INTO @OffersTable
SELECT
P.Account,
P.UniquePositionId AS [PositionId],
P.Desk,
P.VersionEffective AS [VersionDate],
P.ProductType,
P.XRef,
P.XRefType
FROM MarkingInventory AS P WITH(nolock)
WHERE
@PointInTime >= P.VersionEffective AND @PointInTime < P.VersionExpiration
RETURN
END
我创建了这个函数,它接受三个参数。我有几个关于执行此功能的问题。
从字符串转换日期和/或时间时转换失败
这是我尝试以下列方式执行此功能时收到的错误
select * from [dbo].[fn_InventoryPositionSet] (GETDATE(), 1, null)
即使我通过2012-03-10
而不是getdate()
我也想学习如何以列表的形式传递参数。作为参数之一是列表的形式。如果您想了解有关该功能的更多信息,请告诉我。
这是在上面编写的函数中使用的函数:
CREATE FUNCTION [dbo].[COBTimestamp]
(
@COBDate DATETIME
)
RETURNS DATETIME
AS
BEGIN
RETURN DATEADD(day, DATEDIFF(day, 0, @COBDate), '23:59:59')
END
GO