請教SQL高手
假若我有一個 SQL Table
我想對其中的這二筆做修改資料
就會出現如下的錯誤訊息
經判定,是我寫的SQL-Tigger造成的
如果我把這 Tigger 停掉不用
上面的 SQL-Update 就可以成功
但我不懂的是
為什麼 Tigger 寫的程式、欄位,都與本次update的欄位都無關
為何會有 [Err] 21000 的錯誤??
接下來我又測試
若我把那二筆,用一筆、一筆的修改卻又可以成功?
我的 SQL-Tigger是有什麼樣的問題
我該如何修改成不會有這樣的錯誤?
請大家幫忙,謝謝
-- 使用 PostgreSQL
-- create trigger 時有 for each row
-- 這樣就能避免上面的情況
-- 提供你參考
create table ithelp190822 (
id int generated always as identity primary key
, mg001 text not null
, mg002 text not null
, mg201 numeric(12,6) not null
);
insert into ithelp190822 (mg002, mg001, mg201)
values
('6400VV', '2423118', 333.000000),
('6400VV', '2423927', 333.000000);
insert into ithelp190822 (mg002, mg001, mg201)
values
('#6300VV', '2423927', 333.000000);
--
create or replace function trifun_ithelp190822()
returns trigger
as
$$
begin
if left(new.mg002, 1) = '#'
then
raise exception '不可修改#開頭的項目';
end if;
return new;
end;
$$ language plpgsql;
create trigger tri_bu_ithelp190822
before update on ithelp190822
for each row execute procedure trifun_ithelp190822();
--
[miku]# \d ithelp190822
Table "miku.ithelp190822"
+--------+---------------+-----------+----------+------------------------------+
| Column | Type | Collation | Nullable | Default |
+--------+---------------+-----------+----------+------------------------------+
| id | integer | | not null | generated always as identity |
| mg001 | text | | not null | |
| mg002 | text | | not null | |
| mg201 | numeric(12,6) | | not null | |
+--------+---------------+-----------+----------+------------------------------+
Indexes:
"ithelp190822_pkey" PRIMARY KEY, btree (id)
Triggers:
tri_bu_ithelp190822 BEFORE UPDATE ON ithelp190822 FOR EACH ROW EXECUTE PROCEDURE trifun_ithelp190822()
---
update ithelp190822
set mg201 = 555
where mg002 = '6400VV';
UPDATE 2
Time: 3.967 ms
[miku]# commit;
COMMIT
Time: 2.298 ms
[miku]# select * from ithelp190822;
+----+---------+---------+------------+
| id | mg001 | mg002 | mg201 |
+----+---------+---------+------------+
| 3 | 2423927 | #6300VV | 333.000000 |
| 1 | 2423118 | 6400VV | 555.000000 |
| 2 | 2423927 | 6400VV | 555.000000 |
+----+---------+---------+------------+
(3 rows)
update ithelp190822
set mg201 = 666
where mg002 = '#6300VV';
ERROR: P0001: 不可修改#開頭的項目
select * from ithelp190822;
+----+---------+---------+------------+
| id | mg001 | mg002 | mg201 |
+----+---------+---------+------------+
| 3 | 2423927 | #6300VV | 333.000000 |
| 1 | 2423118 | 6400VV | 555.000000 |
| 2 | 2423927 | 6400VV | 555.000000 |
+----+---------+---------+------------+
MS SQL 可以這樣寫,參考看看
IF EXISTS
(
SELECT *
FROM inserted
WHERE LEFT(MG002 , 1) = '#'
)
BEGIN
RAISERROR('不可修改 # 開頭的項目' , 16 , 10)
END