这是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
答案 0 :(得分:1)
CREATE TRIGGER
期望触发器的主体为update-stmt
,insert-stmt
,delete-stmt
或select-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