无法使用存储过程将值NULL插入到列(MS-SQL)中

时间:2013-07-05 14:31:33

标签: c# asp.net sql linq stored-procedures

这是我的表,名为Tour_Lists。如果我的桌子是空的。存储过程非常有效。但插入第一行后。我继续插入第二行并得到错误:

无法将值NULL插入列'Ma_tour',表'Travel.dbo.Tour_Lists';列不允许空值。 INSERT失败。声明已经终止。

我的代码出了什么问题?

enter image description here

存储过程

/****** Object:  StoredProcedure [dbo].[ThemTour]    Script Date: 07/05/2013 21:16:14 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[ThemTour]
@ten_tour nvarchar(100),
@lich_trinh nvarchar(MAX),
@gia decimal(18,0),
@thoi_gian nvarchar(50),
@khoi_hanh nvarchar(50),
@noi_khoi_hanh nvarchar(100),
@phuong_tien nvarchar(50),
@khach_san nvarchar(30),
@diem_den nvarchar(100),
@anh_dai_dien image,
@trang_thai bit = 1
AS
BEGIN
declare @return nvarchar(100),
@Matourcuoi nvarchar(100),
@sothutu nvarchar(50),
@dodaichuoi int, 
@tangsotour int
select top(1) @Matourcuoi = Ma_tour from Tour_Lists order by Ma_tour desc -- lấy cái MaTour cuối cùng trong bảng
if(@Matourcuoi is not null)
begin
-- Analyzing...
select @dodaichuoi = len(@Matourcuoi) -- cho biết độ dài của chuổi
select @sothutu = substring(@Matourcuoi,3,@dodaichuoi - 2) -- Trả về con số trong chuối, bắt đầu lấy từ ký tự thứ 3 cho đên hêt chuổi (trừ 2 chữ 'NV' ra)
select @tangsotour = convert(int,@tangsotour) + 1 -- cho nó tăng thêm 1 đơn vị, chuẩn bị chèn vô

-- cấu trúc switch-case-default bên SQL là thế này
select @return = case
when len(convert(nvarchar,@tangsotour))=1 then 'Tour000'+convert(nvarchar,@tangsotour) --nếu là số có 1 chữ số
when len(convert(nvarchar,@tangsotour))=2 then 'Tour00'+convert(nvarchar,@tangsotour) -- nếu là 2 chữ số
when len(convert(nvarchar,@tangsotour))=3 then 'Tour0'+convert(nvarchar,@tangsotour) -- nếu là 3 chữ số
when len(convert(nvarchar,@tangsotour))=4 then 'Tour'+convert(nvarchar,@tangsotour) --nếu là 4 chữ số
else 'Tour'+convert(nvarchar,@tangsotour) -- nếu trên 4 chữ số: từ 10,000 trở đi
end -- end of switch-case-default

end -- endif
else select @return='Tour0001' -- nếu chưa có mẩu tin nào trong bảng Tour_Lists
insert into Tour_Lists (Ma_tour,Ten_tour,Lich_trinh,Gia,Thoi_gian,Khoi_hanh,Noi_khoi_hanh,Phuong_tien,Khach_san,Diem_den,Anh_dai_dien,Trang_thai)
VALUES (@return,@ten_tour,@lich_trinh,@gia,@thoi_gian,@khoi_hanh,@noi_khoi_hanh,@phuong_tien,@khach_san,@diem_den,@anh_dai_dien,@trang_thai)
END

2 个答案:

答案 0 :(得分:2)

这里的问题是,一旦输入了一行,那么:

    select top(1) @Matourcuoi = Ma_tour from Tour_Lists order by Ma_tour desc

@Matourcuoi一个值。这符合您的条件:

     if(@Matourcuoi is not null)

继续根据@return的值计算@tangsotour的值。这是null,因此评估条件的以下部分(因为没有其他部分匹配):

     else 'Tour'+convert(nvarchar,@tangsotour)

将varchar添加到NULL会产生NULL,因此到达时:

    insert into Tour_Lists (Ma_tour,Ten_tour,Lich_trinh,Gia,Thoi_gian,Khoi_hanh,Noi_khoi_hanh,Phuong_tien,Khach_san,Diem_den,Anh_dai_dien,Trang_thai)
    VALUES (@return,@ten_tour,@lich_trinh,@gia,@thoi_gian,@khoi_hanh,@noi_khoi_hanh,@phuong_tien,@khach_san,@diem_den,@anh_dai_dien,@trang_thai)

@return仍为NULL,因此您的错误。

由于我不熟悉示例代码中使用的语言,我无法分辨出变量的含义,因此无法建议简单的解决方法。我猜,虽然@tangsotour@Matourcui加上1的数字部分 - 如果是这样的话,那个步骤就错过了。

答案 1 :(得分:1)

我猜你从未在桌面上设置身份种子?

相关问题