[英]how to output a parameter using a stored procedure in mysql?
I have two tables say 我有两张桌子说
table1: 表格1:
create table Person(
Id int primary key auto_increment,
name varchar(20),
Gender char(1)
);
table2: 表2:
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. 如何以以下格式'QW001'获得ID作为输出参数。 Can someone help me out in correcting the above stored procedure,since i'm new to this. 有人可以帮助我纠正上述存储过程,因为我是新来的。 Any help is appreciated. 任何帮助表示赞赏。 TYIA! TYIA!
you need to use LAST_INSERT_ID() like this : 您需要像这样使用LAST_INSERT_ID():
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.