0

I have two tables say

table1:

create table Person(
  Id int primary key auto_increment,
  name varchar(20),
  Gender char(1)
);

table2:

create table Details(
  Age int,
  Phone int(10) primary key,
  Address varchar(100)
  Id foreign key(Id) references Person(Id),
  Name foreign key(Name) references Person(Name)
);

i will have to create a stored procedure to insert data into table:'Details'

create procedure usp_insert(
  IN Name varchar(100),
  IN Age int,
  IN Phone int(10),
  IN Address varchar(100),
  OUT Id int
)
begin

//want to output the Id as QW001 on inserting the data into the Details table.

insert into Details(Name,Age,Phone,Address) values(name,age,phone,address)
end

How can i achieve the Id in the following format 'QW001' as output parameter. Can someone help me out in correcting the above stored procedure,since i'm new to this. Any help is appreciated. TYIA!

Raymond Nijland
  • 11,488
  • 2
  • 22
  • 34
Kenz
  • 269
  • 1
  • 3
  • 10
  • MySQL auto-increment columns are integers only. See https://stackoverflow.com/questions/23331194/concatenating-a-string-and-primary-key-id-while-inserting/23331246#23331246 – Bill Karwin Sep 18 '17 at 15:40

1 Answers1

0

you need to use LAST_INSERT_ID() like this :

create procedure usp_insert(
  IN Name varchar(100),
  IN Age int,
  IN Phone int(10),
  IN Address varchar(100),
  OUT Id int
)
begin

insert into Details(Name,Age,Phone,Address) values(name,age,phone,address);
set Id =CONCAT("QW00", LAST_INSERT_ID()) AS ConcatenatedString;
end
colapiombo
  • 179
  • 1
  • 3
  • 13