簡體   English   中英

在單個存儲過程中有多個插入?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM