I have a data table called ‘batch’ which has been created as follows;
CREATE TABLE [dbo].[batch](
[id] [int] IDENTITY(1,1) NOT NULL,
[batch_ref] [varchar](8) NOT NULL,
[data_number_of_rows] [int] NOT NULL,
CONSTRAINT [pk_batch] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY],
CONSTRAINT [uc_batch_ref] UNIQUE NONCLUSTERED
(
[batch_ref] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
Also, I have a stored procedure called ‘update_batch_data_number_of_rows’ written on ‘batch’ table as follows;
CREATE PROCEDURE [dbo].[update_batch_data_number_of_rows]
@batch_ref varchar(8),
@data_number_of_rows int
AS
BEGIN
UPDATE [dbo].[batch]
SET [data_number_of_rows] = @data_number_of_rows
WHERE batch_ref = @batch_ref;
SELECT @@ROWCOUNT AS Updated;
END
GO
When I need to update a value in ‘batch’ data table according to the stored procedure if I would given the query as follows, that means batch_ref value exceeding the maximum character length of it (it should have 8 length but trying with 10 length), I need to have a SQL exception for exceeding maximum character length. I tried with changing data types from varchar(8) to char(8), nchar(8) and nvarchar(8) but expected answer wasn’t received. I’m looking for a possible way of doing this. Can anyone help me to build up this logic?
BEGIN
UPDATE [dbo].[batch]
SET [data_number_of_rows] = '55'
WHERE batch_ref = '12345678910';
SELECT @@ROWCOUNT AS Updated;
END
GO