The following trigger
marks me the following error when executing it:
Msg 8197, Level 16, State 4, Procedure InsertHystoryOfOrderHasProfiles, Line 1 The object 'dbo.OrderHasProfilesOfPayments' does not exist or is invalid for this operation.
But that table does exist and has that name exactly:
CREATE TRIGGER [dbo].[InsertHystoryOfOrderHasProfiles]
ON [dbo].[OrderHasProfilesOfPayments]
AFTER INSERT, UPDATE
AS
BEGIN
DECLARE @idOrderHasProfilesOfPayments int;
DECLARE @idPayment int;
DECLARE @idDriversProfilesOfPayments int;
DECLARE @idOrderTravelInfo int;
DECLARE @conceptDescription varchar(200);
DECLARE @Total decimal;
DECLARE @UOM varchar(20);
DECLARE @Distance decimal(18,0);
SET @idOrderHasProfilesOfPayments =
(
SELECT TOP 1 CAST ([dbo].[OrderHasProfilesOfPayments].[idOrderHasProfilesOfPayments] AS int)
FROM [dbo].[OrderHasProfilesOfPayments] ORDER BY [dbo].[OrderHasProfilesOfPayments].[idOrderHasProfilesOfPayments] DESC
);
-- Para casi todo es necesario el idDriversProfilesOfPayments por lo que es lo primero que obtendremos
SET @idDriversProfilesOfPayments =
(
SELECT CAST ([dbo].[OrderHasProfilesOfPayments].[idDriversProfilesOfPayments] AS int)
FROM [dbo].[OrderHasProfilesOfPayments]
WHERE [dbo].[OrderHasProfilesOfPayments].[idOrderHasProfilesOfPayments] = @idOrderHasProfilesOfPayments
);
SET @idOrderTravelInfo =
(
SELECT CAST ([dbo].[OrderHasProfilesOfPayments].[idOrderTravelInfo] AS int)
FROM [dbo].[OrderHasProfilesOfPayments]
WHERE [dbo].[OrderHasProfilesOfPayments].[idOrderHasProfilesOfPayments] = @idOrderHasProfilesOfPayments
);
-- Cargamos la variable Concept Description
SET @conceptDescription =
(
SELECT CAST (ISNULL (dbo.DistanceConcept.UOM, '')AS varchar(20)) +
CAST (ISNULL (LocationFrom.Location,'') AS varchar(50)) + '|*|' +
CAST (ISNULL (LocationTo.Location, '') AS varchar(50))
FROM
dbo.DriversProfilesOfPayments
FULL JOIN dbo.DistanceConcept ON dbo.DistanceConcept.idDistanceConcept = (
SELECT dbo.DriversPaymentsHasDistance.idDistanceConcept FROM dbo.DriversPaymentsHasDistance WHERE
dbo.DriversPaymentsHasDistance.idDriversProfilesOfPayments = dbo.DriversProfilesOfPayments.idDriversProfilesOfPayments )
FULL JOIN dbo.TrafficLocations AS LocationFrom ON LocationFrom.trafficLocationsID = (
SELECT dbo.LocationConcept.idLocationFrom FROM dbo.LocationConcept WHERE ( dbo.LocationConcept.idLocationConcept = (
SELECT dbo.DriversPaymentsHasLocation.idLocationConcept FROM dbo.DriversPaymentsHasLocation WHERE
dbo.DriversPaymentsHasLocation.idDriversProfilesOfPayments = dbo.DriversProfilesOfPayments.idDriversProfilesOfPayments )))
FULL JOIN dbo.TrafficLocations AS LocationTo ON LocationTo.trafficLocationsID = (
SELECT dbo.LocationConcept.idLocationTo FROM dbo.LocationConcept WHERE ( dbo.LocationConcept.idLocationConcept = (
SELECT dbo.DriversPaymentsHasLocation.idLocationConcept FROM dbo.DriversPaymentsHasLocation WHERE
dbo.DriversPaymentsHasLocation.idDriversProfilesOfPayments = dbo.DriversProfilesOfPayments.idDriversProfilesOfPayments )))
WHERE (dbo.DriversProfilesOfPayments.idDriversProfilesOfPayments = @idDriversProfilesOfPayments)
);
SET @idPayment =
(
SELECT CAST ([idPayment] AS INT )
FROM [DriversProfilesOfPayments]
WHERE [DriversProfilesOfPayments].[idDriversProfilesOfPayments] = @idDriversProfilesOfPayments
);
SET @UOM =
(
SELECT CAST (ISNULL (dbo.DistanceConcept.UOM,'') AS varchar(30))
FROM dbo.DistanceConcept
WHERE idDistanceConcept =
(
SELECT dbo.DriversPaymentsHasDistance.idDistanceConcept
FROM dbo.DriversPaymentsHasDistance
WHERE dbo.DriversPaymentsHasDistance.idDriversProfilesOfPayments =
(
SELECT idDriversProfilesOfPayments
FROM dbo.DriversProfilesOfPayments
WHERE dbo.DriversProfilesOfPayments.idDriversProfilesOfPayments = @idDriversProfilesOfPayments
)
)
);
--inicializamos el valor de @UOM para posteriormente compararlo
-- SI no tiene UOM debe pertenecer su ID a un location
IF @UOM = ''
BEGIN
SET @Total =
(
SELECT CAST (dbo.DriversProfilesOfPayments.amountBase AS decimal) +
CAST (dbo.DriversProfilesOfPayments.amount AS decimal)
FROM
dbo.DriversProfilesOfPayments
WHERE
dbo.DriversProfilesOfPayments.idDriversProfilesOfPayments = @idDriversProfilesOfPayments
);
END
ELSE
BEGIN
SET @Distance =
(
SELECT CAST ([dbo].[OrderTravelInfo].[distance] AS decimal(18,0)) FROM [dbo].[OrderTravelInfo] WHERE [dbo].[OrderTravelInfo].[OrderTravelInfoID] =
(
SELECT [dbo].[OrderHasProfilesOfPayments].[idOrderTravelInfo] FROM [dbo].[OrderHasProfilesOfPayments] WHERE [dbo].[OrderHasProfilesOfPayments].[idDriversProfilesOfPayments] = @idDriversProfilesOfPayments
)
);
IF @UOM LIKE '%Mile%'
BEGIN
SET @Total =
(
SELECT CAST (dbo.DriversProfilesOfPayments.amountBase AS decimal (18,2)) +
CAST (dbo.DriversProfilesOfPayments.amount AS decimal (18,2)) * (@Distance/1600.0)
FROM
dbo.DriversProfilesOfPayments
WHERE
dbo.DriversProfilesOfPayments.idDriversProfilesOfPayments = @idDriversProfilesOfPayments
);
END
ELSE
BEGIN
SET @Total =
(
SELECT CAST (dbo.DriversProfilesOfPayments.amountBase AS decimal (18,2)) +
CAST (dbo.DriversProfilesOfPayments.amount AS decimal (18,2)) * (@Distance/1000.0)
FROM
dbo.DriversProfilesOfPayments
WHERE
dbo.DriversProfilesOfPayments.idDriversProfilesOfPayments = @idDriversProfilesOfPayments
);
END
END
INSERT INTO [dbo].[HistoryOfOrderHasProfiles]
(
[idOrderHasProfilesOfPayments],
[idOrderTravelInfo],
[idPayment],
[conceptDescription],
[Total]
)
VALUES
(
@idOrderHasProfilesOfPayments,
@idOrderTravelInfo,
@idPayment,
@conceptDescription,
@Total
);
END
The Table is the following:
CREATE TABLE [dbo].[OrderHasProfilesOfPayments]
(
[idOrderHasProfilesOfPayments][int] IDENTITY(1,1) NOT NULL,
[idDriversProfilesOfPayments][int] NOT NULL,
[idOrderTravelInfo][int]NOT NULL UNIQUE,
PRIMARY KEY CLUSTERED
(
[idOrderHasProfilesOfPayments] ASC
)
)ON [PRIMARY]
CREATE TABLE [dbo].[HistoryOfOrderHasProfiles]
(
[idHistoryOfOrderHasProfiles][int] IDENTITY(1,1) NOT NULL,
[idOrderHasProfilesOfPayments][int] NOT NULL,
[idOrderTravelInfo][int] NOT NULL,
[idPayment][int] NOT NULL,
[conceptDescription][varchar](200),
[Total] [decimal],
[paidDate][DATE],
[Paid][bit],
PRIMARY KEY CLUSTERED
(
[idHistoryOfOrderHasProfiles] ASC
)
) ON [PRIMARY]