我正在尝试创建一个触发器,在创建之前我尝试将其测试为执行内部代码。获取代码本身正在工作,但触发器无效。这是代码:
USE [PMIDB]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [dbo].[BRK_ÇEKME_KAP_INSERTED]
ON [dbo].[ATMATERIALTRANS]
FOR INSERT
AS BEGIN
DECLARE @RDATE DATE
DECLARE @MAKİNE VARCHAR(10)
DECLARE @OKUTULAN_BARKOD VARCHAR(20)
DECLARE @MIKTAR INT
DECLARE @WC_ID INT
SELECT * FROM ATMATERIALTRANS
DECLARE CUR CURSOR
FOR
SELECT TDATE, WORKCENTERS.CODE, ATMATERIALS.BARCODE, ATMATERIALS.AMOUNT, WC_ID FROM INSERTED T1
LEFT JOIN ATCOMPOSITIONS ON ATCOMPOSITIONS.ID=T1.COMPID
LEFT JOIN WORKCENTERS ON WORKCENTERS.ID=T1.WC_ID
LEFT JOIN ATMATERIALS ON ATMATERIALS.ID=ATCOMPOSITIONS.MATERIALID
WHERE T1.COMPID IS NOT NULL AND WORKCENTERS.CODE LIKE 'DD%'
OPEN CUR
FETCH NEXT FROM CUR INTO @RDATE, @MAKİNE, @OKUTULAN_BARKOD,@MIKTAR, @WC_ID
WHILE @@FETCH_STATUS=0 BEGIN
INSERT INTO BURAK..ÇEKME_KAP (TARİH,MAKİNE,OKUTULAN_BARKOD,MİKTAR, HAMMADDE_GCKOD)
VALUES
(@RDATE,
@MAKİNE,
@OKUTULAN_BARKOD,
@MIKTAR, 'G')
UPDATE BURAK..MALKABUL_BARKOD SET BURAK..MALKABUL_BARKOD.KULLANILDI =1 WHERE BURAK..MALKABUL_BARKOD.BARKOD=@OKUTULAN_BARKOD
FETCH NEXT FROM CUR INTO @RDATE, @MAKİNE, @OKUTULAN_BARKOD,@MIKTAR, @WC_ID
END
CLOSE CUR
DEALLOCATE CUR
END
答案 0 :(得分:0)
在编写触发器之前,请参考以下语句,以通过触发器将结果返回给应用程序。
"当触发器触发时,结果将返回给调用应用程序,就像存储过程一样。要防止因触发器触发而将结果返回给应用程序,请不要包含返回结果的SELECT语句或在触发器中执行变量赋值的语句。包含将结果返回给用户的SELECT语句或执行变量赋值的语句的触发器需要特殊处理;这些返回的结果必须写入允许修改触发器表的每个应用程序中。如果必须在触发器中进行变量赋值,请在触发器的开头使用SET NOCOUNT语句,以防止返回任何结果集。"