如何解决SQL INSERT触发器的问题
伙计们长期潜伏在第一次的海报上。我正在为此触发器寻求帮助,以尝试上班。我看过类似的文章,但没有一个真的适合我的尝试。所以我向你们寻求帮助。
基本上这是我正在尝试做的事情。 我们有DMS软件,它可以写入数据库,并且要在特定插入值上触发触发器。
这是将被处理的插入语句的示例。
INSERT INTO DOCSADM.ACTIVITYLOG (CR_IN_USE,ACTIVITY_DESC,BILLED_ON,BILLABLE,PAGES,KEYSTROKES,TYPE_TIME,ELAPSED_TIME,TYPIST,AUTHOR,START_DATE,ACTIVITY_TYPE,REF_DOCUMENT,REF_LIBRARY,APPLICATION,VERSION_LABEL,DOCNUMBER,SYSTEM_ID)
VALUES ('','DOCSFusion','1753-01-01','',1920,'2020-08-26T10:17:56',**115**,-1,1173,75,3252)
但是我只希望在插入语句中的粗体部分看到值115时触发触发器。 (Activity_type值)
对于不是115的所有其他值,我什么也不想做。
这是我到目前为止所拥有的:
CREATE TRIGGER BW_TRIGGER
ON DOCSADM.ACTIVITYLOG
AFTER INSERT
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
--Declare some variable and set it as a value of 115.
--Example:
DECLARE @AlogType int = (SELECT I.ACTIVITY_TYPE FROM DOCSADM.ACTIVITYLOG A,INSERTED I) --This is the value you are looking for regarding the DM client/Matter actitivty type.
DECLARE @AlogDesc varchar(32) = (Select i.ACTIVITY_DESC from docsadm.ACTIVITYLOG A,INSERTED I)
--Next,you should have a fork or path in your trigger to determine how it proceeds.
--Path 1: The @AlogType value matches the inserted value so you want to process the rest of the trigger. Example path – “ProcessTrigger:”
--Path 2: The @AlogType value does NOT match the inserted value,you want to exit the trigger. Example Path – “ExitTrigger:”
IF @AlogType <> 115
GOTO TriggerExit;
ELSE
Begin
/*Create first temp table to collect insert values*/ --This table will have the SysID Value and the corresponding docnumber for the items you want.
--You can add whatever other values you think you need.
CREATE TABLE #TempSet1
(
AlogsysID INT,Docnum INT,AlogDate Varchar(64),AlogTypist INT,AlogAuthor INT,AlogDesc varchar(32),ALOGVER varchar(10),ALOG_MATTER INT
)
INSERT INTO #TempSet1 (AlogsysID,Docnum,AlogDate,AlogTypist,AlogAuthor,ALOG_MATTER)
--SELECT You SELECT STATEMENT WILL GO HERE MODIFIED TO POPULATE THE TABLE WITH THE DOCNUMBERS YOU WANT!!
select top 1 System_id,docnumber,LAST_ACCESS_DATE,MATTER from docsadm.PROFILE where EXISTS (SELECT CLIENT.SYSTEM_ID FROM DOCSADM.CLIENT INNER JOIN DOCSADM.MATTER ON MATTER.CLIENT_ID = CLIENT.SYSTEM_ID
WHERE MATTER.SYSTEM_ID =@AlogDesc OR INH_LUP_SEC_FROM IS NULL OR INH_LUP_SEC_FROM = 0) AND MATTER=@AlogDesc
/*Set variable @SysID as the LASTKEY value -1. This will be used to set the SysID column on the #TempSet table*/
--DECLARE @SysID INT = (SELECT LASTKEY FROM DOCSADM.SEQ_SYSTEMKEY) -1;
/*Set the SysID value for every row on the #TempSet1 table as the @SysID variable +1*/
--UPDATE #TempSet1
--SET @SysID = AlogsysID = @SysID + 1
--Your #TempSet should now be set with ALL of the System_IDs and Docnumbers necessary for your insert!!!!—
--Verify this by doing a select against the #TempSet1 Table
SELECT * FROM #TempSet1;
--Next you need to set the SystemID to the correct value for future processing. To do this,we need to get a total count from the #TempSet table.
/*Set a variable to update the NEXTKEY value on the DOCSADM.SEQ_SYSTEMKEY table. The NEXTKEY value is used for the SYSTEM_ID field*/
--DECLARE @SeqUpdateCount INT = (SELECT COUNT(*) FROM #TempSet1);
/*Update the LASTKEY Value on the SEQ_SYSTEMKEY table to the next available value for DM.*/
--UPDATE DOCSADM.SEQ_SYSTEMKEY SET LASTKEY = LASTKEY+@SeqUpdateCount
--If you have all the values you need in your temp table,you can now insert them into the ACTIVITYLOG table.
--INSERT INTO DOCSADM.ACTIVITY
--(SYSTEM_ID,version,EXT,)
--SELECT
--AlogSysID,GETUTCDATE(),BLAH,BLAH
--FROM #TableSet1
INSERT INTO DOCSADM.ACTIVITYLOG
(SYSTEM_ID,ACTIVITY_TYPE)
SELECT
AlogsysID,ALOG_MATTER,115
FROM #TempSet1;
--Now you need to Drop the Temp Table
DROP TABLE #TempSet1
--Go to the other half of your path above to exit the trigger.
END
TriggerExit:
END
Go
但是当我尝试在此表上运行任何insert语句时,会收到此错误消息。 activity_type的值是否为115都没有关系
Subquery returned more than 1 value. This is not permitted when the subquery follows =,!=,<,<=,>,>= or when the subquery is used as an expression.
我知道问题出在触发器的这一部分:
INSERT INTO #TempSet1 (AlogsysID,ALOG_MATTER)
--SELECT You SELECT STATEMENT WILL GO HERE MODIFIED TO POPULATE THE TABLE WITH THE DOCNUMBERS YOU WANT!!
SELECT TOP 1
System_id,MATTER
FROM docsadm.PROFILE
WHERE EXISTS (SELECT CLIENT.SYSTEM_ID
FROM DOCSADM.CLIENT
INNER JOIN DOCSADM.MATTER
ON MATTER.CLIENT_ID = CLIENT.SYSTEM_ID
WHERE MATTER.SYSTEM_ID =@AlogDesc
OR INH_LUP_SEC_FROM IS NULL
OR INH_LUP_SEC_FROM = 0)
AND MATTER=@AlogDesc
执行导致失败的选择语句。 我知道该语句将带回多行,但我只需要其中之一的值,这样我就可以将此值用于插入。我虽然拥有“ select top 1”将为我做到这一点,但它并不像我认为的那样起作用。我想念什么?任何帮助将不胜感激。
解决方法
如果我不得不猜测我会说你的问题在这里:
DECLARE @AlogType int = (SELECT I.ACTIVITY_TYPE FROM DOCSADM.ACTIVITYLOG A,INSERTED I) --This is the value you are looking for regarding the DM client/Matter actitivty type.
DECLARE @AlogDesc varchar(32) = (Select i.ACTIVITY_DESC from docsadm.ACTIVITYLOG A,INSERTED I)
上面的ACTIVITYLOG
和INSERTED
如何加入?没有CROSS JOIN
的位置。为什么甚至将ACTIVITYLOG
拖到其中,也可以简单地使用INSERTED
。另外,请尝试停止使用隐式联接(我可以看到在脚本的后面,您使用了正确的,更详细的联接语法)
尝试:
DECLARE @AlogType int = (SELECT I.ACTIVITY_TYPE FROM INSERTED I) --This is the value you are looking for regarding the DM client/Matter actitivty type.
DECLARE @AlogDesc varchar(32) = (Select i.ACTIVITY_DESC from INSERTED I)
请注意,这仅适用于单个刀片。批量插入时,INSERTED
是一个包含多行的表,您将再次遇到问题。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。