这是我添加多个播放器的存储过程:
CREATE PROCEDURE [dbo].[AddMultipleUserofSameCountry]
@UserIds NVARCHAR(MAX),
@CountryCode INT,
@OwnsHasAccess INT,
@QryResult INT OUTPUT
AS
BEGIN TRY
SET @QryResult = 0;
BEGIN TRANSACTION
BEGIN
INSERT INTO Players (CountryCode, UserId, OwnsHasAccess, InsertedWhen)
VALUES (@CountryCode,
(SELECT Value FROM dbo.SplitString(@UserIds, ',')),
@OwnsHasAccess, GETDATE());
END
COMMIT TRANSACTION
SET @QryResult = 1;
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION
SET @QryResult = 0;
END CATCH
UserIds是逗号分隔的字符串值
DAL代码
array = string.Join(",", UserIds.Select(n => n.ToString()).ToArray());
SqlParameter sqlParam = cmdProc.Parameters.AddWithValue("@UserIds", array);
sqlParam.SqlDbType = SqlDbType.VarChar;
请建议有什么方法可以传递多个UserId插入多个玩家
答案 0 :(得分:1)
您应该构建完整的行以在INSERT
语句中使用:
CREATE PROCEDURE [dbo].[AddMultipleUserofSameCountry]
@UserIds nvarchar(MAX),
@CountryCode int,
@OwnsHasAccess int,
@QryResult int output
AS
Begin Try
Set @QryResult = 0;
Begin Transaction
Begin
INSERT INTO Players(CountryCode,UserId,OwnsHasAccess,InsertedWhen)
SELECT @CountryCode, Value, @OwnsHasAccess, GETDATE()
FROM dbo.SplitString(@UserIds,',');
END
Commit Transaction
Set @QryResult = 1;
End Try
Begin Catch
Rollback Transaction
Set @QryResult = 0;
End Catch
注意:我假设dbo.SplitString
与SQL Server 2016中的STRING_SPLIT
相同