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.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 tigger failing

Author  Topic 

rajasekhar857
Constraint Violating Yak Guru

396 Posts

Posted - 2010-03-02 : 08:03:13
Hi trigger is not deleting records inside the trigger body.



CREATE TRIGGER TR_DELETE_EMRTransactions ON EMRTransactions FOR DELETE
AS
BEGIN
DECLARE
@CO_TRANSACTION_ID VARCHAR(50)
select @CO_TRANSACTION_ID = COALESCE(@CO_TRANSACTION_ID + ',', '') + CAST
(EMR_TRANSACTION_ID AS VARCHAR(20)) FROM DELETED WHERE ROLE_NAME IN ('CONSULTANT')
if(@CO_TRANSACTION_ID!=null AND @CO_TRANSACTION_ID!='')
set @CO_TRANSACTION_ID = substring(@CO_TRANSACTION_ID,0,len(@CO_TRANSACTION_ID))
print @CO_TRANSACTION_ID
DELETE FROM EMRCOTransactions WHERE CAST(EMR_TRANSACTION_ID AS VARCHAR(50)) IN (@CO_TRANSACTION_ID)
--select * from EMRCOTransactions WHERE CAST(EMR_TRANSACTION_ID AS VARCHAR(50)) IN (@CO_TRANSACTION_ID)
END
GO

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-03-02 : 08:37:10
lots of problems

1. You're using variables to store values from DELETED table so this will work fine only when you're deleting a single record. batch deletion results in multiple records in DELETED so you might need to use table variable to hold the values in that case
2.condition checks like @CO_TRANSACTION_ID!=null will work only if your ANSI NULL settings is OFF (which is not default option). you need to use @CO_TRANSACTION_ID IS NOT NULL instead
3.better to take join with DELETED table in last table rather than using In and variable.

so your above code can be replaced by


CREATE TRIGGER TR_DELETE_EMRTransactions ON EMRTransactions FOR DELETE
AS
BEGIN

DELETE e
FROM EMRCOTransactions e
JOIN DELETED d
ON CAST(e.EMR_TRANSACTION_ID AS VARCHAR(50))= CAST(d.EMR_TRANSACTION_ID AS VARCHAR(20))
WHERE d.ROLE_NAME IN ('CONSULTANT')
END
GO



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-03-02 : 08:41:49
No need of CAST as both would have the same datatype

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-03-02 : 08:48:28
quote:
Originally posted by madhivanan

No need of CAST as both would have the same datatype

Madhivanan

Failing to plan is Planning to fail


yeah..exactly. sorry didnt notice that. just copy pasted from OPs query

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

rajasekhar857
Constraint Violating Yak Guru

396 Posts

Posted - 2010-03-02 : 10:24:21
Hi visakh and Madhi sorry to ask once agin as Trigger given by you is working extremely good.
But if i try in oracle the same with this what need to be modified.

i have written delete query like
DELETE FROM EMRCOTransactions e JOIN DELETED d ON (e.EMR_TRANSACTION_ID)= d.EMR_TRANSACTION_ID) WHERE d.ROLE_NAME IN ('CONSULTANT')
what needs to replace Deleted as it is OLD.EMRTransactions OR EMRTransactions.help me in this regard too
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-03-02 : 10:28:14
quote:
Originally posted by rajasekhar857

Hi visakh and Madhi sorry to ask once agin as Trigger given by you is working extremely good.
But if i try in oracle the same with this what need to be modified.

i have written delete query like
DELETE FROM EMRCOTransactions e JOIN DELETED d ON (e.EMR_TRANSACTION_ID)= d.EMR_TRANSACTION_ID) WHERE d.ROLE_NAME IN ('CONSULTANT')
what needs to replace Deleted as it is OLD.EMRTransactions OR EMRTransactions.help me in this regard too


I'm not sure on Oracle. You may be better off asking this in some oracle forums. This is MS SQL Server forums and not too many people here knows Oracle.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

rajasekhar857
Constraint Violating Yak Guru

396 Posts

Posted - 2010-03-02 : 10:30:49
Thanks Visakh and Madhi once again
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-03-02 : 10:37:40
quote:
Originally posted by rajasekhar857

Thanks Visakh and Madhi once again


Good that at last you learned how to respond

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-03-03 : 01:40:20
Try posting at www.orafaq.com

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -