SQL INSERT触发器的问题

如何解决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) 

上面的ACTIVITYLOGINSERTED如何加入?没有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 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-