如何在mysql中使用存储过程输出参数?

时间:2017-09-18 14:45:43

标签: mysql stored-procedures

我有两张桌子说

表1:

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

表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)
);

我必须创建一个存储过程来将数据插入表中:'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

如何使用以下格式'QW001'作为输出参数来实现Id。 有人可以帮我纠正上面的存储过程,因为我是新手。任何帮助表示赞赏。 TYIA!

1 个答案:

答案 0 :(得分:0)

你需要像这样使用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
相关问题