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