'How to increment a char value
Does anyone know how can I auto-increment a char value in SQL Server?
If I have
create table mae_student (
id_student char (3),
First_Name varchar (75),
Last_Name varcha (75)
)
Where the first row must be 001, the second 002 and so on until 999.
How can I handle this in a query, using Microsoft SQL Server?
Because in the query I have:
Create Procedure new_student(
-- user data
@par_id_student char (3),
@par_First_Name varchar (75),
@par_Last_Name varcha (75)
)
AS
BEGIN
SET NO COUNT ON
-- 1. Verify the id not come empty
If rtrim(ltrim(@par_cod_student))=''
begin
select @parretorno=-2,@parerrorstate='',@parerrormessage='The ID cannot be empty!';
select @parretorno as retorno,@parerrorstate as estado,@parerrormessage as mensaje;
return;
end;
-- 2. Verify the par_First_Name not come empty
If rtrim(ltrim(@par_First_Name))=''
begin
select @parretorno=-2,@parerrorstate='',@parerrormessage='The First Name cannot be empty!';
select @parretorno as retorno,@parerrorstate as estado,@parerrormessage as mensaje;
return;
end;
-- 3. Verify the par_Last_Name not come empty
If rtrim(ltrim(@par_Last_Name))=''
begin
select @parretorno=-2,@parerrorstate='',@parerrormessage='The Last Name cannot be empty!';
select @parretorno as retorno,@parerrorstate as estado,@parerrormessage as mensaje;
return;
end;
-- 4. Verify the student does not Exist
if exists(select cod_student from mae_student where id_student=@par_id_student)
begin
select @parretorno=-6,@parerrorstate='',@parerrormessage='The ID Student is Already Register !';
select @parretorno as retorno,@parerrorstate as estado,@parerrormessage as mensaje;
return;
end;
-- Transaction
begin transaction
begin try
-- Add the new student
insert into erp.mae_student(cod_student,First_Name,Last_Name)
values (@par_cod_student,@par_First_Name,@par_Last_Name);
-- Comfirm the transaction
commit;
-- Message for user
select @parretorno=1,@parerrorstate=@parcodigo,@parerrormessage='Success';
select @parretorno as retorno,@parerrorstate as estado,@parerrormessage as mensaje;
end try
end
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
