[英]Multiple inserts in a single stored procedure?
TableName: Information
将数据插入上表的存储过程。
CREATE PROCEDURE sp_insert_information
(
@profileID as int,
@profileName as varchar(8)
@profileDescription as varchar(100)
)
AS
BEGIN
INSERT INTO information(profileid, profilename, profiledescription)
VALUES (@profileID, @profileName, @profileDescription);
END
我从.NET调用此过程,如果我将profileID
作为逗号分隔的参数传递,是否可以进行多次插入? (我可以使用拆分功能吗?)
我可以循环访问profileID
并将其profileID
发送给过程,但是除了不同的profileID
之外,我的数据将是相同的。
表数据(三列):
1 profileUnavailable User Error
2 profileUnavailable User Error
3 profileUnavailable User Error
4 profileUnavailable User Error
5 profileUnavailable User Error
我还有其他方法可以尝试一次完成吗?
您有两种选择:
SqlBulkInsert-您可以创建可转储到表的数据集。 这对于许多插入很有用。 这将完全绕过该程序。
表值参数 -您可以将表值参数用作存储过程的参数,再次使用数据集来处理数据。
带有字符串拆分功能的CSV参数是一个选项,但我建议在上面使用上述参数之一。
不。 该sproc一次写入一次,因为它是当前编写的。 您必须单独调用它。
您也可以考虑将其包装到事务中,这样,如果失败,则不会全部提交。
直到几年前,我最喜欢的技术是拥有大量的拆分功能,可以将定界的均质值列表(例如,所有整数,所有布尔值,所有日期时间等)拆分为一个表变量。 这是这种功能的一个例子。
CREATE FUNCTION [dbo].[fn_SplitInt](@text varchar(8000),
@delimiter varchar(20) = '|')
RETURNS @Values TABLE
(
pos int IDENTITY PRIMARY KEY,
val INT
)
AS
BEGIN
DECLARE @index int
SET @index = -1
-- while the list is not over...
WHILE (LEN(@text) > 0)
BEGIN
-- search the next delimiter
SET @index = CHARINDEX(@delimiter , @text)
IF (@index = 0) -- if no more delimiters (hence this field is the last one)
BEGIN
IF (LEN(@text) > 0) -- and if this last field is not empty
INSERT INTO @Values VALUES (CAST (@text AS INT)) -- then insert it
ELSE -- otherwise, if this last field is empty
INSERT INTO @Values VALUES (NULL) -- then insert NULL
BREAK -- in both cases exit, since it was the last field
END
ELSE -- otherwise, if there is another delimiter
BEGIN
IF @index>1 -- and this field is not empty
INSERT INTO @Values VALUES (CAST(LEFT(@text, @index - 1) AS INT)) -- then insert it
ELSE -- otherwise, if this last field is empty
INSERT INTO @Values VALUES (NULL) -- then insert NULL
SET @text = RIGHT(@text, (LEN(@text) - @index)) -- in both cases move forward the read pointer,
-- since the list was not over
END
END
RETURN
END
当您拥有像这样的一组功能时,您的问题就会有一个像这样简单的解决方案:
CREATE PROCEDURE sp_insert_information
(
@profileID as varchar(2000),
@profileName as varchar(8)
@profileDescription as varchar(100)
)
AS
BEGIN
DECLARE @T TABLE (Id int)
INSERT INTO @T (Id)
SELECT val FROM dbo.fn_SplitInt(@profileID)
INSERT INTO information(profileid, profilename,profiledescription)
SELECT Id, @profileName, @profileDescription
FROM @T
END
但是今天,它可能更快地执行,甚至需要更少的编码,即可生成要插入数据的XML表示形式,然后将XML传递给存储过程,然后将其插入表SELECT FROM xml中,如果您明白我的意思。 。
WHILE len(@ProfileId) > 0
BEGIN
DECLARE @comm int= charindex(',',@ProfileId)
IF @comm = 0 set @comm = len(@ProfileId)+1
DECLARE @Profile varchar(1000) = substring(@ProfileId, 1, @comm-1)
INSERT INTO Information(ProfileId,ProfileName,ProfileDescription)
VALUES (@ProfileId,@ProfileName,@ProfileDescription)
SET @ProfileId= substring(@ProfileId, @comm+1, len(@ProfileId))
END
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.