2017-10-16 65 views
0

我试图创建一个程序,它将数据同时添加到两个表中,但使用mySQL,但我没有太多经验在数据库中使用if/else语句,所以有人可以告诉我如何我可以解决我的代码在数据库中添加过程

这里的属性:

Customer information: ID,Name, Address, Telephone. 

Staff information: ID, Name, Phone 

Camera/Len Type: ID,Name 

Camera: ID, typeID, Model, Status, Color, Hiring Price (per day), Paying Price (in case of loss). 

Len: ID, TypeID, Model, Status, Hiring Price (per day), Paying Price (in case of loss). 

Order orderID, customerID, employeeID, hireDate, returnDate 

OrderDetail: orderID, cameraID, lenID, cameraQuantity, lenQuantity, Status(paid/not paid), pStatus(Lost/Ok) 

这里的程序代码:

DELIMITER $$ 
Create Procedure addOrder(IN cust_id int, IN camera_id int, IN len_id int, 
IN emp_id int, IN c_quantity int,IN l_quantity int, 
IN status char(30), IN p_Status char(30), IN hire_date date, IN return_date 
date) 
begin 
Declare order_id int; 
Declare hireTime int; 
Declare totalC int; 
Declare totalL int; 
Declare totalP int; 

Select order_id = max(orderID) from orders; 
if orderID is null then set order_id = 1; 

Set hireTime = DATEDILL(hire_date, return_date); 
if hire_date == return_date then set hireTime = 1; 

if p_Status = "Lost" then set 
    select totalC = hireTime*c_quantity*hPrice + pPrice from Camera as C 
    inner join orderDetail as OD on C.camID = OD.camera_id inner join Orders 
    as O on O.orderID = OD.order_id; 

    select totalL = hireTime*l_quantity*hPrice + pPrice from Lens as L 
    inner join orderDetail as OD on L.lenID = OD.len_id inner join Orders as 
    O on O.orderID = OD.order_id; 

    Set totalP = totalL + totalC; 
else set 
    select totalC = hireTime*c_quantity*hPrice + pPrice from Camera as C 
    inner join orderDetail as OD on C.camID = OD.camera_id inner join Orders 
    as O on O.orderID = OD.order_id; 

    select totalL = hireTime*l_quantity*hPrice + pPrice from Lens as L 
    inner join orderDetail as OD on L.lenID = OD.len_id inner join Orders as 
    O on O.orderID = OD.order_id; 

    Set totalP = totalL + totalC; 
end if; 

insert into Orders(custID, empID, hireDate, returnDate) 
    values (cust_id, emp_id,hire_date, return_date); 

insert into orderDetail(orderID, cameraID, lenID, totalPrice, camQuantity, 
lenQuantity, status, pStatus) values (order_id,camera_id, len_id, totalP, 
c_quantity, l_quantity, status, p_Status); 
end $$ 
DELIMITER 
+0

您打算为订单详细信息的每一行调用它? –

+0

这里有一些明显的问题,不存在任何的日期标记功能 - 你可能意思是datediff,null安全等于<=> not ==,if后面的设置语句then然后else不正确,因为它没有设置任何东西,为什么当你看起来并没有使用这个表中的任何东西时,如果你正在为每个订单明细调用这个命令,那么你可能不想每次都插入到订单中,而是在select语句中引用订单。如果您可以将问题中的示例数据发布为具有预期结果的文本或sqlfiddle,这将有所帮助。 –

回答

0

我觉得你需要使用视图表使用存储后添加的所有表d程序。我给你和我​​想你可以弄清楚的例子。

CREATE PROCEDURE [dbo].[sp_CMIDByID] @UID INT, @TID INT 
AS 
BEGIN 
DECLARE @CMID INT 

IF(@TID IS NULL AND @UID IS NOT NULL) 
BEGIN 
    SELECT @TID = TID, @CMID = CMID FROM tbl_U WHERE UID = @UID  
END 


IF (@CMID IS NULL AND @TID IS NOT NULL) 
BEGIN 
    SELECT @CMID = CMID FROM tbl_T WHERE TID = @TID 
END 

SELECT @CMID 
END 
+0

这是一个sqlserver的答案,但问题是为MySQL - 有显着差异。 –