sql server中批量插入与更新两种解决方案分享(存储过程)

网络整理 - 08-22


DECLARE @Data NVARCHAR(max)
SET @Data='tanw,keenboy' --Id,Name

DECLARE @Temp TABLE
(
Id INT IDENTITY(1,1),
Name NVARCHAR(50)
)
DECLARE @Id INT
DECLARE @Name NVARCHAR(50)
DECLARE @Results NVARCHAR(MAX) SET @Results=''
INSERT INTO @Temp SELECT (SELECT * FROM split(@Data,';'))

WHILE EXISTS(SELECT * FROM @Temp)
BEGIN
SELECT TOP 1 @Id=Id,@Name=Name from @Temp
DELETE FROM @Temp where [id] = @Id
SET @Results=@Results+@Name+','

/*
在这里做逻辑处理,插入或更新操作 ...
*/
END
SELECT @Results