如果我创建一个之前的插入/更新触发器,我知道如何做这样的事情。
CREATE TRIGGER upd_check BEFORE INSERT ON def_servpath_0001_weatherstation
FOR EACH ROW
BEGIN
DECLARE someString text;
FOR EACH ROW
BEGIN
IF NEW.atrType = 'datetime' THEN
SET someString := NEW.atrValue;
END IF;
END;
FOR EACH ROW
BEGIN
IF NEW.atrType <> 'datetime' THEN
SET NEW.atrMD = someString;
END IF;
END;
END;//
我的问题是,我可以做一些简单的事情:
DECLARE someString varchar(100);
SET someString = (SELECT NEW.atrValue FROM **NEW** WHERE NEW.atrType = 'datetime');
UPDATE **NEW**
SET NEW.atrValue = someString;
修改
最好分成两部分。
代码示例
CREATE TABLE [test] ( atrType nvarchar(100), atrValue nvarchar(100), atrMD nvarchar(100));
INSERT INTO [test] VALUES
('datetime', '20150915', ''),
('name', 'Juan', ''),
('city', 'Caracas', '');
** Trigger**
SELECT *
FROM [test]
atrType atrValue atrMD
datetime 20150915 20150915
name Juan 20150915
city Caracas 20150915