Étant débutant en SQL, est-ce que certains peuvent m'aider à adapter ce déclencheur à sqlite ou à HSQLDB, ou peut-être suggérer une approche différente?Déclencheur SQL incompatible
J'ai ce tableau dans ma db:
CREATE TABLE IF NOT EXISTS dfTree
(
id INTEGER,
parentId INTEGER,
name VARCHAR(20),
depth INTEGER,
lineage VARCHAR(100)
)
J'essaie de mettre un déclencheur, mais il semble incompatible avec les deux db j'ai essayé (sqlite et HSQLDB)
CREATE TRIGGER dfTree_InsertTrigger ON dfTree
FOR INSERT AS
UPDATE child
-- set the depth of this "child" to be the
-- depth of the parent, plus one.
SET depth = ISNULL(parent.depth + 1,0),
-- the lineage is simply the lineage of the parent,
-- plus the child's ID (and appropriate '/' characters
lineage = ISNULL(parent.lineage,'/') + LTrim(Str(child.id)) + '/'
-- we can't update the "inserted" table directly,
-- so we find the corresponding child in the
-- "real" table
FROM dfTree child INNER JOIN inserted i ON i.id=child.id
-- now, we attempt to find the parent of this
-- "child" - but it might not exist, so these
-- values may well be NULL
LEFT OUTER JOIN dfTree parent ON child.parentId=parent.id
(Le déclencheur est censé calculer les champs "profondeur" et "lignée" sur une nouvelle entrée.Je suis l'article sur Sturctures d'arbre à http://www.developerfusion.com/article/4633/tree-structures-in-aspnet-and-sql-server/2/
Encore une fois, étant débutant dans SQL, peut-être un peu m'aider pt ce déclencheur à sqlite ou HSQLDB, ou peut-être suggérer une approche différente?
Merci!
Travaillé comme un charme! Merci beaucoup! – user653952