2021-08-24

CREATE DEFINER=`root`@`localhost` PROCEDURE `get_clients_by_state`(state char(2))
BEGIN
	if state is null then
    set state = 'ca';
    end if;
select *
from clients c
where c.state = state;
END

CREATE DEFINER=`root`@`localhost` PROCEDURE `get_clients_by_state`(state char(2))
BEGIN
	if state is null then
		select * from clients;
	else
		select * from clients c
		where c.state = state;
    end if;
END

CREATE DEFINER=`root`@`localhost` PROCEDURE `get_clients_by_state`(state char(2))
BEGIN
	select * from clients c
	where c.state = ifnull(state, c.state);
END

CREATE DEFINER=`root`@`localhost` PROCEDURE `get_payments`
	(client_id int, payment_method tinyint)
BEGIN
select * from payments p
where (p.client_id = ifnull(client_id, p.client_id)) and
	(p.payment_method = ifnull(payment_method, p.payment_method));
END

CREATE DEFINER=`root`@`localhost` PROCEDURE `make_payment`(
	invoice_id INT,
    payment_amount DECIMAL(9,2),
    payment_date DATE
)
BEGIN
	IF payment_amount <= 0 THEN 
		SIGNAL SQLSTATE '22003'
        SET MESSAGE_TEXT = 'Invalid payment amount';
	end if;
    
	UPDATE invoices i
    SET 
    i.payment_total = payment_amount,
    i.payment_date = payment_date
    where i.invoice_id = invoice_id;
END

CREATE DEFINER=`root`@`localhost` PROCEDURE `get_unpaid_invoices_for_client`(
	client_id int,
    OUT invoices_count int,
    OUT invoices_total decimal(9,2))
BEGIN
	select count(*), sum(invoice_total)
    into invoices_count,invoices_total
	from invoices i
	where i.client_id = client_id and i.payment_total = 0;
END

CREATE DEFINER=`root`@`localhost` PROCEDURE `get_risk_factor`()
BEGIN
	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;
    
    set risk_factor = invoices_total / invoices_count * 5;
    
    select risk_factor;
END

CREATE DEFINER=`root`@`localhost` FUNCTION `get_risk_factor_for_client`(client_id INT)
	RETURNS int
    READS SQL DATA
BEGIN
	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 i
    where i.client_id = client_id;
    
    set risk_factor = invoices_total / invoices_count * 5;
    
	return ifnull(risk_factor,0);

END

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值