2010-07-17 9 views
2
내가 SQL 서버 2008 익스프레스를 사용하고

는 아래SCOPE_IDENTITY()가 NULL을 반환합니까?

(영향을받는 0 행 (들))

메시지 515, 수준 16, 상태 2, 프로 시저 sp_AddCarrierFees,

을 반환 SP입니다

라인 21 열에 NULL 값을 입력하십시오. 'attribute_value_id', 테이블 'MyDevSystem.dbo.shipping_fees';

열 은 null을 허용하지 않습니다. INSERT가 실패합니다. 명세서가 종료되었습니다.

(1 개 행 적용됨)

그리고 이것은 SP입니다 :

GO 

SET ANSI_NULLS ON 
GO 
SET QUOTED_IDENTIFIER ON 
GO 

ALTER PROCEDURE [dbo].[sp_AddCarrierFees] 
    @carrier_id INT, 
    @zone_id INT, 
    @attribute_value_id INT, 
    @attribute_title varchar(20), 
    @fees decimal(6,2) 
AS 
BEGIN 
    if @attribute_value_id = 0 begin 
     insert into shipping_attribute_value (attribute_id,attribute_value,sort_order) 
     select attribute_id, @fees,0 from shipping_attribute where [email protected]_id and [email protected]_title; 

     declare @NewID int; 
     set @NewID = SCOPE_IDENTITY(); 
     print @NewID; 

     insert into shipping_fees (zone_id, attribute_value_id) values (@zone_id, @NewID); 
    end 
    else 
    begin 
     update shipping_attribute_value set [email protected] where [email protected]_value_id; 
    end 
END 

모든 사람들이 이유를 알고? StackOverFlow에서 많은 게시물을 읽었지만 여전히 해결책을 찾지 못했습니다. 누군가는 @@ IDENTITY 또는 IDENT_CURRENT를 대신 사용하지만 다른 사용자가 만든 ID를 가질 수 있습니다.

고정 : 첫 번째 삽입 문이 실패했기 때문에 이유를 찾았습니다. 그래서 return 문이 영향을받은 이유는 무엇입니까? 삽입 문을 수정 한 후 이제는 작동합니다. 모두에게 감사드립니다.

+1

'shipping_attribute_value' 테이블의 정의를 게시 할 수 있습니까? – Andomar

+0

예, 가 [DBO] 테이블을 만듭니다. [shipping_attribute_value] ( \t [attribute_value_id] [INT] IDENTITY (1,1), NULL NOT \t [은 attribute_id] [INT] NULL NOT, \t [ATTRIBUTE_VALUE] [VARCHAR] (255) NULL NOT, \t [SORT_ORDER [INT] NULL NOT, CONSTRAINT [PK_shipping_attribute_value] PRIMARY KEY 클러스터링 ( \t [attribute_value_id] ASC ) = OFF = OFF (PAD_INDEX, STATISTICS_NORECOMPUTE, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS WITH = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] – Cheung

+0

attribute_value_id는 primary_key입니다. nd 자동 증가 1 – Cheung

답변

1

먼저 shipping_attribute_value에 실제로 identity 열이 있는지 확인하십시오. 식별 열이 없으면 scope_identity()이 작동하지 않습니다.

편집 : 나는 insert 실제로 select를 사용하고 있음을 놓친하지만 marc_s 그것을 발견 :) * 잡고 커피 * 별도의 일에 대해 출력보고 바로 선택 방법

:

select attribute_id, @fees,0 from shipping_attribute 
     where [email protected]_id and [email protected]_title; 

insert into shipping_attribute_value (attribute_id,attribute_value,sort_order) 
    select attribute_id, @fees,0 from shipping_attribute 
    where [email protected]_id and [email protected]_title; 

을 행이 삽입되지 않으면 scope_identity()이 null이어야합니다.

+0

예, 이유를 찾았습니다. – Cheung

관련 문제