0

使用我的 SPROC 中的这个 SQL 片段:

            -- Having the PanelPayout role gives you more transaction types to look for, hence the CASE statement.
            CASE WHEN EXISTS (SELECT RoleID FROM aspnet_UsersInRoles ur WHERE ur.RoleID = 'DEDCD456-A25A-43C5-8125-A1D1223B19EC' AND ur.UserID = s.aspnet_UserID) THEN
                (SELECT ISNULL(SUM(Amount), 0) FROM TransactionsLog payouts WHERE 
                    payouts.StaffID = s.ID 
                    AND payouts.TransactionType IN (48, 49, 3, 16, 292, 293)
                    AND (payouts.OrderDate >= dbo.Date(@date) AND payouts.OrderDate <= dbo.EndOfDay(@date))
                    AND @shift = CASE @shift WHEN 0 THEN 0 ELSE dbo.GetShiftByDate(payouts.OrderDate) END
                ) 
            ELSE
                (SELECT ISNULL(SUM(Amount), 0) FROM TransactionsLog payouts WHERE 
                    payouts.StaffID = s.ID 
                    AND payouts.TransactionType IN (48, 49, 3) 
                    AND (payouts.OrderDate >= dbo.Date(@date) AND payouts.OrderDate <= dbo.EndOfDay(@date))
                    AND @shift = CASE @shift WHEN 0 THEN 0 ELSE dbo.GetShiftByDate(payouts.OrderDate) END
                ) 
            END     

这很好用。但是,总体而言,由于 7 年前数据库最初的结构存在一些问题,因此查询没有返回我想要的内容。所以,我将使用 C# 来做我需要做的事情。我的问题是 UDF 是否可以返回可以与 IN 一起使用的整数列表,所以我只需要写

                (SELECT ISNULL(SUM(Amount), 0) FROM TransactionsLog payouts WHERE 
                    payouts.StaffID = s.ID 
                    AND payouts.TransactionType IN dbo.PayoutTransactionsValidForStaff(s.ID)
                    AND (payouts.OrderDate >= dbo.Date(@date) AND payouts.OrderDate <= dbo.EndOfDay(@date))
                    AND @shift = CASE @shift WHEN 0 THEN 0 ELSE dbo.GetShiftByDate(payouts.OrderDate) END
                ) 

哪里PayoutTransactionsValidForStaff(s.ID)会返回需要检查的整数列表?我想它可能是一个表值函数,但我没有时间测试,因为我必须发布这个更新。只是想知道是否有人知道他们的头顶。谢谢!

4

1 回答 1

1

是的,您可以在 SQL 中创建一个表值函数,它返回一个表而不是一个标量结果。您可以在 Sqlin语句中使用 this 的结果

http://msdn.microsoft.com/en-us/library/ms191165(v=sql.105).aspx

CREATE FUNCTION dbo.Test()
RETURNS @funcTable TABLE 
(
    -- Columns returned by the function
    ID int PRIMARY KEY NOT NULL    
)
AS 
-- Return some arbitrary ints
BEGIN
    INSERT @funcTable VALUES (1)
    INSERT @funcTable VALUES (2)

    RETURN 
END

GO

-- Test it - insert 1,2,3 into a table var
DECLARE @SomeTable table (ID int)
INSERT INTO @SomeTable VALUES (1)
INSERT INTO @SomeTable VALUES (2)
INSERT INTO @SomeTable VALUES (3)

-- Select from @SomeTable where the ID is in the result of the UDF
select * from @SomeTable WHERE ID IN (SELECT ID FROM dbo.Test())
于 2012-07-11T22:41:48.343 回答