简体   繁体   中英

Why isn't my Auto Increment working in SQL Server 2008?

This is my table creation:

create table Movie 
(
     MovieID int NOT NULL IDENTITY (1,1) PRIMARY KEY, 
     MovieName varchar(40) NOT NULL UNIQUE, 
     CurrentStock int NOT NULL, 
     GenreID int NOT NULL,
     RatingID int NOT NULL, 
     Max_Inventory int NOT NULL, 
     Platforms char(5) NOT NULL, 
     Discontinued bit, 
     DiscontinuedDate Date
); 

create table Inventory
(
     InventoryID int NOT NULL IDENTITY (1,1) PRIMARY KEY, 
     MovieID int Not NULL,
     CurrentStock int NOT NULL, 
     Max_Inventory int NOT NULL
);

Stored procedures:

ALTER PROCEDURE [dbo].[InsInventory]
(@CurrentStock int,
 @ChangeStock int)
as
begin
  insert into Inventory(Max_Inventory, CurrentStock)
  Values (@ChangeStock, @CurrentStock)

  select SCOPE_IDENTITY();
END

ALTER PROCEDURE [dbo].[InsMovie] 
(@GenreID int,
 @RatingID int,
 @Platform varchar(5),
 @MovieName varchar(40)
)
AS
BEGIN
   Insert into Movie (RatingID, MovieName, Platforms, GenreID)
   Values (@RatingID, @MovieName, @Platform, @GenreID)

   select SCOPE_IDENTITY();

   SET NOCOUNT ON;
END

Foreign key:

ALTER Table Inventory
ADD CONSTRAINT fk_Inventory_Movie 
FOREIGN KEY (MovieID) REFERENCES Movie(MovieID)

ALTER TABLE Movie
ADD CONSTRAINT fk_Movie_RatingLookUp
FOREIGN KEY (RatingID) REFERENCES RatingLookUp(RatingID)

ALTER TABLE Movie
ADD CONSTRAINT fk_Movie_GenreLookUp
FOREIGN KEY (GenreID) REFERENCES GenreLookUp (GenreID)

When I run my code I keep getting the error in Visual Studio

MovieID cannot be null

but it should be when I insert a row. I also made sure to manually check to see if SQL Server had the IsIdentity set, which it is. So please help a confused programmer out.

You shouldn't insert NULL as a primary key to make it generate a value, you should just don't insert that value at all by not listing it among the fields to insert. As a sample;

INSERT INTO Movie (moviename, currentstock, genreid, ratingid, max_inventory, platforms)
                  VALUES ('name1', 1, 1, 1, 1, '1');

A simple working sample with your exact table creation .

Below statement is your problem. your table def shows MovieID not null. but in below statement you didn't provide that.

insert into Inventory(Max_Inventory, CurrentStock)
Values (@ChangeStock, @CurrentStock)

here is what you need to do.. first you need to retrieve Movie id which will be return from executing InsMovie sproc. than you need to use that Movie Id as a prameter to InsInvetory sproc call.

according to your table def movie id is required. also for Movie table you missed two column values that i have added which are required as well.

Please see blow updated stored procedures.

ALTER PROCEDURE [dbo].[InsInventory]
(

@CurrentStock int,
@ChangeStock int,
@MovieID int
)

as
begin
insert into Inventory(MovieID, Max_Inventory,CurrentStock)
    Values (@MovieID, @ChangeStock, @CurrentStock)

select SCOPE_IDENTITY();
END

ALTER PROCEDURE [dbo].[InsMovie] 
(
@GenreID int,
@RatingID int,
@Platform varchar(5),
@MovieName varchar(40),
@CurrentStock int, 
@max_inventory int
)
AS
BEGIN

Insert into Movie (CurrentStock, max_inventory, RatingID, MovieName, Platforms,GenreID)
    Values(@CurrentStock, @max_inventory, @RatingID,@MovieName, @Platform, @GenreID)

select SCOPE_IDENTITY();

SET NOCOUNT ON;


END

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM