我一直收到以下错误:
Msg 547,Level 16,State 0,Procedure add_new_customer,Line 6
INSERT语句与CHECK约束" CK__customer__addres__09A971A2"冲突。冲突发生在数据库" AIT 732 - 银行",表" dbo.customer",列' address_state'。
以下是我试图更新的表格:
create table customer
(
cust_id numeric(10,0) not null identity primary key,
social_security_num numeric(9,0) not null,
first_name varchar(20) not null,
last_name varchar(20) not null,
primary_address varchar(50) not null,
address_zip_code numeric(5,0) not null,
address_state char(2) check (address_state in('md','pa','dc', 'de', 'wv', 'va','nj')),
gender char(1) not null check (gender in('m','f')),
phone_num numeric(10,0) null
)
这是我创建的存储过程:
create procedure add_new_customer
(@social_sec numeric, @f_name varchar, @l_name varchar, @pri_address varchar, @zip numeric, @add_state char, @gender char, @phone numeric)
as
begin
begin transaction
insert into customer(
social_security_num,
first_name,
last_name,
primary_address,
address_zip_code,
address_state,
gender,
phone_num)
values (
@social_sec,
lower(@f_name),
lower(@l_name),
lower(@pri_address),
@zip,
lower(@add_state),
lower(@gender),
@phone)
if @@ERROR != 0
begin
rollback transaction
return @@error
end
commit transaction
end
最后但并非最不重要的是,这就是我调用过程的方式
add_new_customer 211118888, 'Bob', 'JONES', '222 some st', 21333, 'md', 'm', 4102227878
我一直在弄乱这个问题大约一小时,并且无法弄清楚错误的来源。任何帮助将不胜感激。
答案 0 :(得分:3)
您的存储过程参数定义为@add_state char
,但这会将您输入的“md”值截断为“m”,这不是有效值。更改参数定义以匹配表defn char(2)
,然后它应该有效。