SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Old Value


Old Value

Author
Message
hrvoje.voda
hrvoje.voda
SSC Rookie
SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)

Group: General Forum Members
Points: 32 Visits: 5
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
John Mitchell-245523
John Mitchell-245523
SSC Guru
SSC Guru (80K reputation)SSC Guru (80K reputation)SSC Guru (80K reputation)SSC Guru (80K reputation)SSC Guru (80K reputation)SSC Guru (80K reputation)SSC Guru (80K reputation)SSC Guru (80K reputation)

Group: General Forum Members
Points: 80498 Visits: 17931
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
hrvoje.voda
hrvoje.voda
SSC Rookie
SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)

Group: General Forum Members
Points: 32 Visits: 5
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 će 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


John Mitchell-245523
John Mitchell-245523
SSC Guru
SSC Guru (80K reputation)SSC Guru (80K reputation)SSC Guru (80K reputation)SSC Guru (80K reputation)SSC Guru (80K reputation)SSC Guru (80K reputation)SSC Guru (80K reputation)SSC Guru (80K reputation)

Group: General Forum Members
Points: 80498 Visits: 17931
Brilliant. Sample data and update query as well, please.

Thanks
John
hrvoje.voda
hrvoje.voda
SSC Rookie
SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)

Group: General Forum Members
Points: 32 Visits: 5
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.
Attachments
data.png (25 views, 10.00 KB)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum








































































































































































SQLServerCentral


Search