我目前正在构建一个小型系统,可以为我们的用户提供为我们的合作伙伴执行不同任务的奖励。为伙伴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
答案 0 :(得分: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
这显然只是一个草图,但它以面向集合的方式展示了你想要的东西。