【发布时间】:2010-07-17 08:29:32
【问题描述】:
我用的是SQL Server 2008 express,下面是SP,谁返回
(0 行受影响)
消息 515,级别 16,状态 2,程序 sp_AddCarrierFees,
第 21 行不能 将值 NULL 插入列 'attribute_value_id',表 'MyDevSystem.dbo.shipping_fees';
列 不允许空值。插入失败。 声明已终止。
(受影响的 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 carrier_id=@carrier_id and attribute_title=@attribute_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 attribute_value=@fees where attribute_value_id=@attribute_value_id;
end
END
有人知道为什么吗?我在 StackOverFlow 上看了很多帖子,但仍然没有找到解决方案。有人说改用@@IDENTITY 或 IDENT_CURRENT,但它可能得到了其他用户制作的身份。
已修复:我找到了原因,因为第一个插入语句失败,所以为什么返回(0 行受影响),在我修复该插入语句之后,它现在可以工作了。谢谢大家。
【问题讨论】:
-
你能发布
shipping_attribute_value表的定义吗? -
是的,创建表 [dbo].[shipping_attribute_value]( [attribute_value_id] [int] IDENTITY(1,1) NOT NULL, [attribute_id] [int] NOT NULL, [attribute_value] [varchar] (255) 非空,[sort_order] [int] 非空,约束 [PK_shipping_attribute_value] 主键集群([attribute_value_id] ASC)与(PAD_INDEX = OFF,STATISTICS_NORECOMPUTE = OFF,IGNORE_DUP_KEY = OFF,ALLOW_ROW_LOCKS = ON,ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY]
-
attribute_value_id 是primary_key,自增1
标签: sql sql-server-2008