简体   繁体   English

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

[英]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.

相关问题 如何使用doctrine 2在symfony 2中使用输出参数调用mysql存储过程? - How to call mysql stored procedure with output parameter in symfony 2 using doctrine 2? 如何使用mysql将参数传递给存储过程 - how to pass parameter to stored procedure using mysql 使用PHP返回mysql存储过程的输出参数 - Returning the output parameter of a mysql stored procedure using PHP 使用PDO从mysql中的存储过程中检索输出参数 - Using PDO to retrieve an output parameter from a stored procedure in mysql MySQL:如何调用只有输出参数的存储过程? - MySQL: how to call a stored procedure with only an output parameter? 如何使用休眠实体管理器在Spring Boot中使用输出参数调用MySQL存储过程 - How to call MySQL stored procedure with output parameter in spring boot using hibernate entitymanager 如何使用 Pomelo ORM 为 .net core 的 MySQL 存储过程传递输出参数 - How to pass the output parameter for MySQL stored procedure for .net core using Pomelo ORM mysql中存储过程中的EXISTS和输出参数 - EXISTS and output parameter in stored procedure in mysql 使用ExecuteDataset使用输出参数调用存储过程 - Call stored procedure with an output parameter using ExecuteDataset 在存储的mysql过程中声明一个用作参数的变量 - Declaring a variable for using as parameter in stored mysql procedure
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM