2014-04-14 3 views
0

TableName : Information단일 저장 프로 시저에 여러 삽입이 있습니까?

위의 표에 데이터를 삽입하는 저장 프로 시저입니다. 나는 .NET에서이 프로 시저를 호출

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 

, 내가 쉼표로 구분 된 매개 변수로 profileID의를 통과하면 여러 삽입 할 수있는 방법은 무엇입니까? (분할 기능을 사용할 수 있습니까?)

profileID을 통해 반복하고 1을 1로 처리 할 수 ​​있지만, 다른 데이터는 profileID을 제외하고는 동일합니다. (3 열)

테이블 데이터 : 나는 하나의 샷이 일을 시도 할 수 있습니다

1 profileUnavailable User Error 
2 profileUnavailable User Error 
3 profileUnavailable User Error 
4 profileUnavailable User Error 
5 profileUnavailable User Error 

다른 방법?

+0

proc을 수정하려고합니까? – Habib

+0

proc 또는 .net 코드를 변경할 수 있습니다. – Sharpeye500

+1

사이드 노트 : 스토어드 프로 시저에'sp_' 접두사를 사용하지 말아야합니다 **. Microsoft는 [자체 저장을 위해이 접두어를 예약했습니다 (* 저장 프로 시저 명명 * 참조)] (http://msdn.microsoft.com/en-us/library/ms190669%28v=sql.105%29.aspx) 및 당신은 미래에 언젠가 이름 충돌의 위험을 감수해야합니다. [저장 프로 시저 성능에 좋지 않습니다.] (http://www.sqlperformance.com/2012/10/t-sql-queries/sp_prefix). 'sp_'를 피하고 다른 것을 접두어로 사용하는 것이 가장 좋습니다. –

답변

0

아니요. 그 sproc은 현재 쓰여진대로 한 번에 하나씩 삽입합니다. 별도로 호출해야합니다.

또한 트랜잭션으로 래핑하는 것을 고려해 볼 수 있습니다. 하나가 실패하면 모든 트랜잭션이 커밋되지 않습니다.

+0

@downvoter - 왜 downvote? –

1

당신은 몇 가지 옵션이 있습니다 :

SqlBulkInsert을 - 당신은 테이블에 덤프 할 수있는 데이터 집합을 만들 수 있습니다. 이것은 많은 삽입물에 유용합니다. 이렇게하면 절차가 생략됩니다.

Table Valued Parameters - 테이블 값 매개 변수를 저장 프로 시저의 매개 변수로 사용하여 데이터 집합을 사용하여 데이터를 다시 조작 할 수 있습니다.

문자열 분할을 사용하는 CSV 매개 변수는 옵션이지만 위의 코드 중 하나를 권장합니다.

+0

하지만 C# 코드 (예 : OP가 언급 한 것)를 거친 경우 행 단위로만 삽입됩니다. – Rahul

+0

@Rahul 정교하게 신경 써야할까요? 내 해결책 중 어느 것도 행마다 없습니다. – Khan

0

내가 좋아하는 기술은 몇 년 전까지는 동질적인 값 (예 : 모든 정수, 모든 부울 값, 모든 날짜 시간 등)의 구분 된 목록을 테이블 변수로 나눌 수있는 분할 기능이 있어야했습니다. 다음은 그러한 함수의 예입니다. 당신이 이런 함수의 집합이있을 때

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을 저장 프로 시저에 전달하고 XML 의미를 알면 INSERT INTO 테이블에서 XML을 선택하십시오.

0
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 
관련 문제