| Author |
Topic |
|
icw
Constraint Violating Yak Guru
378 Posts |
Posted - 2006-03-14 : 04:48:42
|
| Is there any way to create a "Conditional Insert Trigger"My Scenario is this;When a user adds an email address to the database, I want to look to see if the email address is like '%@acme-holdings%' and if it is then to change the value to 'Not allowed', otherwise to leave it alone and go ahead with inserting the original email addressTIAICW |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-03-14 : 05:04:52
|
| Cant you validate this in your Front End application and send values accordingly?In SQL, you can usecase when email like '%@acme-holdings%' then 'Not allowed' else email endMadhivananFailing to plan is Planning to fail |
 |
|
|
icw
Constraint Violating Yak Guru
378 Posts |
Posted - 2006-03-14 : 05:10:55
|
| Hi ,thanks for the quick reply.I can't do anything with the front end as it is a hard coded off the shelf product.As I am a bit of a newbie here can I ask you a possibly naiive question?WHen you say "case when email like '%@acme-holdings%' then 'Not allowed' else email end" How do you apply this? Is is part of the trigger or something else. If i am not being too cheeky could you give me an idea of the actual syntax?best regardsICW |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-03-14 : 05:37:53
|
| Something like thiscreate trigger validate on yourTable for insertasupdate t1 set email=case when I.email like '%@acme-holdings%' then 'Not allowed' else I.email endfrom yourTable t1 inner join inserted I on t1.PrimaryKeycol=I.PrimaryKeycolMadhivananFailing to plan is Planning to fail |
 |
|
|
icw
Constraint Violating Yak Guru
378 Posts |
Posted - 2006-03-14 : 06:18:07
|
| Thanks a lot for the guidance;I have just created and added the actual trigger (see below)---------------------------Use GoldMineGOcreate trigger validate_email on Contsupp for insert,updateasupdate Contsupp set contsupref=case when I.contsupref like '%@acme-holdings%' then 'Not@allowed' else I.contsupref endfrom contsupp cs inner join inserted I on cs.accountno=I.accountno----------------------------But I think I may have got something wrong because I have just added an acme-holdings email address and it wasn't changed.TIAICW |
 |
|
|
icw
Constraint Violating Yak Guru
378 Posts |
Posted - 2006-03-14 : 06:22:46
|
| SOrry My Mistake.It appeared that the email address stayed but when I scrolled off the record and back again it said 'Not@allowed'Thanks AgainICW |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-03-14 : 06:32:13
|
| Make sure that accountno is unique/Primary Key columnMadhivananFailing to plan is Planning to fail |
 |
|
|
icw
Constraint Violating Yak Guru
378 Posts |
Posted - 2006-03-15 : 06:59:19
|
| Hello,I have my trigger working now but I also want to insert a row into a new table from the pseudo 'inserted' table. I know the syntax to do that, but can't quite figure out where to put it in?Use GoldMineGOcreate trigger validate_email on Contsupp for insert,updateasupdate Contsupp set contsupref=case when I.contsupref like '%@acme holdings%' then 'Not@allowed' else I.contsupref endfrom contsupp cs inner join inserted I on cs.accountno=I.accountnonew syntax to be addedInsert Into Email_validate (accountno, contsupref, Lastuser, lastdate, recid)Select accountno, contsupref, lastuser, lastdate, recidFROM insertedthanks if u can help |
 |
|
|
icw
Constraint Violating Yak Guru
378 Posts |
Posted - 2006-03-15 : 07:07:25
|
| I'm now answering my own questions doh!!I used this and it seems to work ok.Use GoldMineGOcreate trigger validate_email on Contsupp for insert,updateasupdate Contsupp set contsupref=case when I.contsupref like '%@acme%' then 'Not@allowed' else I.contsupref endfrom contsupp cs inner join inserted I on cs.accountno=I.accountnoInsert Into Email_validate (accountno, contsupref, Lastuser, lastdate, recid)Select accountno, contsupref, lastuser, lastdate, recidFROM insertedWhere Inserted.contsupref like '%@acme%'GO |
 |
|
|
icw
Constraint Violating Yak Guru
378 Posts |
Posted - 2006-03-15 : 07:12:48
|
| How about one more thing!!How about;Xp_sendmail ‘admin@acme.com’, ‘Someone tried to add an acme email address again! Please run your report for details’I am pretty sure I have to have SQL mail configured for this...is that right?Also where can I insert this one (as I have never used it before) |
 |
|
|
|