Hi, I have a problem with my IDENTITY column (item_id) and my INSERT statement into my 'inventory' table.
The stored procedure is executed from VB6, if the user enters an error, and the INSERT statement does not complete the IDENTITY column still increments. i.e. The 'amount' field is NOT NULL, if the user forgets to enter an amount, an error occurs in VB and the row is not inserted, but the IDENTITY column still increments.
I am NOT worried about filling in IDENTITY column gaps if a record is deleted, but I do want IDENTITY values to be in sequence (NO GAPS) when inserting records.
I have looked at DBCC CHECKIDENT, but dont understand how to use the values returned from it.
Here is the SP I am using:
CREATE PROCEDURE insert_inventory
@item_id int,
@item_name varchar(20),
@description varchar(100),
@notes varchar(255),
@amount char(8)
AS
SET NOCOUNT ON
DECLARE @transaction_date datetime
BEGIN TRANSACTION
IF (@item_name = '') SET @item_name = NULL
IF (@description = '') SET @description = NULL
IF (@notes = '') SET @notes = NULL
IF (@amount = '') SET @amount = NULL
SET @transaction_date = GETDATE()
INSERT INTO inventory (item_name, item_description, notes) VALUES (@item_name, @description, @notes)
IF @@ROWCOUNT = 0 OR @@ERROR <> 0
BEGIN
RAISERROR('insert_inventory SP FAILED', 16, 1)
ROLLBACK TRANSACTION
RETURN
END
SET @item_id = @@IDENTITY
INSERT INTO expenditure (item_id, transaction_date, amount) VALUES (@item_id, @transaction_date, CAST(@amount AS money))
IF @@ROWCOUNT = 0 OR @@ERROR != 0
BEGIN
RAISERROR('insert_inventory SP FAILED', 16, 1)
ROLLBACK TRANSACTION
RETURN
END
COMMIT TRANSACTION
The stored procedure is executed from VB6, if the user enters an error, and the INSERT statement does not complete the IDENTITY column still increments. i.e. The 'amount' field is NOT NULL, if the user forgets to enter an amount, an error occurs in VB and the row is not inserted, but the IDENTITY column still increments.
I am NOT worried about filling in IDENTITY column gaps if a record is deleted, but I do want IDENTITY values to be in sequence (NO GAPS) when inserting records.
I have looked at DBCC CHECKIDENT, but dont understand how to use the values returned from it.
Here is the SP I am using:
CREATE PROCEDURE insert_inventory
@item_id int,
@item_name varchar(20),
@description varchar(100),
@notes varchar(255),
@amount char(8)
AS
SET NOCOUNT ON
DECLARE @transaction_date datetime
BEGIN TRANSACTION
IF (@item_name = '') SET @item_name = NULL
IF (@description = '') SET @description = NULL
IF (@notes = '') SET @notes = NULL
IF (@amount = '') SET @amount = NULL
SET @transaction_date = GETDATE()
INSERT INTO inventory (item_name, item_description, notes) VALUES (@item_name, @description, @notes)
IF @@ROWCOUNT = 0 OR @@ERROR <> 0
BEGIN
RAISERROR('insert_inventory SP FAILED', 16, 1)
ROLLBACK TRANSACTION
RETURN
END
SET @item_id = @@IDENTITY
INSERT INTO expenditure (item_id, transaction_date, amount) VALUES (@item_id, @transaction_date, CAST(@amount AS money))
IF @@ROWCOUNT = 0 OR @@ERROR != 0
BEGIN
RAISERROR('insert_inventory SP FAILED', 16, 1)
ROLLBACK TRANSACTION
RETURN
END
COMMIT TRANSACTION