变量
invoices表格:
创建一个存储过程
DELIMITER $$ create procedure get_risk_factor() begin -- 定义一个变量risk_factor:小数点前可有9位,小数点后2位,默认值为0 declare risk_factor decimal(9,2) default 0; declare invoices_total decimal(9,2); declare invoices_count int; -- 查询发票数及金额,并赋值到变量 select count(*),sum(invoice_total) into invoices_count,invoices_total from invoices; -- 定义变量risk_factor的值 set risk_factor=invoices_total/invoices_count*5; select risk_factor; end$$ DELIMITER ;
运行这个存储过程
call get_risk_factor();