Cannot insert the value NULL into column (MS-SQL) with Store Procedure

Hung PD

Here is my Table, called Tour_Lists. If my table is empty. Store Procedure work very well. But after insert first row. I continue inserting second row and get error:

Cannot insert the value NULL into column 'Ma_tour', table 'Travel.dbo.Tour_Lists'; column does not allow nulls. INSERT fails. The statement has been terminated.

What's wrong in my code?

enter image description here

Store Procedure

/****** 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
Adrian Wragg

The problem here is that once a row has been input, then:

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

gives @Matourcuoi a value. This then matches your condition:

     if(@Matourcuoi is not null)

which goes on to calculates the value of @return based on the value of @tangsotour. This is null, so the following part of your condition is evaluated (as no other parts are matched):

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

Adding a varchar to a NULL produces a NULL, so by the time it reaches:

    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 is still NULL, hence your error.

As I'm unfamiliar with the language used in the sample code, I can't tell what the variables are meant to be, so can't suggest an easy work-around. I'm guessing, though that @tangsotour is meant to be the numeric part of @Matourcui plus 1 - if so, that step has been missed.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Cannot insert 'NULL' value into SQL Server column

SQL: Cannot insert the value NULL into column 'ExampleID'

SQL insert error: Cannot insert the value NULL into column 'Id'

How to insert a NULL value in PL/SQL Procedure?

Cannot insert NULL into column while value is not null

Column cannot be null - procedure

EFCore Cannot insert the value NULL into column

cannot insert null value in id column

SqlBulkCopy Cannot insert the value NULL into column

"Cannot insert the value NULL into column" despite value not being NULL

How to fix: Cannot insert the value NULL into column, insert failed

Cannot insert the value NULL into column 'ID' even the column is Identity

MS SQL Store Procedure to Merge Multiple Rows into Single Row based on Variable Table and Column Names

Web API error: "Cannot insert the value NULL into column 'Id'

Entity Framework Non Identity - Cannot insert the value NULL into column 'ID'

Cannot insert the value NULL into column 'EventDateTime', table using datatable

ASP.NET Cannot insert the value NULL into column ‘name’

Cannot insert or update varchar empty/null value into always encrypted column

Stored procedure cannot insert into column name Timestamp

Ms Sql Stored Procedure Using Not Null Syntax

Cannot INSERT NULL into Column on Remove

Cannot insert NULL into column 'UserId'

Cannot insert NULL into column 'Type'

Cannot insert the value NULL into column 'Discriminator', table '...AspNetUsers'; column does not allow nulls. INSERT fails

Cannot insert the value NULL into column X, column X does not allow nulls. INSERT fails.

Cannot insert the value NULL into column 'DeletedDatabaseRecord',...; column does not allow nulls. INSERT fails

SqlException: Cannot insert the value NULL into column 'x'; column does not allow nulls. INSERT fails

how to insert null value into datetime column in sql with java

Insert a value at a NOT NULL column int in SQL Laravel Eloquent

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  3. 3

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  4. 4

    pump.io port in URL

  5. 5

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    Do Idle Snowflake Connections Use Cloud Services Credits?

  9. 9

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

  10. 10

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  11. 11

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  12. 12

    Generate random UUIDv4 with Elm

  13. 13

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  14. 14

    Is it possible to Redo commits removed by GitHub Desktop's Undo on a Mac?

  15. 15

    flutter: dropdown item programmatically unselect problem

  16. 16

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  17. 17

    EXCEL: Find sum of values in one column with criteria from other column

  18. 18

    Pandas - check if dataframe has negative value in any column

  19. 19

    How to use merge windows unallocated space into Ubuntu using GParted?

  20. 20

    Make a B+ Tree concurrent thread safe

  21. 21

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

HotTag

Archive