下面的触发器代码(从MSSQL转换为oracle)不起作用。
两列在表中不应有重复的行。我正在为此创建触发器。
有人可以帮助您更新/更正我的触发器中要使用的上述代码吗?
/*
**Unique Constraint for TestOracle - TestTinyInt.
*/
if (Update(UpdOperation) or Update(TestTinyInt)) THEN
IF Exists(
SELECT * FROM inserted i INNER LOOP JOIN TestOracle x ON
(i.TestTinyInt=x.TestTinyInt)
WHERE i.updoperation IN (0, 1) AND x.updoperation IN (0, 1) GROUP BY x.TestTinyInt
HAVING COUNT(*) > 1)
BEGIN
RAISERROR( 'Invalid attempt to enter duplicate TestTinyInt in TestOracle', 16, -1 )
ROLLBACK TRAN
RETURN
END
END
答案 0 :(得分:1)
最好的方法是在每列上创建2个唯一索引。通过这样做,您消除了特殊列中的重复项(例如提到的@a_horse_with_no_name)。
对于其他情况,您不需要使用triger,只需要简单的where条件
where Column_A not in (select Column_B from table) and Column_B not in (Select Column_A in table).
编辑: 如果必须在触发器THEN中完成:
create or replace trigger ... instead of insert or update on ...
Declare
dummy number;
Begin
select 1 into dummy from dual where :new.Column_A in (select Column_B from table) or new:.Column_B in (Select Column_A in table);
if dummy <> 1 THEN
INSERT
END IF;
END;
EDIT2:如果您不希望唯一索引,这里就是解决方案:
create or replace trigger ... instead of insert or update on ...
Declare
dummy number;
Begin
select count(*) into dummy from(
SELECT COL1 FROM (
(select :new.Column_A col1 from dual
UNION
select :new.Column_B from dual))
INTERSECT
SELECT COL2 FROM (
( SELECT COLUMN_A COL2 from table
UNION
SELECT COLUMN_B from table));
if dummy = 0 THEN
INSERT
END IF;
END;