2016-07-26 5 views
0

Мне нужно сохранить дерево в SQLite. Я нашел очень хороший метод для SQL Server здесь:Преобразование запросов из SQL Server в SQLite

Tree structures in ASP.NET and SQL Server

Концепция с триггерами ясно конца вперед мышления.

я пытаюсь перевести описанные триггеры SQLite

SQL Server Версия:

CREATE TRIGGER dfTree_UpdateTrigger 
ON dfTree 
FOR UPDATE AS 
-- if we've modified the parentId, then we 
-- need to do some calculations 
IF UPDATE (parentId) 
UPDATE child 
-- to calculate the correct depth of a node, remember that 
--  - old.depth is the depth of its old parent 
--  - child.depth is the original depth of the node 
--   we're looking at before a parent node moved. 
--   note that this is not necessarily old.depth + 1, 
--   as we are looking at all depths below the modified 
--   node 
-- the depth of the node relative to the old parent is 
-- (child.depth - old.depth), then we simply add this to the 
-- depth of the new parent, plus one. 
    SET depth = child.depth - old.depth + ISNULL(parent.depth + 1,0), 
    lineage = ISNULL(parent.lineage,'/') + LTrim(Str(old.id)) + '/' + 
        right(child.lineage, len(child.lineage) -  len(old.lineage)) 
-- if the parentId has been changed for some row 
-- in the "inserted" table, we need to update the 
-- fields in all children of that node, and the 
-- node itself     
FROM dfTree child 
INNER JOIN inserted old ON child.lineage LIKE old.lineage + '%' 
-- as with the insert trigger, attempt to find the parent 
-- of the updated row 
LEFT OUTER JOIN dfTree parent ON old.parentId=parent.id 

Моя SQLite версии до сих пор:

CREATE TRIGGER IF NOT EXISTS sc_compare_UpdateTrigger 
AFTER UPDATE ON dfTree 
FOR EACH ROW 
BEGIN 
    UPDATE child 
     SET depth = child.depth - OLD.depth + IFNULL(parent.depth + 1,0), 
     lineage = IFNULL(parent.lineage,'/') + LTrim(CAST(OLD.id AS TEXT)) +  '/' + 
      right(child.lineage, len(child.lineage) - len(OLD.lineage)) 
    FROM dfTree child 
    INNER JOIN inserted OLD ON child.lineage LIKE OLD.lineage + '%' 
    LEFT OUTER JOIN dfTree parent ON OLD.parentId=parent.id 
END; 

Для этой версии, «Ошибка: рядом "FROM": синтаксическая ошибка "occours.

Я на правильном пути? Возможно ли создать эту триггерную функциональность с помощью SQLite?

ответ

0

Синтаксис Sqlite UPDATE не поддерживает предложение FROM. Примечание также нет insetred стол, только NEW, OLD строки в FOR EACH ROW триггер.

+0

Спасибо, я вижу. Любая идея, как получить тот же результат, что и с сервером sql? – metamagicson

Смежные вопросы