我已经使用选择表创建了一个插入,我想要它做的是为0-99之间的每个玩家提供一个随机的“玩家重量”。但是我遇到的问题是,不是为每一行随机化一个玩家加权,而是为玩家所在的每个团队做这件事。例如:
PlayerID FirstName Surname PlayerWeighting TeamID
1 Chelsea Player1 87 1
2 Chelsea Player2 87 1
3 Chelsea Player3 87 1
4 Liverpool Player1 73 2
5 Liverpool Player2 73 2
我反而希望它做这样的事情:
PlayerID FirstName Surname PlayerWeighting TeamID
1 Chelsea Player1 87 1
2 Chelsea Player2 73 1
3 Chelsea Player3 62 1
4 Liverpool Player1 90 2
5 Liverpool Player2 51 2
在我的查询中需要更改什么才能使用(NumberID是每个字符串末尾的数字'播放器'?
CREATE PROCEDURE [dbo].[Player_CreateWholeTeam]
@TeamID INT
AS
SET NOCOUNT ON
BEGIN
INSERT INTO dbo.Player (FirstName, Surname, PlayerWeighting, TeamID)
SELECT TeamName AS FirstName, 'Player' + cast(NumberID AS VARCHAR (10)) AS Surname, CONVERT(INT, (RAND() * 99)) AS PlayerWeighting, @TeamID
FROM dbo.Team t
CROSS JOIN dbo.Number n
WHERE TeamID = @TeamID
AND n.NumberID <= 20
END
答案 0 :(得分:2)
这将为您提供0到99之间的随机整数
abs(convert (bigint, convert(varbinary(20), newid()))) % 100
或
abs(checksum(newid())) % 100
他们的关键是使用NEWID(),它会为每一行生成一个唯一的值。兰德()不