DB2 如何防止删除值(使用 'before delete' 触发器)?
DB2 how to prevent to delete values (with 'before delete' trigger)?
我有这样的代码:
CREATE TABLE A10.STUDENTS
( IDSTUDENT INTEGER NOT NULL,
LASTNAME VARCHAR(30) NOT NULL,
FIRSTNAME VARCHAR(30) NOT NULL,
MARK DECIMAL(5,1) NOT NULL,
PRIMARY KEY ( IDSTUDENT )
);
ALTER TABLE A10.STUDENTS ADD CONSTRAINT CHECK_MARK CHECK(CAST(MARK AS DECIMAL(5,1)) IN (2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0));
INSERT INTO A10.STUDENTS (LASTNAME,FIRSTNAME,MARK) VALUES ('Smith','John',2.0);
我想添加“删除前”触发器,防止删除 3.0 以下的标记。
-- <ScriptOptions statementTerminator="@" />
CREATE TRIGGER A10.DONT_DELETE_MARKS
NO CASCADE BEFORE DELETE
ON A10.STUDENTS
REFERENCING NEW AS N OLD AS O
FOR EACH ROW MODE DB2SQL NOT SECURED
BEGIN
--and what here?
END@
-- <ScriptOptions statementTerminator=";" />
我试过类似的方法:
raise_application_error(-20001,'Records can not be deleted');
但是没用。
感谢您的帮助:)
“不起作用”是什么意思?
Db2 是什么平台和版本?
尝试
signal sqlstate xxxxx
set message_text = 'Records can not be deleted';
将 xxxxx
替换为您希望使用的 SQL 州代码。注意
SQLSTATE values are comprised of a two-character class code value,
followed by a three-character subclass code value. Class code values
represent classes of successful and unsuccessful completion
conditions. If you want to use SQLSTATE as the basis of your
application's return codes, you can define your own SQLSTATE classes
or subclasses using the following guidelines:
- SQLSTATE classes that begin with the characters 7 through 9 or I through Z can be defined. Within these classes, any subclass can be
defined.
- SQLSTATE classes that begin with the characters 0 through 6 or A through H are reserved for the database manager. Within these classes,
subclasses that begin with the characters 0 through H are reserved for
the database manager. Subclasses that begin with the characters I
through Z can be defined.
我有这样的代码:
CREATE TABLE A10.STUDENTS
( IDSTUDENT INTEGER NOT NULL,
LASTNAME VARCHAR(30) NOT NULL,
FIRSTNAME VARCHAR(30) NOT NULL,
MARK DECIMAL(5,1) NOT NULL,
PRIMARY KEY ( IDSTUDENT )
);
ALTER TABLE A10.STUDENTS ADD CONSTRAINT CHECK_MARK CHECK(CAST(MARK AS DECIMAL(5,1)) IN (2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0));
INSERT INTO A10.STUDENTS (LASTNAME,FIRSTNAME,MARK) VALUES ('Smith','John',2.0);
我想添加“删除前”触发器,防止删除 3.0 以下的标记。
-- <ScriptOptions statementTerminator="@" />
CREATE TRIGGER A10.DONT_DELETE_MARKS
NO CASCADE BEFORE DELETE
ON A10.STUDENTS
REFERENCING NEW AS N OLD AS O
FOR EACH ROW MODE DB2SQL NOT SECURED
BEGIN
--and what here?
END@
-- <ScriptOptions statementTerminator=";" />
我试过类似的方法:
raise_application_error(-20001,'Records can not be deleted');
但是没用。
感谢您的帮助:)
“不起作用”是什么意思?
Db2 是什么平台和版本?
尝试
signal sqlstate xxxxx
set message_text = 'Records can not be deleted';
将 xxxxx
替换为您希望使用的 SQL 州代码。注意
SQLSTATE values are comprised of a two-character class code value, followed by a three-character subclass code value. Class code values represent classes of successful and unsuccessful completion conditions. If you want to use SQLSTATE as the basis of your application's return codes, you can define your own SQLSTATE classes or subclasses using the following guidelines:
- SQLSTATE classes that begin with the characters 7 through 9 or I through Z can be defined. Within these classes, any subclass can be defined.
- SQLSTATE classes that begin with the characters 0 through 6 or A through H are reserved for the database manager. Within these classes, subclasses that begin with the characters 0 through H are reserved for the database manager. Subclasses that begin with the characters I through Z can be defined.