| Author |
Topic |
|
rstout
Starting Member
25 Posts |
Posted - 2005-03-18 : 18:43:47
|
| I have a table Test with 3 columes, c1, c2 and 3cI need to make a trigger when a recored is added to only 3c it is copied to c1It will be happening an append |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2005-03-18 : 18:46:48
|
| So how far did you get? Did you look up CREATE TRIGGER in SQL Server Books Online?Tara |
 |
|
|
rstout
Starting Member
25 Posts |
Posted - 2005-03-18 : 19:10:38
|
| Yea but didn't understand a thing. I am not sure how to know what record was added or updated to modify |
 |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2005-03-18 : 19:34:50
|
You should read the topic "Using the inserted and deleted Tables" in BOL.quote: Originally posted by rstout Yea but didn't understand a thing. I am not sure how to know what record was added or updated to modify
CODO ERGO SUM |
 |
|
|
rstout
Starting Member
25 Posts |
Posted - 2005-03-28 : 21:34:54
|
| OK.. Yes I understand how to add the trigger but how do I know the record that was added? Is that implied in the trigger?This would be the upate stringUPDATE test SET test.test2 = test.test1 |
 |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2005-03-28 : 21:45:18
|
As I said before, you should read the topic "Using the inserted and deleted Tables" in SQL Server Books Online.quote: Originally posted by rstout OK.. Yes I understand how to add the trigger but how do I know the record that was added? Is that implied in the trigger?This would be the upate stringUPDATE test SET test.test2 = test.test1
CODO ERGO SUM |
 |
|
|
rstout
Starting Member
25 Posts |
Posted - 2005-03-28 : 23:33:46
|
| OK I read it and still have some questions. There is no key in the table so how do I determaind the acual record to update? ON i.title_id = t.title_id WHERE t.title_id = i.title_idhacked up sample code....CREATE TRIGGER Copy_Test2_to_Test1_TriggerON testINSTEAD OF updateASIF @@rowcount = 0 RETURNIF UPDATE(title_id) BEGIN PRINT 'updates to primary key title_id are not allowed' RETURNENDIF UPDATE (Test2) BEGIN IF i.test1 !<> '' BEGIN PRINT 'Do not update Test1' RETURN END UPDATE t SET Test1 = i.Test2 FROM Test t join inserted i ON i.title_id = t.title_id WHERE t.title_id = i.title_id ENDRETURN |
 |
|
|
rstout
Starting Member
25 Posts |
Posted - 2005-03-29 : 00:10:32
|
| Got more info....SC, SN, and DY are the keys in the table named ATTThe field that I am monitoring is A1 and want it copied to AL if AL is not empyt or Null AL and A1 are valcharSC,DY are smallintSN is inthere is the new triggerCREATE TRIGGER Copy_A1_to_AL_TriggerON ATTINSTEAD OF updateASIF @@rowcount = 0 RETURNIF UPDATE (A1) BEGIN IF i.AL != '' BEGIN PRINT 'Do not update.' RETURN END UPDATE t SET AL = i.A1 FROM ATT t join inserted i ON t.SC = i.SC AND t.SN = i.SN AND t.DY = i.DY WHERE t.SC = i.SC AND t.SN = i.SN AND t.DY = i.DY ENDRETURNI get the following error in query anylzerServer: Msg 107, Level 16, State 2, Procedure Copy_A1_to_AL_Trigger, Line 10The column prefix 'i' does not match with a table name or alias name used in the query. |
 |
|
|
jen
Master Smack Fu Yak Hacker
4110 Posts |
Posted - 2005-03-29 : 01:09:54
|
| try using inserted instead of the alias in your joinHTH--------------------keeping it simple... |
 |
|
|
rstout
Starting Member
25 Posts |
Posted - 2005-03-29 : 13:02:47
|
| I am still new at this so... you are saying to have it like this?IF inserted.AL != '' |
 |
|
|
Bustaz Kool
Master Smack Fu Yak Hacker
1834 Posts |
Posted - 2005-03-29 : 16:06:00
|
| >>> There is no key in the table <<<You have bigger issues than how to write a trigger.HTH=================================================================In order to improve the mind, we ought less to learn than to contemplate.-Rene Descartes, philosopher and mathematician (1596-1650) |
 |
|
|
rstout
Starting Member
25 Posts |
Posted - 2005-03-29 : 22:49:24
|
| the key thing is not a problem any morenow it is just the problem above |
 |
|
|
|