2
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
4

2 回答 2

0

查看您的表定义:

1. Account            VARCHAR(10),
2. PositionId         VARCHAR(150),
*** 3. VersionDate        DATETIME, ***
4. ProductType        VARCHAR(50),
5. XRef               VARCHAR(50),
6. XRefType           VARCHAR(20),
7. Desk               VARCHAR(50)

你的选择:

            SELECT      
                1. P.Account,
                2. P.UniquePositionId AS [PositionId],
                *** 3. P.Desk, ***
                4. P.VersionEffective AS [VersionDate],
                5. P.ProductType,
                6. P.XRef,
                7. P.XRefType
于 2013-03-12T14:34:22.010 回答
0

不在其中的问题[dbo].[fn_InventoryPositionSet][dbo].[COBTimestamp].

RETURN DATEADD(day, DATEDIFF(day, 0, @COBDate), '23:59:59')
                                                    ^__________ Must be converted to `DATETIME`

日期添加 (Transact-SQL)

于 2013-03-10T19:55:34.987 回答