启用 SQL 触发器时,Java 中的 Prepared Statement 失败

Posted

技术标签:

【中文标题】启用 SQL 触发器时,Java 中的 Prepared Statement 失败【英文标题】:Prepared Statement in Java fails when SQL trigger is enabled 【发布时间】:2017-05-01 05:51:51 【问题描述】:

我已经构建了一个 Eclipse Java 插件,它具有将选定的 XML 文件复制到服务器的功能,然后运行一个 SQL 准备语句,该语句填充一个名为 XMLInsertion 的表,该表反过来激活一个 SQL 触发器,该触发器从XML 文件和更新 XMLInsertion 和另一个表 WeedingReports。几天前我打开它之前效果很好,所以在此期间发生了一些事情。

如果禁用 SQL 触发器,则准备好的语句将起作用。如果我启用触发器都不起作用,则会出现错误“错误!无法批量加载。文件“blah”不存在。”如果我通过 SQL Server 输入数据,SQL 触发器就会工作。这表明存在权限问题,但是如果触发器或插入都不能通过插件工作,这怎么可能,因为错误表明触发器找不到文件,但触发器需要从 XMLInsertion 中读取,而这不是填充以激活触发器 - 令人困惑?

两个问题

如果准备好的语句或触发器都没有运行,错误是如何发生的?该错误与触发器内的 BULK LOAD 相关,但触发器依赖于从准备好的语句输入的数据,该语句从不执行。

为什么只有在启用触发器时准备好的语句才完全不运行?

Connection connection = DriverManager.getConnection("jdbc:sqlserver://gis-s-rv;databaseName=bushland_dev;user=blah;password=blah;");     

PreparedStatement insertXMLTable = connection.prepareStatement("UPDATE Bushland_Dev.dbo.XMLINSERTION SET FILEPATH = ?, FILENAME = ?, TRIGGERFIELD = ? WHERE FILENAME = ?");

insertXMLTable.setString(1,destination.toString());
insertXMLTable.setString(2,destination.getFileName().toString());
insertXMLTable.setString(3,GMSCKey);
insertXMLTable.setString(4,destination.toString());
insertXMLTable.executeUpdate();

直到几天前它还运行良好,所以我想有人更改了权限,因为我自己没有进行任何更改。

DDLS 下面

XML插入触发器

USE [bushland_dev]
    GO

SET ANSI_NULLS ON
    GO

SET QUOTED_IDENTIFIER ON
    GO

CREATE TRIGGER [dbo].[XML_Permission] ON [bushland_dev].[dbo].[XMLINSERTION] 
AFTER UPDATE, INSERT
AS

IF (SELECT COUNT(TRIGGERFIELD) FROM XMLINSERTION WHERE TRIGGERFIELD < 0) IS 
NOT NULL

BEGIN

CREATE TABLE #Temp
(
ID INT IDENTITY PRIMARY KEY,
XMLData XML,
LoadedDateTime DATETIME
)

DECLARE @Filepath AS NVARCHAR(MAX)
DECLARE @Filepath2 AS NVARCHAR(MAX)

SET @Filepath = '\\GMSRVDEV\XML\'+(SELECT [FILENAME] FROM bushland_dev.dbo.XMLINSERTION WHERE TRIGGERFIELD > 0)+''

DECLARE @SQL nvarchar(max)
SET @SQL = 'SELECT CONVERT(XML, BulkColumn) AS BulkColumn, GETDATE() FROM OPENROWSET(BULK '''+@Filepath+''', SINGLE_BLOB) AS x' 

Insert into #Temp (XMLData,LoadedDateTime)

    exec(@SQL)

    DECLARE @XML AS XML, @hDoc AS INT

    SELECT @XML = XMLData FROM #Temp

    EXEC sp_xml_preparedocument @hDoc OUTPUT, @XML      

    DECLARE @FormFields TABLE (ID int IDENTITY(1,1) primary key, TabNum int, fieldName varchar(50), value nvarchar(max));

    WITH NumberMethods AS (SELECT ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS [ROW], 'Method' AS fieldname, CAST(ex.value('Method[1]','int') AS NVARCHAR(MAX)) AS Method FROM @xml.nodes('/dataroot/MPSExport') AS A(ex)),
    NumberHours AS (SELECT ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS [ROW], 'Hours' AS fieldname, CAST(ex.value('Hours[1]','float') AS NVARCHAR(MAX)) AS [Hours] FROM @xml.nodes('/dataroot/MPSExport') AS A(ex)),
    NumberComments AS (SELECT ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS [ROW], 'Comments' AS fieldname, ex.value('Comments[1]','nvarchar(max)') AS Comments FROM @xml.nodes('/dataroot/MPSExport') AS A(ex)),
    NumberFollowUpNotes AS (SELECT ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS [ROW], 'FollowUpNotes' AS fieldname, ex.value('FollowUpNotes[1]','nvarchar(max)') AS FollowUpNotes FROM @xml.nodes('/dataroot/MPSExport') AS A(ex)),
    NumberHerbicide_1 AS (SELECT ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS [ROW], 'Herbicide_1' AS fieldname, CAST(ex.value('Herbicide_1[1]','int') AS NVARCHAR(MAX)) AS Herbicide_1 FROM @xml.nodes('/dataroot/MPSExport') AS A(ex)),
    NumberHerbicide_Rate_1 AS (SELECT ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS [ROW], 'Herbicide_Rate_1' AS fieldname, CAST(ex.value('Herbicide_Rate_1[1]','float') AS NVARCHAR(MAX)) AS Herbicide_Rate_1 FROM @xml.nodes('/dataroot/MPSExport') AS A(ex)),
    NumberHerbicide_Qty_1 AS (SELECT ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS [ROW], 'Herbicide_Qty_1' AS fieldname, CAST(ex.value('Herbicide_Qty_1[1]','float') AS NVARCHAR(MAX)) AS Herbicide_Qty_1 FROM @xml.nodes('/dataroot/MPSExport') AS A(ex)),
    NumberHerbicide_2 AS (SELECT ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS [ROW], 'Herbicide_2' AS fieldname, CAST(ex.value('Herbicide_2[1]','int') AS NVARCHAR(MAX)) AS Herbicide_2 FROM @xml.nodes('/dataroot/MPSExport') AS A(ex)),
    NumberHerbicide_Rate_2 AS (SELECT ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS [ROW], 'Herbicide_Rate_2' AS fieldname, CAST(ex.value('Herbicide_Rate_2[1]','float') AS NVARCHAR(MAX)) AS Herbicide_Rate_2 FROM @xml.nodes('/dataroot/MPSExport') AS A(ex)),
    NumberHerbicide_Qty_2 AS (SELECT ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS [ROW], 'Herbicide_Qty_2' AS fieldname, CAST(ex.value('Herbicide_Qty_2[1]','float') AS NVARCHAR(MAX)) AS Herbicide_Qty_2 FROM @xml.nodes('/dataroot/MPSExport') AS A(ex)),
    NumberHerbicide_IsSurfactant_2 AS (SELECT ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS [ROW], 'Herbicide_IsSurfactant_2' AS fieldname, CAST(ex.value('Herbicide_IsSurfactant_2[1]','int') AS NVARCHAR(MAX)) AS Herbicide_IsSurfactant_2 FROM @xml.nodes('/dataroot/MPSExport') AS A(ex)),
    NumberWeed_1 AS (SELECT ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS [ROW], 'Weed_1' AS fieldname, ex.value('Weed_1[1]','nvarchar(max)') AS Weed_1 FROM @xml.nodes('/dataroot/MPSExport') AS A(ex)),
    NumberWeed_2 AS (SELECT ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS [ROW], 'Weed_2' AS fieldname, ex.value('Weed_2[1]','nvarchar(max)') AS Weed_2 FROM @xml.nodes('/dataroot/MPSExport') AS A(ex)),
    NumberWeed_3 AS (SELECT ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS [ROW], 'Weed_3' AS fieldname, ex.value('Weed_3[1]','nvarchar(max)') AS Weed_3 FROM @xml.nodes('/dataroot/MPSExport') AS A(ex)),
    NumberWeed_4 AS (SELECT ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS [ROW], 'Weed_4' AS fieldname, ex.value('Weed_4[1]','nvarchar(max)') AS Weed_4 FROM @xml.nodes('/dataroot/MPSExport') AS A(ex)),
    NumberTaskID AS (SELECT ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS [ROW], 'ActivityID' AS fieldname, ex.value('ActivityID[1]','nvarchar(max)') AS ActivityID FROM @xml.nodes('/dataroot/MPSExport') AS A(ex))

    INSERT INTO @formfields (TabNum, fieldname, value)
    SELECT * FROM NumberMethods WHERE Method IS NOT NULL
        UNION all
    SELECT * FROM NumberHours WHERE [Hours] IS NOT NULL
        UNION all
    SELECT * FROM NumberComments WHERE Comments IS NOT NULL
        UNION all
    SELECT * FROM NumberFollowUpNotes WHERE FollowUpNotes IS NOT NULL
        UNION all
    SELECT * FROM NumberHerbicide_1 WHERE Herbicide_1 IS NOT NULL
        UNION all
    SELECT * FROM NumberHerbicide_Rate_1 WHERE Herbicide_Rate_1 IS NOT NULL
        UNION all
    SELECT * FROM NumberHerbicide_Qty_1 WHERE Herbicide_Qty_1 IS NOT NULL
        UNION all
    SELECT * FROM NumberHerbicide_2 WHERE Herbicide_2 IS NOT NULL
        UNION all
    SELECT * FROM NumberHerbicide_Rate_2 WHERE Herbicide_Rate_2 IS NOT NULL
        UNION all
    SELECT * FROM NumberHerbicide_Qty_2 WHERE Herbicide_Qty_2 IS NOT NULL
        UNION all
    SELECT * FROM NumberHerbicide_IsSurfactant_2 WHERE Herbicide_IsSurfactant_2 IS NOT NULL
        UNION all
    SELECT * FROM NumberWeed_1 WHERE Weed_1 IS NOT NULL
        UNION all
    SELECT * FROM NumberWeed_2 WHERE Weed_2 IS NOT NULL
        UNION all
    SELECT * FROM NumberWeed_3 WHERE Weed_3 IS NOT NULL
        UNION all
    SELECT * FROM NumberWeed_4 WHERE Weed_4 IS NOT NULL
        UNION all
    SELECT * FROM NumberTaskID WHERE ActivityID IS NOT NULL OR ActivityID <> 0

    SELECT * FROM @FormFields

    UPDATE Bushland_dev.dbo.XMLINSERTION
    SET XMLINSERTION.[M1_METHOD] = (SELECT VALUE FROM @FormFields WHERE fieldName= 'Method' AND TabNum = 2),
    XMLINSERTION.[HOURS] = (SELECT VALUE FROM @FormFields WHERE fieldName = 'Hours' AND TabNum = 1),
    XMLINSERTION.[M1_HERBICIDE1] = (SELECT VALUE FROM @FormFields WHERE fieldName = 'Herbicide_1' AND TabNum = 2), 
    XMLINSERTION.[M1_HERBICIDE2] = (SELECT VALUE FROM @FormFields WHERE fieldName = 'Herbicide_2' AND TabNum = 2), 
    XMLINSERTION.[M1_HERBICIDE1RATE] = (SELECT VALUE FROM @FormFields WHERE fieldName = 'Herbicide_Rate_1' AND TabNum = 2),
    XMLINSERTION.[M1_HERBICIDE1ADDRATE] = (SELECT VALUE FROM @FormFields WHERE fieldName = 'Herbicide_Rate_2' AND TabNum = 2),      
    XMLINSERTION.[M1_HERBICIDE1QTY] = (SELECT VALUE FROM @FormFields WHERE fieldName = 'Herbicide_Qty_1' AND TabNum = 2),       
    XMLINSERTION.[M1_HERBICIDE1SURFECTANT] = (SELECT VALUE FROM @FormFields WHERE fieldName = 'Herbicide_IsSurfactant_2' AND TabNum = 2), 
    XMLINSERTION.[M1_WEED1] = (SELECT ID FROM TARGET_WEEDS_LOOKUP WHERE NLinks_Ref IN (SELECT VALUE FROM @FormFields WHERE fieldName = 'Weed_1' AND TabNum = 2)), 
    XMLINSERTION.[M1_WEED2] = (SELECT ID FROM TARGET_WEEDS_LOOKUP WHERE NLinks_Ref IN (SELECT VALUE FROM @FormFields WHERE fieldName = 'Weed_2' AND TabNum = 2)), 
    XMLINSERTION.[M1_WEED3] = (SELECT ID FROM TARGET_WEEDS_LOOKUP WHERE NLinks_Ref IN (SELECT VALUE FROM @FormFields WHERE fieldName = 'Weed_3' AND TabNum = 2)), 
    XMLINSERTION.[M1_WEED4] = (SELECT ID FROM TARGET_WEEDS_LOOKUP WHERE NLinks_Ref IN (SELECT VALUE FROM @FormFields WHERE fieldName = 'Weed_4' AND TabNum = 2)),
    XMLINSERTION.[M1_WEEDCOMMENTS] = (SELECT VALUE FROM @FormFields WHERE fieldName = 'Comments' AND TabNum = 2),
    XMLINSERTION.[Priority_Tasks] = (SELECT VALUE FROM @FormFields WHERE fieldName = 'FollowUpNotes' AND TabNum = 2),
    XMLINSERTION.[ActivityID1] = (SELECT VALUE FROM @FormFields WHERE fieldName = 'ActivityID' AND TabNum = 2),

    XMLINSERTION.[M2_METHOD] = (SELECT VALUE FROM @FormFields WHERE fieldName= 'Method' AND TabNum = 3),        
    XMLINSERTION.[M2_HERBICIDE1] = (SELECT VALUE FROM @FormFields WHERE fieldName = 'Herbicide_1' AND TabNum = 3), 
    XMLINSERTION.[M2_HERBICIDE2] = (SELECT VALUE FROM @FormFields WHERE fieldName = 'Herbicide_2' AND TabNum = 3),  
    XMLINSERTION.[M2_HERBICIDE2RATE] = (SELECT VALUE FROM @FormFields WHERE fieldName = 'Herbicide_Rate_1' AND TabNum = 3),
    XMLINSERTION.[M2_HERBICIDE2ADDRATE] = (SELECT VALUE FROM @FormFields WHERE fieldName = 'Herbicide_Rate_2' AND TabNum = 3),
    XMLINSERTION.[M2_HERBICIDE2QTY] = (SELECT VALUE FROM @FormFields WHERE fieldName = 'Herbicide_Qty_2' AND TabNum = 3), 
    XMLINSERTION.[M2_HERBICIDE1SURFECTANT] = (SELECT VALUE FROM @FormFields WHERE fieldName = 'Herbicide_IsSurfactant_2' AND TabNum = 3), 
    XMLINSERTION.[M2_WEED1] = (SELECT ID FROM TARGET_WEEDS_LOOKUP WHERE NLinks_Ref IN (SELECT VALUE FROM @FormFields WHERE fieldName = 'Weed_1' AND TabNum = 3)),
    XMLINSERTION.[M2_WEED2] = (SELECT ID FROM TARGET_WEEDS_LOOKUP WHERE NLinks_Ref IN (SELECT VALUE FROM @FormFields WHERE fieldName = 'Weed_2' AND TabNum = 3)),
    XMLINSERTION.[M2_WEED3] = (SELECT ID FROM TARGET_WEEDS_LOOKUP WHERE NLinks_Ref IN (SELECT VALUE FROM @FormFields WHERE fieldName = 'Weed_3' AND TabNum = 3)), 
    XMLINSERTION.[M2_WEED4] = (SELECT ID FROM TARGET_WEEDS_LOOKUP WHERE NLinks_Ref IN (SELECT VALUE FROM @FormFields WHERE fieldName = 'Weed_4' AND TabNum = 3)),
    XMLINSERTION.[M2_WEEDCOMMENTS] = (SELECT VALUE FROM @FormFields WHERE fieldName = 'Comments' AND TabNum = 3),
    XMLINSERTION.[ActivityID2] = (SELECT VALUE FROM @FormFields WHERE fieldName = 'ActivityID' AND TabNum = 3),

    XMLINSERTION.[M3_METHOD] = (SELECT VALUE FROM @FormFields WHERE fieldName= 'Method' AND TabNum = 4),        
    XMLINSERTION.[M3_HERBICIDE1] = (SELECT VALUE FROM @FormFields WHERE fieldName = 'Herbicide_1' AND TabNum = 4), 
    XMLINSERTION.[M3_HERBICIDE2] = (SELECT VALUE FROM @FormFields WHERE fieldName = 'Herbicide_2' AND TabNum = 4), 
    XMLINSERTION.[M3_HERBICIDE3RATE] = (SELECT VALUE FROM @FormFields WHERE fieldName = 'Herbicide_Rate_1' AND TabNum = 4),
    XMLINSERTION.[M3_HERBICIDE3ADDRATE] = (SELECT VALUE FROM @FormFields WHERE fieldName = 'Herbicide_Rate_2' AND TabNum = 4),
    XMLINSERTION.[M3_HERBICIDE3QTY] = (SELECT VALUE FROM @FormFields WHERE fieldName = 'Herbicide_Qty_3' AND TabNum = 4), 
    XMLINSERTION.[M3_HERBICIDE1SURFECTANT] = (SELECT VALUE FROM @FormFields WHERE fieldName = 'Herbicide_IsSurfactant_2' AND TabNum = 4), 
    XMLINSERTION.[M3_WEED1] = (SELECT ID FROM TARGET_WEEDS_LOOKUP WHERE NLinks_Ref IN (SELECT VALUE FROM @FormFields WHERE fieldName = 'Weed_1' AND TabNum = 4)), 
    XMLINSERTION.[M3_WEED2] = (SELECT ID FROM TARGET_WEEDS_LOOKUP WHERE NLinks_Ref IN (SELECT VALUE FROM @FormFields WHERE fieldName = 'Weed_2' AND TabNum = 4)),  
    XMLINSERTION.[M3_WEED3] = (SELECT ID FROM TARGET_WEEDS_LOOKUP WHERE NLinks_Ref IN (SELECT VALUE FROM @FormFields WHERE fieldName = 'Weed_3' AND TabNum = 4)),  
    XMLINSERTION.[M3_WEED4] = (SELECT ID FROM TARGET_WEEDS_LOOKUP WHERE NLinks_Ref IN (SELECT VALUE FROM @FormFields WHERE fieldName = 'Weed_4' AND TabNum = 4)), 
    XMLINSERTION.[M3_WEEDCOMMENTS] = (SELECT VALUE FROM @FormFields WHERE fieldName = 'Comments' AND TabNum = 4),
    XMLINSERTION.[ActivityID3] = (SELECT VALUE FROM @FormFields WHERE fieldName = 'ActivityID' AND TabNum = 4),

    XMLINSERTION.[M4_METHOD] = (SELECT VALUE FROM @FormFields WHERE fieldName= 'Method' AND TabNum = 5),        
    XMLINSERTION.[M4_HERBICIDE1] = (SELECT VALUE FROM @FormFields WHERE fieldName = 'Herbicide_1' AND TabNum = 5), 
    XMLINSERTION.[M4_HERBICIDE2] = (SELECT VALUE FROM @FormFields WHERE fieldName = 'Herbicide_2' AND TabNum = 5), 
    XMLINSERTION.[M4_HERBICIDE4RATE] = (SELECT VALUE FROM @FormFields WHERE fieldName = 'Herbicide_Rate_1' AND TabNum = 5),
    XMLINSERTION.[M4_HERBICIDE4ADDRATE] = (SELECT VALUE FROM @FormFields WHERE fieldName = 'Herbicide_Rate_2' AND TabNum = 5), 
    XMLINSERTION.[M4_HERBICIDE4QTY] = (SELECT VALUE FROM @FormFields WHERE fieldName = 'Herbicide_Qty_4' AND TabNum = 5), 

    XMLINSERTION.[M4_HERBICIDE1SURFECTANT] = (SELECT VALUE FROM @FormFields WHERE fieldName = 'Herbicide_IsSurfactant_2' AND TabNum = 5), 
    XMLINSERTION.[M4_WEED1] = (SELECT ID FROM TARGET_WEEDS_LOOKUP WHERE NLinks_Ref IN (SELECT VALUE FROM @FormFields WHERE fieldName = 'Weed_1' AND TabNum = 5)), 
    XMLINSERTION.[M4_WEED2] = (SELECT ID FROM TARGET_WEEDS_LOOKUP WHERE NLinks_Ref IN (SELECT VALUE FROM @FormFields WHERE fieldName = 'Weed_2' AND TabNum = 5)),  
    XMLINSERTION.[M4_WEED3] = (SELECT ID FROM TARGET_WEEDS_LOOKUP WHERE NLinks_Ref IN (SELECT VALUE FROM @FormFields WHERE fieldName = 'Weed_3' AND TabNum = 5)),  
    XMLINSERTION.[M4_WEED4] = (SELECT ID FROM TARGET_WEEDS_LOOKUP WHERE NLinks_Ref IN (SELECT VALUE FROM @FormFields WHERE fieldName = 'Weed_4' AND TabNum = 5)), 
    XMLINSERTION.[M4_WEEDCOMMENTS] = (SELECT VALUE FROM @FormFields WHERE fieldName = 'Comments' AND TabNum = 5),
    XMLINSERTION.[ActivityID4] = (SELECT VALUE FROM @FormFields WHERE fieldName = 'ActivityID' AND TabNum = 5)


    WHERE TRIGGERFIELD > 0

    EXEC sp_xml_removedocument @hDoc

    DROP TABLE #Temp

UPDATE b
    SET Hrs_Worked = x.HOURS,
    Method1 = x.M1_METHOD,
    Method1_Herbicide = x.M1_HERBICIDE1,
    Method1_Rate = ROUND(x.M1_HERBICIDE1RATE, 2),
    Method1_Add_Rate = ROUND(x.M1_HERBICIDE1ADDRATE, 2),
    Method1_Litres_Used = x.M1_HERBICIDE1QTY,
    Method1_Surface = x.M1_HERBICIDE1SURFECTANT,
    Method1_Add_Herbicide = x.M1_HERBICIDE2,
    Method1_Weeds = x.M1_WEED1,
    Method1_Weeds2 = x.M1_WEED2,
    Method1_Weeds3 = x.M1_WEED3,
    Method1_Weeds4 = x.M1_WEED4,
    Method1_Details = x.M1_WEEDCOMMENTS,
    Priority_Tasks = x.PRIORITY_TASKS,
    NLinks_ActivityID1 = x.ActivityID1,
    Method2 = x.M2_METHOD,
    Method2_Herbicide = x.M2_HERBICIDE1,
    Method2_Add_Herbicide = x.M2_HERBICIDE2,
    Method2_Rate = ROUND(x.M2_HERBICIDE2RATE,2),
    Method2_Add_Rate = ROUND(x.M2_HERBICIDE2ADDRATE,2),
    Method2_Litres_Used = x.M2_HERBICIDE2QTY,
    Method2_Surface = x.M2_HERBICIDE1SURFECTANT,
    Method2_Weeds = x.M2_WEED1,
    Method2_Weeds2 = x.M2_WEED2,
    Method2_Weeds3 = x.M2_WEED3,
    Method2_Weeds4 = x.M2_WEED4,
    Method2_Details = x.M2_WEEDCOMMENTS,
    NLinks_ActivityID2 = x.ActivityID2,
    Method3 = x.M3_METHOD,
    Method3_Herbicide = x.M3_HERBICIDE1,
    Method3_Add_Herbicide = x.M3_HERBICIDE2,
    Method3_Rate = ROUND(x.M3_HERBICIDE3RATE,2),
    Method3_Add_Rate = ROUND(x.M3_HERBICIDE3ADDRATE,2),
    Method3_Litres_Used = x.M3_HERBICIDE3QTY,
    Method3_Surface = x.M3_HERBICIDE1SURFECTANT,
    Method3_Weeds = x.M3_WEED1,
    Method3_Weeds2 = x.M3_WEED2,
    Method3_Weeds3 = x.M3_WEED3,
    Method3_Weeds4 = x.M3_WEED4,
    Method3_Details = x.M3_WEEDCOMMENTS,
    NLinks_ActivityID3 = x.ActivityID3,
    Method4 = x.M4_METHOD,
    Method4_Herbicide = x.M4_HERBICIDE1,
    Method4_Add_Herbicide = x.M4_HERBICIDE2,
    Method4_Rate = ROUND(x.M4_HERBICIDE4RATE,2),
    Method4_Add_Rate = ROUND(x.M4_HERBICIDE4ADDRATE,2),
    Method4_Litres_Used = x.M4_HERBICIDE4QTY,
    Method4_Surface = x.M4_HERBICIDE1SURFECTANT,
    Method4_Weeds = x.M4_WEED1,
    Method4_Weeds2 = x.M4_WEED2,
    Method4_Weeds3 = x.M4_WEED3,
    Method4_Weeds4 = x.M4_WEED4,
    Method4_Details = x.M4_WEEDCOMMENTS,
    NLinks_ActivityID4 = x.ActivityID4

FROM
Bushland_dev.dbo.Weeding_Reports b INNER JOIN Bushland_dev.dbo.XMLINSERTION 
x ON b.GMSC_Key = x.TRIGGERFIELD
WHERE b.GMSC_Key = x.TRIGGERFIELD

UPDATE Bushland_dev.dbo.XMLINSERTION
SET TRIGGERFIELD = 0,
Imported = 1
FROM
Bushland_dev.dbo.XMLINSERTION x INNER JOIN Bushland_dev.dbo.Weeding_Reports 
b
ON b.GMSC_Key = x.TRIGGERFIELD
WHERE x.TRIGGERFIELD = b.GMSC_Key       

END

GO

XML_插入表

USE [bushland_dev]
    GO

SET ANSI_NULLS ON
    GO
SET QUOTED_IDENTIFIER ON
    GO
CREATE TABLE [dbo].[XMLINSERTION](
[FILEPATH] [nvarchar](250) NULL,
[FILENAME] [nvarchar](250) NULL,
[ID] [int] IDENTITY(1,1) NOT NULL,
[Hours] [float] NULL,
[TRIGGERFIELD] [int] NULL,
[M1_HERBICIDE1] [int] NULL,
[M1_HERBICIDE1RATE] [real] NULL,
[M1_HERBICIDE1QTY] [float] NULL,
[M1_HERBICIDE1SURFECTANT] [int] NULL,
[M1_WEED1] [int] NULL,
[M1_WEED2] [int] NULL,
[M1_WEED3] [int] NULL,
[M1_WEED4] [int] NULL,
[M1_WEEDCOMMENTS] [nvarchar](max) NULL,
[PRIORITY_TASKS] [nvarchar](max) NULL,
[M2_HERBICIDE2] [int] NULL,
[M2_HERBICIDE2RATE] [real] NULL,
[M2_HERBICIDE2QTY] [float] NULL,
[M2_HERBICIDE1SURFECTANT] [int] NULL,
[M2_WEED1] [int] NULL,
[M2_WEED2] [int] NULL,
[M2_WEED3] [int] NULL,
[M2_WEED4] [int] NULL,
[M2_WEEDCOMMENTS] [nvarchar](max) NULL,
[M3_HERBICIDE3] [int] NULL,
[M3_HERBICIDE3RATE] [real] NULL,
[M3_HERBICIDE3QTY] [float] NULL,
[M3_HERBICIDE1SURFECTANT] [int] NULL,
[M3_WEED1] [int] NULL,
[M3_WEED2] [int] NULL,
[M3_WEED3] [int] NULL,
[M3_WEED4] [int] NULL,
[M3_WEEDCOMMENTS] [nvarchar](max) NULL,
[M4_HERBICIDE4] [int] NULL,
[M4_HERBICIDE4RATE] [real] NULL,
[M4_HERBICIDE4QTY] [float] NULL,
[M4_HERBICIDE1SURFECTANT] [int] NULL,
[M4_WEED1] [int] NULL,
[M4_WEED2] [int] NULL,
[M4_WEED3] [int] NULL,
[M4_WEED4] [int] NULL,
[M4_WEEDCOMMENTS] [nvarchar](max) NULL,
[Imported] [int] NULL,
[M1_METHOD] [varchar](10) NULL,
[M2_METHOD] [varchar](10) NULL,
[M3_METHOD] [varchar](10) NULL,
[M4_METHOD] [varchar](10) NULL,
[M1_HERBICIDE2] [int] NULL,
[M2_HERBICIDE1] [int] NULL,
[M3_HERBICIDE1] [int] NULL,
[M3_HERBICIDE2] [int] NULL,
[M4_HERBICIDE1] [int] NULL,
[M4_HERBICIDE2] [int] NULL,
[M1_HERBICIDE1ADDRATE] [real] NULL,
[M2_HERBICIDE2ADDRATE] [real] NULL,
[M3_HERBICIDE3ADDRATE] [real] NULL,
[M4_HERBICIDE4ADDRATE] [real] NULL,
[WR_GMSC_Key] [int] NULL,
[ActivityID1] [int] NULL,
[ActivityID2] [int] NULL,
[ActivityID3] [int] NULL,
[ActivityID4] [int] NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

GO

除草报告表

USE [bushland_dev]
    GO

SET ANSI_NULLS ON
    GO
SET QUOTED_IDENTIFIER ON
    GO
CREATE TABLE [dbo].[Weeding_Reports](
[GMSC_KEY] [int] IDENTITY(1,1) NOT NULL,
[Author] [varchar](80) NULL,
[Hrs_Worked] [varchar](10) NULL,
[Method1] [varchar](10) NULL,
[Method1_Weeds] [varchar](10) NULL,
[Method1_Herbicide] [varchar](10) NULL,
[Method1_Add_Herbicide] [varchar](10) NULL,
[Method1_Add_Rate] [varchar](10) NULL,
[Method1_Surface] [varchar](50) NULL,
[Method1_Surface_Rate] [varchar](10) NULL,
[Method1_Litres_Used] [varchar](10) NULL,
[Method1_Details] [varchar](1000) NULL,
[Method2] [varchar](10) NULL,
[Method2_Weeds] [varchar](10) NULL,
[Method2_Herbicide] [varchar](10) NULL,
[Method2_Rate] [varchar](10) NULL,
[Method2_Add_Herbicide] [varchar](10) NULL,
[Method2_Add_Rate] [varchar](10) NULL,
[Method2_Surface] [varchar](50) NULL,
[Method2_Surface_Rate] [varchar](10) NULL,
[Method2_Litres_Used] [varchar](10) NULL,
[Method2_Details] [varchar](500) NULL,
[Method3] [varchar](10) NULL,
[Method3_Weeds] [varchar](10) NULL,
[Method3_Herbicide] [varchar](10) NULL,
[Method3_Rate] [varchar](10) NULL,
[Method3_Add_Herbicide] [varchar](10) NULL,
[Method3_Add_Rate] [varchar](10) NULL,
[Method3_Surface] [varchar](50) NULL,
[Method3_Surface_Rate] [varchar](10) NULL,
[Method3_Litres_Used] [varchar](10) NULL,
[Method3_Details] [varchar](500) NULL,
[Method4] [varchar](10) NULL,
[Method4_Weeds] [varchar](10) NULL,
[Method4_Herbicide] [varchar](10) NULL,
[Method4_Rate] [varchar](10) NULL,
[Method4_Add_Herbicide] [varchar](10) NULL,
[Method4_Add_Rate] [varchar](10) NULL,
[Method4_Surface] [varchar](50) NULL,
[Method4_Surface_Rate] [varchar](10) NULL,
[Method4_Litres_Used] [varchar](10) NULL,
[Method4_Details] [varchar](500) NULL,
[Auditor] [varchar](80) NULL,
[Audit_Others_Present] [varchar](80) NULL,
[Methods_Applied] [varchar](50) NULL,
[Methods_Applied_com] [varchar](500) NULL,
[Timing_Appropriate] [varchar](50) NULL,
[Timing_Appropriate_com] [varchar](1000) NULL,
[Herbicide_Appropriate] [varchar](50) NULL,
[Herbicide_Appropriate_com] [varchar](500) NULL,
[Accecptable_Kill] [varchar](50) NULL,
[Accecptable_Kill_com] [varchar](500) NULL,
[Off_Target_Kill] [varchar](50) NULL,
[Off_Target_Kill_com] [varchar](500) NULL,
[Thorough_Works] [varchar](50) NULL,
[Thorough_Works_com] [varchar](500) NULL,
[Cautions_Adhered] [varchar](50) NULL,
[Cautions_Adhered_com] [varchar](500) NULL,
[Instructions_Adhered] [varchar](50) NULL,
[Instructions_Adhered_com] [varchar](500) NULL,
[Works_Impact] [varchar](50) NULL,
[Works_Impact_com] [varchar](500) NULL,
[Weeding_Management_FK] [int] NULL,
[Geometry] [varbinary](max) NULL,
[Geometry_spa] [geometry] NULL,
[off_Target_Damage_com] [varchar](500) NULL,
[non_Treated_Weeds_com] [varchar](500) NULL,
[Priority_Tasks] [varchar](500) NULL,
[Next_Visit] [varchar](500) NULL,
[Method1_Weeds2] [varchar](10) NULL,
[Method1_Weeds3] [varchar](10) NULL,
[Method1_Weeds4] [varchar](10) NULL,
[Method2_Weeds2] [varchar](10) NULL,
[Method2_Weeds3] [varchar](10) NULL,
[Method2_Weeds4] [varchar](10) NULL,
[Method3_Weeds2] [varchar](10) NULL,
[Method3_Weeds3] [varchar](10) NULL,
[Method3_Weeds4] [varchar](10) NULL,
[Method4_Weeds2] [varchar](10) NULL,
[Method4_Weeds3] [varchar](10) NULL,
[Method4_Weeds4] [varchar](10) NULL,
[Audit_Date] [date] NULL,
[ACTION_ID] [nvarchar](50) NULL,
[Works_On_Target_com] [nvarchar](500) NULL,
[Works_On_Target] [nvarchar](50) NULL,
[off_Target_Damage] [nvarchar](50) NULL,
[non_Treated_Weeds] [nvarchar](50) NULL,
[Date] [date] NULL,
[Locked] [nvarchar](10) NULL,
[Method1_Rate] [float] NULL,
[Further_Comments] [nvarchar](1000) NULL,
[isDisposed] [nvarchar](10) NULL,
[Methods_Applied_res] [varchar](750) NULL,
[Timing_Appropriate_res] [varchar](1000) NULL,
[Herbicide_Appropriate_res] [varchar](750) NULL,
[Accecptable_Kill_res] [varchar](750) NULL,
[Off_Target_Kill_res] [varchar](750) NULL,
[Thorough_Works_res] [varchar](750) NULL,
[Cautions_Adhered_res] [varchar](750) NULL,
[Instructions_Adhered_res] [varchar](750) NULL,
[Works_Impact_res] [varchar](750) NULL,
[Works_On_Target_res] [varchar](750) NULL,
[Responder] [nvarchar](100) NULL,
[Responder_date] [date] NULL,
[Crew_Leader] [varchar](80) NULL,
[isArchived] [varchar](10) NULL,
[Financial_Year] [nvarchar](10) NULL,
[Method1_Other] [nvarchar](100) NULL,
[Method2_Other] [nvarchar](100) NULL,
[Method3_Other] [nvarchar](100) NULL,
[Method4_Other] [nvarchar](100) NULL,
[Weeds_Targeted_Other1] [nvarchar](200) NULL,
[Weeds_Targeted_Other2] [nvarchar](200) NULL,
[Weeds_Targeted_Other3] [nvarchar](200) NULL,
[Weeds_Targeted_Other4] [nvarchar](200) NULL,
[FILEPATH] [int] NULL,
[NLinks_ActivityID1] [int] NULL,
[NLinks_ActivityID2] [int] NULL,
[NLinks_ActivityID3] [int] NULL,
[NLinks_ActivityID4] [int] NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

GO

【问题讨论】:

所以澄清一下,SQL 准备语句是一个插入到表中的插入,无论触发器与否都应该运行。由于某种原因,启用触发器时都不会运行。我尝试将触发器设置为在 FOR 和 AFTER UPDATE、INSERT 上运行。这没什么区别。插入只是不想在启用触发器的情况下工作。 请包含表和触发器的 DDL。 参见附加的 DDLS。对不起,如果它很长。 【参考方案1】:

我在使用 c++ sqlite 绑定时遇到了一个非常相似的问题。如果触发器在 BEGIN ... END 中有任何错字;部分,它实际上仍然允许添加触发器,但会破坏它引用的表。我的触发器中有一个错字,它似乎仍然添加了触发器,但是准备一个将值插入到引用的表之一的语句失败并出现一般逻辑错误。我建议您非常非常仔细地阅读您的触发器,以确保您没有错别字。

【讨论】:

以上是关于启用 SQL 触发器时,Java 中的 Prepared Statement 失败的主要内容,如果未能解决你的问题,请参考以下文章

SQL注入与jdbc操作详解

SQL Server:检查触发器是启用还是禁用?

java 连接oracle数据库

SQL Server触发器的禁用和启用

SQL Server 无法插入启用触发器的表中

java servlet练习测试