我从存储过程中得到一个错误,我仔细检查了一下,发现该查询中发生了错误。
INSERT INTO Custodian (ID, EditVersion, DisplayValue, LookupKey, ImportTrackingID)
SELECT
NEWID(), 1, ImportActivity.Custodian, ImportActivity.Custodian,
'2eca1eba-68e0-4490-9e93-aa8f00eff76e'
FROM
ImportActivity
WHERE
ImportActivity.BatchKey = '00317eb6-96fc-48c9-b530-e9fed3aea2ce'
AND dbo.IsValidLookup(NULL, ImportActivity.Custodian, ImportActivity.Custodian) = 1
AND NOT EXISTS (SELECT ID FROM Custodian WHERE Custodian.LookupKey = ImportActivity.Custodian)
GROUP BY
ImportActivity.Custodian, ImportActivity.Custodian
在某些情况下,此SELECT
查询返回空结果,然后发生错误:
SELECT
NEWID(), 1, ImportActivity.Custodian, ImportActivity.Custodian,
'2eca1eba-68e0-4490-9e93-aa8f00eff76e'
FROM
ImportActivity
WHERE
ImportActivity.BatchKey = '00317eb6-96fc-48c9-b530-e9fed3aea2ce'
AND dbo.IsValidLookup(NULL, ImportActivity.Custodian, ImportActivity.Custodian) = 1
AND NOT EXISTS (SELECT ID FROM Custodian WHERE Custodian.LookupKey = ImportActivity.Custodian)
GROUP BY
ImportActivity.Custodian, ImportActivity.Custodian
我想仅在存在INSERT
结果的情况下运行SELECT
查询。
这可以解决问题。
我该如何解决?
这是错误:
tr_Custodian_Insert E 1
tr_Custodian_Update E 2Msg 3930,第16级,状态1,过程tr_Custodian_Insert,第36行[批处理开始第2行]
当前事务无法提交,并且不能支持写入日志文件的操作。回滚交易。
它发生在下面的Custodian
表INSERT触发器中。
USE [LMAdvImportsTesting]
GO
/****** Object: Trigger [dbo].[tr_Custodian_Insert] Script Date: 7/24/2019 8:52:32 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [dbo].[tr_Custodian_Insert] ON [dbo].[Custodian]
FOR INSERT
AS
BEGIN
IF ( UPDATE(LookupKey) OR UPDATE(BalanceType) )
BEGIN
BEGIN TRY
-- Trigger setup
DECLARE @Start datetime
SELECT @Start = GETDATE()
DECLARE @RoundAmount decimal(8,4)
SELECT @RoundAmount = .02
PRINT SPACE(5*@@NestLevel) + 'tr_Custodian_Insert E ' + RTRIM(CAST(@@NestLevel AS varchar(50)))
SET NOCOUNT ON
-- Combined balances
UPDATE Custodian
SET Custodian.BalanceType = 1
FROM Custodian
JOIN inserted ON Custodian.ID = inserted.ID
WHERE Custodian.LookupKey IN ('TDAMERITRADE','THINKORSWIM','WELLSFARGO')
AND Custodian.BalanceType <> 1
-- Trigger cleanup
PRINT SPACE(5*@@NestLevel) + 'tr_Custodian_Insert X ' + RTRIM(CAST(@@NestLevel AS varchar(50))) + ' ' + CAST(DATEDIFF(ms, @Start, GETDATE()) as varchar(200)) + 'ms'
END TRY
BEGIN CATCH
DECLARE @error int, @line int, @message varchar(4000), @xstate int, @procedure varchar(128), @severity int, @state int;
SELECT @error = ERROR_NUMBER(), @line=ERROR_LINE(), @message = ERROR_MESSAGE(), @procedure = ERROR_PROCEDURE(), @severity = ERROR_SEVERITY(), @state = ERROR_STATE()
INSERT DBError (Error_Line, Source, Error_Number, Error_Message, Error_Procedure, Error_Severity, Error_State) VALUES (@line, 'tr_Custodian_Insert', @error, @message, @procedure, @severity, @state)
RAISERROR ('tr_Custodian_Insert: %d: %s', 16, 1, @error, @message);
END CATCH
END
END
;