2016-11-13 51 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'之後是必需的,爲了清晰起見,您應該將語句分隔符和分隔符聲明留在中。 –