0

我目前正在构建一个小型系统,该系统可以为我们的用户为我们的合作伙伴执行不同的任务提供奖励。为合作伙伴 Y 做 X 将奖励用户 Z 金额。与其一直转移大量的小账户变动,我们希望为所有用户一天的活动提供综合奖金。

我想构建一个可以在午夜左右由 SQL Server 代理运行的存储过程。此存储过程将汇总用户的收入并将其添加到他们的帐户中。但是,我当时只能设法让这个工作的 1 个用户。是否可以通过 1 次调用存储过程来管理 100 个用户的收入?

分解它,我想要一个存储过程,它可以在 1 个调用中处理 100 个用户的总事务,而不是调用存储过程 100 次,一次是 1 个用户。那可能吗?

提前致谢!

编辑:

这是我一直在处理的一些示例代码:

ALTER PROCEDURE [dbo].[rewards_dailybonus]
AS
BEGIN
SET NOCOUNT ON;

DECLARE @userid INT = 0
DECLARE @partnerId INT = 0
DECLARE @totalPoints INT = 0
DECLARE @theDate DATETIME = ''


    -- This SELECT should return 100 rows
    SELECT @userid = userid, @partnerId = partnerId, @totalPoints = SUM(points), @theDate = CAST(created AS DATE)
    FROM rewards_movements WITH (NOLOCK)
    WHERE
        transferred IS NULL
        AND
        CAST(created AS DATE) != CAST(GETDATE() AS DATE)
    GROUP BY userid, partnerId, CAST(created AS DATE)
    ORDER BY CAST(created AS DATE), userid ASC

    -- For each row from above SELECT, execute another Stored Procedure to add the points to a users account
    EXEC registerAccountMovement @userid, @totalPoints, 'Account movement notice'

    -- Set the points as transferred
    UPDATE rewards_movements SET transferred = GETDATE() WHERE userid = @userid AND partnerId = @partnerId
END
4

1 回答 1

1

好吧,我认为代码看起来像这样:

with toupdate as (
      select userid, sum(earnings) as NewEarnings
      from tblEarnings e
      where e.earningdate = cast(getdate() -1 as date)
      group by userid
     )
update users
    set earnings = earnings + newearnings
    from toupdate
    where users.userid = toupdate.userid

这显然只是一个草图,但它显示了如何以面向集合的方式做你想做的事情。

于 2013-02-06T14:47:33.580 回答