June 13, 2017 at 4:41 am
I'm using this trigger to update specific columns.
I want to get old values for some columns, but this update below only returns values for last row.
ALTER TRIGGER [dbo].[StatusChangeLog]
ON GH_Office_Material_Order_Material
AFTER UPDATE
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
DECLARE @OrderID INT
DECLARE @PartnerName varchar(100)
DECLARE @LastUpdated datetime
SET @OrderID = (SELECT OrderID FROM INSERTED)
SET @PartnerName = (SELECT Partner_Created FROM GH_Office_Material_Orders WHERE OrderID = @OrderID)
SET @LastUpdated = GETDATE()
SELECT MatrialID into #tempTbl from deleted where OrderID = @OrderID
UPDATE GH_Office_Material_Order_Material SET MaterialNameOld = d.MaterialName, StatusIDOld = d.StatusID, PartnerName = @PartnerName,
LastUpdated = @LastUpdated
from deleted d where d.MatrialID in (SELECT * FROM #tempTbl) AND d.OrderID = @OrderID
END
June 13, 2017 at 5:22 am
If more than one OrderID is updated at once, your trigger will only capture one of them. Indeed, I think it would return an error. Please will you provide table DDL and sample data (CREATE TABLE and INSERT statements), along with an UPDATE statement for which you're having this problem?
John
June 13, 2017 at 5:36 am
There is only one OrderID.
I don't get an error just data for last row:
GH_Office_Material_Orders :
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[GH_Office_Material_Orders](
[OrderID] [int] IDENTITY(1,1) NOT NULL,
[ReasonID] [int] NOT NULL,
[Name] [nvarchar](50) NULL,
[Partner_Created] [int] NULL,
[Date_Created] [datetime] NULL,
[Delivery_LocationID] [int] NULL,
[Partner_Approved] [int] NULL,
[Date_Approved] [datetime] NULL,
[StatusID] [int] NULL,
[StatusName] AS ([dbo].[Status_Name]([StatusID])),
[Access] [int] NULL,
[DocName] [varchar](150) NULL,
CONSTRAINT [PK_GH_Office_Material_Order] PRIMARY KEY CLUSTERED
(
[OrderID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
ALTER TABLE [dbo].[GH_Office_Material_Orders] WITH CHECK ADD CONSTRAINT [FK_GH_Office_Material_Orders_GH_Cost_Center1] FOREIGN KEY([Delivery_LocationID])
REFERENCES [dbo].[GH_Cost_Center] ([Cost_CenterID])
GO
ALTER TABLE [dbo].[GH_Office_Material_Orders] CHECK CONSTRAINT [FK_GH_Office_Material_Orders_GH_Cost_Center1]
GO
ALTER TABLE [dbo].[GH_Office_Material_Orders] WITH CHECK ADD CONSTRAINT [FK_GH_Office_Material_Orders_GH_Office_Material_Order_Reason] FOREIGN KEY([ReasonID])
REFERENCES [dbo].[GH_Office_Material_Order_Reason] ([ReasonID])
GO
ALTER TABLE [dbo].[GH_Office_Material_Orders] CHECK CONSTRAINT [FK_GH_Office_Material_Orders_GH_Office_Material_Order_Reason]
GO
ALTER TABLE [dbo].[GH_Office_Material_Orders] WITH CHECK ADD CONSTRAINT [FK_GH_Office_Material_Orders_GH_Office_Material_Order_Status] FOREIGN KEY([StatusID])
REFERENCES [dbo].[GH_Office_Material_Order_Status] ([StatusID])
GO
ALTER TABLE [dbo].[GH_Office_Material_Orders] CHECK CONSTRAINT [FK_GH_Office_Material_Orders_GH_Office_Material_Order_Status]
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'Ured u koji ce se izvršiti narudžba' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'GH_Office_Material_Orders'
GO
GH_Office_Material_Order_Material
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[GH_Office_Material_Order_Material](
[Material_OrderID] [int] IDENTITY(1,1) NOT NULL,
[OrderID] [int] NOT NULL,
[MatrialID] [int] NOT NULL,
[UnitID] [int] NOT NULL,
[Quantity] [int] NOT NULL,
[Unit_Price] [float] NOT NULL,
[Price] [float] NULL,
[Partner_Created] [int] NULL,
[Date_Created] [datetime] NULL,
[MaterialName] AS ([dbo].[Material_Name]([MatrialID])),
[MaterialGroupID] [int] NULL,
[StatusID] [int] NULL,
[QuantityDelivered] [int] NULL,
[QuantityUndelivered] [int] NULL,
[SupplierName] [varchar](50) NULL,
[Description] [varchar](max) NULL,
[CatalogBr] [nvarchar](50) NULL,
[MaterialNameOld] [varchar](150) NULL,
[StatusIDOld] [int] NULL,
[PartnerName] [varchar](100) NULL,
[LastUpdated] [datetime] NULL,
CONSTRAINT [PK_GH_Office_Material_Order_Material] PRIMARY KEY CLUSTERED
(
[Material_OrderID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
June 13, 2017 at 6:01 am
Brilliant. Sample data and update query as well, please.
Thanks
John
June 13, 2017 at 6:18 am
Update is handled by lightswitch app.
i'm using trigger to change data.
data - so StatusID is only the last row and MaterialNameOld has the same values not like column MaterialNAme - i hope I explained good.
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply