別のテーブル ( ) をデータEquip
で更新するトリガーを持つテーブル ( 、挿入された) があります。SSMS をasset_tracker.dbo.EIS_JobAssetMaint
使用してデータを手動で更新すると、トリガーによって列と列が適切に更新されます。Equip
Maxtemp
RunHrs
asset_tracker.dbo.EIS_JobAssetMaint
Equip
C# で Linq To Sql コードを編集すると、Equip
更新asset_tracker.dbo.EIS_JobAssetMaint
されますが、トリガーから部分的にしか更新されません。
つまり、 のデータEquip
は正しいが、2 つの列、Maxtemp
およびが列にRuntime
引き継がれず、asset_tracker.dbo.EIS_JobAssetMaint
Maxtemp
RunHrs
トリガーは次のとおりです。
ALTER TRIGGER [dbo].[Eis_JobAssetMaint_trigger_new]
ON [dbo].[Equip]
For INSERT, UPDATE
AS
BEGIN
SET NOCOUNT ON
DECLARE @runhrsDiff float,
@jobID int,
@partno varchar(20),
@assetno varchar(36),
@maxtemp float,
@runtime float,
@assetID int
SELECT @jobID = (Select JobID from inserted),
@partno = (Select Part_No from inserted),
@assetno = (Select Asset_No from inserted),
@maxtemp = (Select MaxTemp from inserted),
@runtime = (Select RunTime from inserted)
SELECT @runhrsDiff = t2.runtime - t1.Runhrs FROM asset_tracker.dbo.EIS_JobAssetMaint AS t1 INNER JOIN INSERTED AS t2
ON t1.Part_No = t2.part_no AND t1.Asset_No = t2.asset_no AND t1.jobid = t2.jobid
IF Exists (Select t1.jobid,t1.Asset_No,t1.Part_No from asset_tracker.dbo.Eis_jobassetmaint as t1
inner join Inserted T2 on T1.Jobid = T2.jobid and T1.Part_no = T2.Part_no and T1.Asset_No = T2.Asset_no)
Begin
UPDATE t10 SET t10.RunHrs = t10.Runhrs+@runhrsDiff
, t10.maxt = CASE WHEN t12.maxtemp > t10.maxt then t12.maxtemp ELSE t10.maxt END
from asset_tracker.dbo.EIS_repairlog_maint as t10
INNER JOIN asset_tracker.dbo.eis_repairlog as t11 ON t10.repair_number = t11.repair_number AND t11.repair_status IN (1,4,6,7) AND t11.repair_type IN (13,14,15)
INNER JOIN INSERTED AS t12 ON t12.part_no = t11.part_number AND t12.asset_no = t11.asset_number
Update T1 set T1.RunHrs = T2.Runtime , T1.MaxTemp = T2.MaxTemp
From Inserted as T2 inner join asset_tracker.dbo.Eis_JobAssetMaint as T1
on T2.Jobid = T1.jobid and T2.Part_no = T1.Part_no and T2.Asset_No = T1.Asset_no
End
Else
BEGIN TRY
----------------- new line
UPDATE t10 SET t10.RunHrs = t10.Runhrs + t12.runtime
, t10.maxt = CASE WHEN t12.maxtemp > t10.maxt then t12.maxtemp ELSE t10.maxt END
from asset_tracker.dbo.EIS_repairlog_maint as t10
INNER JOIN asset_tracker.dbo.eis_repairlog as t11 ON t10.repair_number = t11.repair_number AND t11.repair_status IN (1,4,6,7)
INNER JOIN INSERTED AS t12 ON t12.part_no = t11.part_number AND t12.asset_no = t11.asset_number
IF EXISTS (SELECT t1.Assetname, t1.Part_No from asset_tracker.dbo.AssetPositions as t1
inner join Inserted as t2 on T1.Part_no = T2.Part_no and T1.Assetname = T2.asset_no)
IF NOT Exists (Select t1.jobid,t1.Asset_No,t1.Part_No from asset_tracker.dbo.Eis_jobassetmaint as t1
inner join Inserted T2 on T1.Jobid = T2.jobid and T1.Part_no = T2.Part_no and T1.Asset_No = T2.Asset_no)
BEGIN
PRINT ('Attempting to insert new record into JobAssetMaint')
Insert into asset_tracker.dbo.Eis_JobAssetMaint
( Jobid
,Part_no
,Asset_no
,MaxTemp
,MaxTempIgnore
,RunHrs
,RunHrsIgnore
,jobcnt
,JobCntIgnore
,MaxShock
,MaxShockIgnore
,LastUpdated
)
VALUES ( @jobID,
@partno,
@assetno,
@maxtemp,
0,
@runtime,
0,
1,
0,
0,
0,
getdate()
)
--From inserted
END
END Try
BEGIN CATCH
--Declare @jobid int
select @jobid = jobid from inserted
RAISERROR (N'The INSERT statement conflicted with the FOREIGN KEY constraint "FK_EIS_JobAssetMaint 2_AssetPositions". The conflict occurred in database "asset_tracker", table "dbo.AssetPositions".The current JOBID is:%d ',10, 1,@jobid);
END CATCH
END
これがあまり混乱しないことを願っています。もしそうなら、私に知らせてください。