2016-11-13 141 views
1

我在phpmyadmin中创建了一个mySQL过程,它一直抛出这个错误:#1064 - 你的SQL语法错误;检查对应于您的MySQL服务器版本的手册,以便在第18行使用正确的语法在mySQL中创建过程

我试过下面的代码,没有使用分隔符,没有用。任何人都可以指出这段代码有什么问题吗?谢谢!

DELIMITER \\ 
CREATE PROCEDURE ORDERBOOKS(IN store VARCHAR(10), IN title VARCHAR(50)) 
BEGIN 
    DECLARE order_num VARCHAR(100); 
    DECLARE tod DATE; 
    SELECT @ordered_qty := qty FROM customer_sales WHERE customer_sales.store_id = store AND customer_sales.title_id = title; 
    SELECT @in_stock := qty FROM store_inventories WHERE store_inventories.stor_id=store AND store_inventories.title_id = title; 
    SELECT @threshold := minStock FROM store_inventories WHERE store_inventories.stor_id=store AND store_inventories.title_id = title; 
    SET order_num = CONCAT(store, title); 
    SET tod = GETDATE(); 
    IF (@ordered_qty < (@in_stock - @threshold)) THEN UPDATE store_inventories SET qty = (@in_stock - @ordered_qty) WHERE store_inventories.stor_id = store and store_inventories.title_id = title; 
    ELSE 
     INSERT INTO pending_orders(stor_id, ord_num, title_id, qty, date, fulfilled) VALUES(store, order_num, title, (@ordered_qty + @threshold), tod ,1); 
     INSERT INTO sales VALUES(store, order_num, tod); 
     INSERT INTO salesdetail VALUES(store, order_num, title , (@ordered_qty + @threshold),0); 
     UPDATE pending_orders SET fulfilled=0 WHERE pending_orders.stor_id = store AND pending_orders.title_id = title; 
     UPDATE store_inventories SET qty = (@threshold + @in_stock) WHERE store_inventories.stor_id = store and store_inventories.title_id= title; 
     DELETE FROM pending_orders WHERE pending_orders.stor_id = store AND pending_orders.title_id = title; 
END\\ 

DELIMITER ; 
+4

缺少结束如果 – e4c5

+0

相依尖端:分配用户定义'@'变量是安全的(且无声)如果使用标量子查询:'SET @ordered_qty = (SELECT qty FROM customer_sales WHERE customer_sales.store_id = store AND customer_sales.title_id = title);'如果你决定重构这个,你可能会倾向于将原来的语句重写为'SELECT ... INTO', (经常)[意外后果](http://dba.stackexchange.com/a/35207/11651)旧值从调用到同一连接的调用。现在更好地使用更安全的任务范例,fwiw。 –

回答

0

尝试这一个:

CREATE PROCEDURE ORDERBOOKS(IN store VARCHAR(10), IN title VARCHAR(50)) 
BEGIN 
    DECLARE order_num VARCHAR(100); 
    DECLARE tod DATE; 
    SELECT @ordered_qty := qty FROM customer_sales WHERE customer_sales.store_id = store AND customer_sales.title_id = title; 
    SELECT @in_stock := qty FROM store_inventories WHERE store_inventories.stor_id=store AND store_inventories.title_id = title; 
    SELECT @threshold := minStock FROM store_inventories WHERE store_inventories.stor_id=store AND store_inventories.title_id = title; 
    SET order_num = CONCAT(store, title); 
    SET tod = GETDATE(); 
    IF (@ordered_qty < (@in_stock - @threshold)) THEN UPDATE store_inventories SET qty = (@in_stock - @ordered_qty) WHERE store_inventories.stor_id = store and store_inventories.title_id = title; 
    ELSE 
     INSERT INTO pending_orders(stor_id, ord_num, title_id, qty, date, fulfilled) VALUES(store, order_num, title, (@ordered_qty + @threshold), tod ,1); 
     INSERT INTO sales VALUES(store, order_num, tod); 
     INSERT INTO salesdetail VALUES(store, order_num, title , (@ordered_qty + @threshold),0); 
     UPDATE pending_orders SET fulfilled=0 WHERE pending_orders.stor_id = store AND pending_orders.title_id = title; 
     UPDATE store_inventories SET qty = (@threshold + @in_stock) WHERE store_inventories.stor_id = store and store_inventories.title_id= title; 
     DELETE FROM pending_orders WHERE pending_orders.stor_id = store AND pending_orders.title_id = title; 
    END IF 
END 
+0

';'在'END IF'之后是必需的,为了清晰起见,您应该将语句分隔符和分隔符声明留在中。 –