Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
kyleb
Starting Member
7 Posts |
Posted - 2012-09-12 : 18:52:17
|
Hello, I am looking for suggestions including syntax and thank you in advance! Details:1) We have program code that we cannot alter that fires an update to our PAYMENTS table by updating a column called CardAuthStatus to equal "NEEDAUTH" when certain criteria are met.2) Using a trigger, we want to prevent/undo/overwrite that table update to keep CardAuthStatus as a NULL value when this criteria is met (DATE is also a field in the PAYMENTS table):DATEDIFF(DAY,DATEADD(HOUR,1,CURRENT_TIMESTAMP),DATE)>0(in other words, if the event is tomorrow I do not want it authorizing the card if it is today, only if the order is placed tomorrow)3) As an FYI, I believe other PAYMENTS table fields may get updated at the same time that the CARDAUTHSTATUS is getting updated as part of this routine that we cannot alter.4) Once that field value gets set to "NEEDAUTH" behind it a credit card authorization request fires to our credit card processor, which we are trying to stop by stopping or modifying this update to keep the field value at NULL (which it was before it was changed to "NEEDAUTH"). Only as an FYI, once the credit card auth comes back from the processor the "NEEDAUTH" value changes to something else based on the authorization.5) Here's the most recent code our developer suggested, but it has syntax issues....CREATE TRIGGER [dbo].[CardAuthStatus] ON [dbo].[Payments] UPDATEAS UPDATE inserted SET CARDAUTHSTATUS=NULL WHERE CARDAUTHSTATUS LIKE 'NEEDAUTH' AND DATEDIFF(DAY,DATEADD(HOUR,1,CURRENT_TIMESTAMP),DATE)>0GOSuggested syntax is greatly appreciated! Again thank you for taking the time to look at this for me. |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-09-12 : 22:30:30
|
it should be an INSTEAD OF UPDATE triggerCREATE TRIGGER [dbo].[CardAuthStatus]ON [dbo].[Payments]INSTEAD OF UPDATEASBEGINUPDATE pSET p.CardAuthStatus = 'NEEDAUTH'FROM INSERTED iINNER JOIN [dbo].[Payments] pON p.PK = i.PKWHERE DATEDIFF(DAY,DATEADD(HOUR,1,CURRENT_TIMESTAMP),DATE)<=0END PK is primary key(s) of your table------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
kyleb
Starting Member
7 Posts |
Posted - 2012-09-13 : 00:30:37
|
Thanks visakh16 for reply. I assume you meant NULL in the SET statement.SET p.CardAuthStatus = 'NEEDAUTH' (you meant NULL)Also. If other fields were updated as part of the UPDATE statement we are trying to alter, do those need to get referenced in some way in your UPDATE statement below?UPDATE pSET p.CardAuthStatus = 'NEEDAUTH'FROM INSERTED iINNER JOIN [dbo].[Payments] pON p.PK = i.PKWHERE DATEDIFF(DAY,DATEADD(HOUR,1,CURRENT_TIMESTAMP),DATE)<=0ENDLastly, I am assuming I should reference the "i" table when referencing the date field on this line.WHERE DATEDIFF(DAY,DATEADD(HOUR,1,CURRENT_TIMESTAMP),DATE)<=0 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-09-13 : 10:32:42
|
quote: Originally posted by kyleb Thanks visakh16 for reply. I assume you meant NULL in the SET statement.SET p.CardAuthStatus = 'NEEDAUTH' (you meant NULL)Also. If other fields were updated as part of the UPDATE statement we are trying to alter, do those need to get referenced in some way in your UPDATE statement below?UPDATE pSET p.CardAuthStatus = 'NEEDAUTH'FROM INSERTED iINNER JOIN [dbo].[Payments] pON p.PK = i.PKWHERE DATEDIFF(DAY,DATEADD(HOUR,1,CURRENT_TIMESTAMP),DATE)<=0ENDLastly, I am assuming I should reference the "i" table when referencing the date field on this line.WHERE DATEDIFF(DAY,DATEADD(HOUR,1,CURRENT_TIMESTAMP),DATE)<=0
Nope I meant NEEDAUTHsee my used condition (its just opposite of yours)if other fields need to be updates yes you need to refer them in updateDepends on whether you need to compared to existing or new value for Date. If former its p else its i------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
kyleb
Starting Member
7 Posts |
Posted - 2012-09-13 : 11:59:19
|
Thanks again. This is what we ended up doing and it's working perfectly now! (the after update trigger is happening before the credit card auth gets run and this saves us from having to worry about dealing with any other fields that are getting updated). If there are performance benefits from doing it before update let me know your thoughts.ALTER TRIGGER [dbo].[CardAuthStatus] ON [dbo].[Payments] AFTER UPDATEAS UPDATE PAYMENTS SET CARDAUTHSTATUS=DELETED.CARDAUTHSTATUSFROM PAYMENTS INNER JOIN INSERTED ON INSERTED.ID=PAYMENTS.ID INNER JOIN DELETED ON DELETED.ID=INSERTED.IDWHERE INSERTED.CARDAUTHSTATUS = 'NEEDAUTH' AND INSERTED.LOCKED IS NULLAND DATEDIFF(DAY,DATEADD(HOUR,1,CURRENT_TIMESTAMP),PAYMENTS.DATE)>0 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-09-13 : 12:10:36
|
ok...but keep in mind that this gets executed after actual update action------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
kyleb
Starting Member
7 Posts |
Posted - 2012-09-13 : 13:04:09
|
Got it. From what I can tell the update after is processing before our credit card processor gets its signal so it's working OK right now. Thanks again for your input. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-09-13 : 13:20:02
|
ok...then you should be good!------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
|
|
|
|
|