我想执行许多这样的SQL UPDATE语句:

UPDATE cityd SET time_zone='-7.000000' WHERE locId = 173567;
UPDATE cityd SET time_zone='-8.000000' WHERE locId = 173568;
UPDATE cityd SET time_zone='-6.000000' WHERE locId = 173569;
UPDATE cityd SET time_zone='-5.000000' WHERE locId = 173570;
UPDATE cityd SET time_zone='-6.000000' WHERE locId = 173571;

我想优化交易时间,因此我需要使用BEGIN TRANSACTION/COMMIT对。如何在SQLtite Manager的SQL语法中编写此代码?

编辑:
当我在SQLite Manager中使用标准语法时,收到以下错误消息:
“SQLiteManager:BEGIN TRANSACTION; [无法在事务内启动事务”

最佳答案

根据SQL Documention,有两种受支持的CASE语法

CASE x WHEN w1 THEN r1 WHEN w2 THEN r2 ELSE r3 END

CASE WHEN x = w1 THEN r1 WHEN x = w2 THEN r2 ELSE r3 END

因此,您可以将多个UPDATE语句进一步简化为
UPDATE  cityd
SET     time_zone = CASE locId
                        WHEN 173567 THEN '-7.000000'
                        WHEN 173568 THEN '-8.000000'
                        WHEN 173569 THEN '-6.000000'
                        WHEN 173570 THEN '-5.000000'
                        WHEN 173571 THEN '-6.000000'
                    END
WHERE   locId IN (173567, 173568, 173569, 173570, 173571)

关于SQLITE批量UPDATE语句,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15501779/

10-09 01:28