UDF可以返回可以与“IN”关键字一起使用的列表吗?

时间:2012-07-11 22:33:54

标签: sql case user-defined-functions sql-in

使用我的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)会返回检查所需的整数列表吗?我认为它可能是一个表值函数,但我没有时间进行测试,因为我必须得到这个更新。只是想知道是否有人知道他们的头脑。谢谢!

1 个答案:

答案 0 :(得分:1)

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

中使用此结果

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())