Haskell Sqlite 3 CASE语句语法错误

时间:2013-12-30 21:14:50

标签: sqlite haskell triggers case

这是HDBC和Sqlite3。它说“CASE”附近有语法错误。但我能找到这样的。有没有?

calculateNoOfStocksTraded :: String-> Database.HDBC.Sqlite3.Connection-> IO() calculateNoOfStocksTraded code conn = do

                                run conn " CREATE TRIGGER calcStocks \
                                           \ AFTER INSERT ON historicalData \
                                           \ FOR EACH ROW \
                                           \ BEGIN \
                                           \ CASE WHEN (SELECT COUNT(*) FROM historicalData) >= 1  THEN \
                                           \    UPDATE company \
                                           \    SET noOfStocks=(SELECT SUM(volume) FROM historicalData  WHERE companyCode= ? ) \
                                           \    WHERE code= ? ; \
                                           \ ELSE \
                                           \    UPDATE company \
                                           \    SET noOfStocks=0  \
                                           \    WHERE code= ? ; \
                                           \ END \

                                           \ END; " [toSql code,toSql code, toSql code]

                                commit conn

1 个答案:

答案 0 :(得分:1)

CREATE TRIGGER期望触发器的主体为update-stmtinsert-stmtdelete-stmtselect-stmt之一;所以我们需要根据其中一个重写触发器;

事实证明,您可以简化触发器,以便我们可以用任何内容替换CASE语法;如果count(foo.*) < 1,则必须是sum(foo.bar) = 0的情况,因此:

在sqlite中; count(foo.*) < 1,导致sum(foo.bar) IS NULL这是不幸的;但我们可以通过将null转换为0 coalesce()来使其做正确的事情:

CREATE TRIGGER calcStocks
AFTER INSERT ON historicalData
FOR EACH ROW
BEGIN
   UPDATE company
   SET noOfStocks=(SELECT COALESCE(SUM(volume), 0) FROM historicalData  WHERE companyCode= ? )
   WHERE code= ? ;
END