我有:
我想存储一个程序,该程序接受来自用户的号码作为@scale,这是每个分销商拥有的区域数量。程序应该去检查哪些经销商已达到@scale号码并为他们的工资增加10美元!
我有这个代码,但感觉很奇怪/愚蠢/不工作!我认为我看待这个问题的方式是错误的。
create procedure RewardDistributors (@scale int = null)
as
if @scale is null
begin
raiserror ('Please provide A value!', 16, 1)
end
else
begin
if not exists
(select COUNT(RegionID) from Region having COUNT(RegionID) >= @scale)
begin
raiserror ('No one reached this number yet', 16, 1)
end
else
begin
update Distributor
set wage = (wage + 10)
where DistributorID =
(select DistributorID, COUNT(*) from Region group by DistributorID having COUNT(*) >= @scale)
end
end
返回
答案 0 :(得分:0)
简化你的过程:
CREATE PROCEDURE RewardDistributors
@Scale INT
AS
UPDATE Distributor
SET wage = wage + 10
WHERE DistributorID IN (
SELECT DistributorID
FROM Region
GROUP BY DistributorID
HAVING COUNT(*) >= @Scale
)
PRINT CAST(@@ROWCOUNT AS VARCHAR) + ' rows updated.'
如果您要检查其是否为NULL,则不允许该参数为NULL。您也不需要执行任何测试 - 只需执行更新,然后打印更新的行数。如果您真的需要不同的输出消息,可以修改它。