不兼容的SQL触发器
作为SQL的新手,有人能帮我将这个触发器调整为sqlite或HSQLDB,或者建议一种不同的方法吗 我的数据库中有此表:不兼容的SQL触发器,sql,sqlite,hsqldb,Sql,Sqlite,Hsqldb,作为SQL的新手,有人能帮我将这个触发器调整为sqlite或HSQLDB,或者建议一种不同的方法吗 我的数据库中有此表: CREATE TABLE IF NOT EXISTS dfTree ( id INTEGER, parentId INTEGER, name VARCHAR(20), depth INTEGER, lineage VARCHAR(100) ) 我尝试设置触发器,但它似乎与我尝试的sqlite和HSQLDB都不兼容 CREATE TRIGGER dfTree_Insert
CREATE TABLE IF NOT EXISTS dfTree
(
id INTEGER,
parentId INTEGER,
name VARCHAR(20),
depth INTEGER,
lineage VARCHAR(100)
)
我尝试设置触发器,但它似乎与我尝试的sqlite和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
触发器应该在新条目时计算深度和沿袭字段。我正在看《树木结构》的文章
再说一次,作为SQL的新手,有人能帮我将这个触发器调整为sqlite或HSQLDB,或者建议一种不同的方法吗
谢谢 这应该有效:
CREATE TRIGGER dfTree_InsertTrigger
after insert ON dfTree
for each row
begin
UPDATE dftree SET
depth = (select coalesce(depth + 1, 1) from (select depth from dftree parent where parent.id = new.parentid union all select null depth)),
lineage = (select coalesce(lineage,'/') || dftree.id || '/' from (select lineage from dftree parent where parent.id = new.parentid union all select null lineage))
where new.id = id;
end;
几句话:
-SQL中字符串连接的运算符是| |,而不是+
-coalesce应该比isnull更易于移植
-工会全体。。part确保了即使没有家长,我们也能始终得到一条线像个魔咒一样工作!非常感谢!