| Author |
Topic |
|
yashk
Starting Member
10 Posts |
Posted - 2009-10-15 : 15:55:32
|
| I have a situation where i have 2 tables.If any value is deleted is table 1 then it should be detected and the deleted value should go to table 2.I am saying just a value not a row or column.So should i write a trigger for delete or update? |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
yashk
Starting Member
10 Posts |
Posted - 2009-10-16 : 08:06:59
|
| Thanks for your reply.As you said it would be better to write code in stored procedure. Unfortunately i don't have an option to do so. I have to write a trigger( I am told to do so by my head)If any value is deleted in Table 1 that value should be deleted in table 1 and the deleted value should go to table 2. Is it possible to write a trigger for this situation.Even if we write a trigger how can we keep track of that deleted value in table2. If only value goes to table 2 then how can we know to which row that value belongs to. |
 |
|
|
asgast
Posting Yak Master
149 Posts |
Posted - 2009-10-16 : 09:31:55
|
| You can't delete a value in a table, only a rowYou can set a value to '' or zero depending on a column type.Can't fall asleep, the ghost of sql server will get me for writing bad queries. Have to stay awake ... |
 |
|
|
yashk
Starting Member
10 Posts |
Posted - 2009-10-16 : 09:42:51
|
| I think it is possible to delete a valueIf someone who have access to table deletes a value just by right click on the value and delete (not through code) then the value will be deleted. |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2009-10-16 : 10:41:48
|
That is not a delete in terms of sql!It is an update of a column which is replacing the old value 'bla' by nothing... No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
yashk
Starting Member
10 Posts |
Posted - 2009-10-16 : 10:52:53
|
| Yes it is an update in SQL. You are right.My task is to write a trigger so that whenever a column is deleted in Schema1.DIMCNAM( My table name) then that values should be inserted into Schema2.DIMCNAM and should be deleted in Schema1.DIMCNAM |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2009-10-16 : 11:12:21
|
So think concrete to the end.I assume there are more than one column with different data types in Schema1.DIMCNAM.So Schema2.DIMCNAM should be clone of Schema1.DIMCNAM plus any new columns to take information about datetime of changes and mybe old AND new values...Describe exactly what you want to do please because there are too many open questions to advice for a good working trigger. No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
yashk
Starting Member
10 Posts |
Posted - 2009-10-16 : 11:17:21
|
| I have two tablesSchema1.DIMCNAM and Schema2.DINCNAMBoth have same column names excpet Schema2.DIMCNAM have one extra INSERT_DT.My task is to write a trigger so that whenever a column or values in a column are deleted in Schema1.DIMCNAM( My table name) then that values should be inserted into Schema2.DIMCNAM and should be deleted in Schema1.DIMCNAM |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2009-10-16 : 11:29:44
|
So an update happens to Schema1.DIMCNAM.One column has changed from value 'webfred' to value 'yashk' so this is not a delete in your terms and there is nothing to do for the trigger?Another update happens to Schema1.DIMCNAM.One column has changed from value 'yak' to value '' or NULL and all other columns are unchanged.So what is to do for the trigger? Insert 'yak' into a new row (Schema2) in the corresponding column with actual datetime in insert_dt and all other columns (Schema2) are NULL?What if a ROW gets deleted (really deleted)? No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
yashk
Starting Member
10 Posts |
Posted - 2009-10-16 : 11:33:10
|
| Let me give you my technical spec so that it will be clear for you.There are 2 triggers that need to be setup to remove certain columns form the GDW when a delete happens in OMS.Triggered TableSchema1.DIMCNAMWhere to be insertedSchema2.DIMCNAMWe need a delete trigger to capture when a delete happens on the table DIMCNAM. When a delete happens we need to insert a record into Schema2.DIMCNAMDIMCNAMOriginal Value-----> Inserted Valuecolumn1 --------> column1column2------> column2column3 -------> 'P'column4 -------> column4column5 -------> column5 column6 --------> if column6 = 'A' or 'B' then NULL ELSE FHAFEQcolumn7 --------> if column6 = 'A' or 'B' then NULL ELSE FHAERScolumn8 --------> if column6 = 'A' or 'B' then NULL ELSE FHAFEScolumn9 --------> if column6 = 'A' or 'B' then NULL ELSE FHAFETcolumn10 --------> if column6 = 'A' or 'B' then NULL ELSE FHAFEScolumn11 --------> if column6 = 'A' or "B' then NULL ELSE FHAFEUcolumn12 --------> column12........................................Column29-------->column29This is the Specification given to me.According to the specification can anyone suggest what i have to write? |
 |
|
|
SQLTony2008
Starting Member
1 Post |
Posted - 2009-10-16 : 12:20:52
|
quote: Originally posted by yashk I have a situation where i have 2 tables.If any value is deleted is table 1 then it should be detected and the deleted value should go to table 2.I am saying just a value not a row or column.So should i write a trigger for delete or update?
You should write an update trigger.TK |
 |
|
|
|