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 |
san79
Starting Member
42 Posts |
Posted - 2008-03-28 : 04:13:02
|
hi i am having a table called allowances one of the field is used to determine the given allowance can be a basic pay or not. when once it is given 'y' then the future records should not be given 'y'. i wrote a trigger for it, but it allows me multiple entering of 'y'. i am pasting my trigger code here.ALTER TRIGGER tblAllowances_Trigger1ON dbo.tblAllowancesFOR insertAS if (SELECT COUNT(*) FROM dbo.tblAllowances where IsBasicpay='y')>=1 begin raiserror ('invalid',1,10) rollback end please correct me if i have done anything silly anticipating your valuable comments and replies.best regardss.an |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-03-28 : 04:20:12
|
I think you need an instead of trigger for this purpose. omething like:-ALTER TRIGGER tblAllowances_Trigger1ON dbo.tblAllowancesINSTEAD OF insertAS if (SELECT COUNT(*) FROM dbo.tblAllowances where IsBasicpay='y')=0 begin INSERT INTO tblAllowances SELECT * FROM INSERTED endGO |
 |
|
san79
Starting Member
42 Posts |
Posted - 2008-03-29 : 10:17:30
|
hi thanks for the reply, it is actually do work but it dose not allow me to enter any record after a single 'y' record. simply putting there can be any number of 'n' but only one 'y' should be present.how can i modify the sql statement |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-03-29 : 10:34:24
|
[code]ALTER TRIGGER tblAllowances_Trigger1ON dbo.tblAllowancesINSTEAD OF insertAS if (SELECT COUNT(*) FROM dbo.tblAllowances where IsBasicpay='y')=0 OR (SELECT IsBasicpay FROM INSERTED)='n' begin INSERT INTO tblAllowances SELECT * FROM INSERTED endGO[/code] |
 |
|
san79
Starting Member
42 Posts |
Posted - 2008-03-30 : 02:28:14
|
thanks boss that works great, and also another thing, how i indicate the user that the record is not inserted, i used a raiserror ('invalid',1,10) but the error didnt pooped out. here is the codeif (SELECT COUNT(*) FROM dbo.tblAllowances where IsBasicpay='y')=0 OR (SELECT IsBasicpay FROM INSERTED)='n' begin INSERT INTO tblAllowances SELECT * FROM INSERTED end raiserror ('invalid'1,10)pls note that the user input is from a vb program |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-03-30 : 03:08:20
|
More info on error handling here:-http://www.sommarskog.se/error-handling-I.html |
 |
|
san79
Starting Member
42 Posts |
Posted - 2008-03-31 : 03:41:25
|
hithanks for the excellent support rendered by you, it worked, the link was very useful i have to just changeif (SELECT COUNT(*) FROM dbo.tblAllowances where IsBasicpay='y')=0 OR (SELECT IsBasicpay FROM INSERTED)='n'beginINSERT INTO tblAllowancesSELECT * FROM INSERTEDendelsebeginraiserror ('invalid',16,10)rollbackreturnendi think the error priority 16 worked the magic |
 |
|
|
|
|
|
|