在表上更新后设置2个列

时间:2020-11-05 16:16:27

标签: mysql sql database triggers sql-insert

因此,我尝试在插入后从表中输入大写字母2字段。因此,我创建了一个触发器,但是由于我想更新2个字段,因此我不确定如何准确地执行此操作。这是我所做的,但我认为这不是正确的方法。我想在插入时将这两个字段设置为大写字母。

谢谢:)

CREATE or ALTER TRIGGER majVilleClient
on client
instead of insert
as
begin
update client
set nom_client =upper(nom_client)
where nom_client in (select nom_client from inserted)
set nom_ville = upper(nom_ville)
where nom_ville in (select nom_ville from inserted) 
end```

3 个答案:

答案 0 :(得分:1)

在标记了问题的MySQL中,您将使用BEFORE INSERT触发器,因此可以在写入值之前对其进行更改:

delimiter //

create trigger majvilleclient
before update on client
for each row
begin
    set new.nom_client = upper(new.nom_client);
    set new.nom_ville  = upper(new.nom_ville);
end;
//

delimiter ;

答案 1 :(得分:1)

我对您的rdms不太熟悉,但是请确保它不是MySQL,您应该更改它

但是当您的第一次更新有效时,yu可以使用以下内容。

CREATE or ALTER TRIGGER majVilleClient
on client
instead of insert
as
begin
update client
set nom_client =upper(nom_client)
,nom_ville = upper(nom_ville)
where nom_client in (select nom_client from inserted)

结束

答案 2 :(得分:0)

这就是我所做的更新,但不确定是否可行。

CREATE or ALTER TRIGGER majVilleClient
on client
after insert
as
begin
update client
set nom_client =upper(nom_client),
    nom_ville = upper(nom_ville)
where nom_ville in (select nom_ville from inserted) and nom_client in (select nom_client from inserted) 
end```