我有两个由一个功能(GLPOST
和GLPOSTO
)更新的表。
我已经基于这两个表创建了一个视图(GLREP
),该视图将GLPOSTO
的行转置为列,因为这就是我想要的数据。
现在,我要在该视图上创建触发器(OPTIONAL
),一旦该视图以视图格式从各个表中收集数据,就将其插入到新表(GLREPORTEXCEL
)中。
查看GLREP
:
CREATE VIEW [dbo].[GLREP]
AS
(SELECT *
FROM
(SELECT
GLPOST.ACCTID,
JRNLDATE,
GLPOST.FISCALYR,
GLPOST.FISCALPERD,
GLPOST.SRCECURN,
GLPOST.BATCHNBR,
GLPOST.ENTRYNBR,
GLPOST.JNLDTLDESC,
GLPOST.JNLDTLREF,
GLPOST.TRANSAMT,
GLPOST.CONVRATE,
GLPOST.SCURNAMT,
GLPOSTO.OPTFIELD,
GLPOST.CNTDETAIL,
CSOPTFD.VDESC
FROM
GLPOST
LEFT JOIN
GLPOSTO ON GLPOST.ACCTID = GLPOSTO.ACCTID
AND GLPOST.POSTINGSEQ = GLPOSTO.POSTINGSEQ
AND glpost.CNTDETAIL = glposto.CNTDETAIL
LEFT JOIN
CSOPTFD ON GLPOSTO.OPTFIELD = CSOPTFD.OPTFIELD
AND GLPOSTO.VALUE = CSOPTFD.VALUE) AS source
PIVOT
(MAX([VDESC])
FOR [OPTFIELD] IN (ADVANCE, MEDICAL, MILEAGE, MOTORVEHICLE, PROMOTION, STAFF)) AS PVT
)
我的触发器不起作用:
CREATE TRIGGER OPTIONAL
ON GLREP
FOR INSERT
AS
INSERT INTO GLREPORTEXCEL (ACCTID, TRANDATE, FISCALYR, FISCALPERD, SRCECURN, BATCHNBR,
ENTRYNBR, JNLDTLDESC, JNLDTLREF, TRANSAMT, CONVRATE, SCURNAMT,
CNTDETAIL, STAFF, ADVANCE, MEDICAL, MILEAGE, MOTORVEHICLE, PROMOTION)
SELECT
ACCTID, JRNLDATE, FISCALYR, FISCALPERD, SRCECURN, BATCHNBR,
ENTRYNBR, JNLDTLDESC, JNLDTLREF, TRANSAMT, CONVRATE, SCURNAMT,
CNTDETAIL, STAFF, ADVANCE, MEDICAL, MILEAGE, MOTORVEHICLE, PROMOTION
FROM
inserted
我要插入的新表称为GLREPORTEXCEL
。
答案 0 :(得分:0)
正如我暗示并在注释中特别指出的那样,SQL Server不支持视图上的for
触发器-仅支持instead of
触发器。
这在CREATE TRIGGER (Transact-SQL)页的“参数”段落中有记录:
表|查看
在其上运行DML触发器的表或视图。该表或视图有时称为触发器表或触发器视图。指定表或视图的标准名称是可选的。 您只能通过INSTEAD OF触发器引用视图。您不能在本地或全局临时表上定义DML触发器。
(重点是我的)。
这意味着您需要将触发器从for
触发器更改为instead of
触发器:
CREATE TRIGGER OPTIONAL
ON GLREP
INSTEAD OF INSERT
AS
INSERT INTO GLREPORTEXCEL (ACCTID, TRANDATE, FISCALYR, FISCALPERD, SRCECURN, BATCHNBR,
ENTRYNBR, JNLDTLDESC, JNLDTLREF, TRANSAMT, CONVRATE, SCURNAMT,
CNTDETAIL, STAFF, ADVANCE, MEDICAL, MILEAGE, MOTORVEHICLE, PROMOTION)
SELECT
ACCTID, JRNLDATE, FISCALYR, FISCALPERD, SRCECURN, BATCHNBR,
ENTRYNBR, JNLDTLDESC, JNLDTLREF, TRANSAMT, CONVRATE, SCURNAMT,
CNTDETAIL, STAFF, ADVANCE, MEDICAL, MILEAGE, MOTORVEHICLE, PROMOTION
FROM
inserted