| Author |
Topic |
|
Studevs
Starting Member
15 Posts |
Posted - 2009-06-12 : 07:05:42
|
| Hi All,Im verry new in this and hope you guys help me.i have to tables:- articles- customersi want to write a trigger: when inserting a new record to the table 'customers', a new record most be added into the 'articles' too. the new record in the 'articles' most have the new added 'customernr' and '0' .Thanks |
|
|
senthil_nagore
Master Smack Fu Yak Hacker
1007 Posts |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-06-14 : 01:55:25
|
quote: Originally posted by Studevs Hi All,Im verry new in this and hope you guys help me.i have to tables:- articles- customersi want to write a trigger: when inserting a new record to the table 'customers', a new record most be added into the 'articles' too. the new record in the 'articles' most have the new added 'customernr' and '0' .Thanks
something likeCREATE TRIGGER YourTriggerON customersAFTER INSERTASBEGININSERT INTO article (fields..)SELECT customerno,'0',...FROM INSERTEDEND |
 |
|
|
chriztoph
Posting Yak Master
184 Posts |
Posted - 2009-06-14 : 02:39:37
|
| is it possible to create a trigger for multiple table?if yes,how? |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-06-14 : 02:58:05
|
quote: Originally posted by chriztoph is it possible to create a trigger for multiple table?if yes,how?
nope. a trigger is created on a table. so if you want to perform trigger actions for multiple table, you need a separate trigger on each |
 |
|
|
chriztoph
Posting Yak Master
184 Posts |
Posted - 2009-06-14 : 07:02:39
|
| how about creating a trigger with separate database?example:sampletable on sampledatabaseand the audit will be saved inaudit_sampletable on auditsampledatabase |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-06-14 : 14:44:36
|
quote: Originally posted by chriztoph how about creating a trigger with separate database?example:sampletable on sampledatabaseand the audit will be saved inaudit_sampletable on auditsampledatabase
yup. you can create a trigger to populate a table in another database as long as its on the same server. |
 |
|
|
chriztoph
Posting Yak Master
184 Posts |
Posted - 2009-06-15 : 06:16:00
|
how?can you give me sample trigger for that?i have a trigger here for insert, update and deleteon database 'dbECI' with a table 'CustomerDetails'and 'Audit_dbECI.CustomerDetails_Audit' here is the database and table CREATE trigger [TRG_CustomerDetails] on [dbo].[CustomerDetails]for insert, update, deleteasdeclare @inserted int,@deleted int,@type char(6)select @inserted = count(*) from insertedselect @deleted = count(*) from deletedset @type = casewhen (@inserted > 0) and (@deleted = 0)then 'insert'when (@inserted > 0) and (@deleted > 0)then 'update'when (@inserted = 0) and (@deleted > 0)then 'delete'endif (@type IN ('insert','update'))insert into Audit_dbECI.CustomerDetails_Auditselect Customer_ID, current_timestamp, @type , Host_Name()from insertedelseinsert into Audit_dbECI.CustomerDetails_Auditselect Customer_ID, current_timestamp, @type , Host_Name()from deletedreturnbut every time to execute an Insert,update and delete in table CustomerDetails i get this errorMsg 208, Level 16, State 1, Procedure TRG_CustomerDetails, Line 16Invalid object name 'Audit_dbECI.CustomerDetails_Audit'. |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2009-06-15 : 06:18:47
|
databasename.schemaname.tablenamemaybe: Audit_dbECI..CustomerDetails_Auditor : Audit_dbECI.dbo.CustomerDetails_Audit No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-06-15 : 13:46:30
|
quote: Originally posted by chriztoph how?can you give me sample trigger for that?i have a trigger here for insert, update and deleteon database 'dbECI' with a table 'CustomerDetails'and 'Audit_dbECI.CustomerDetails_Audit' here is the database and table CREATE trigger [TRG_CustomerDetails] on [dbo].[CustomerDetails]for insert, update, deleteasdeclare @inserted int,@deleted int,@type char(6)select @inserted = count(*) from insertedselect @deleted = count(*) from deletedset @type = casewhen (@inserted > 0) and (@deleted = 0)then 'insert'when (@inserted > 0) and (@deleted > 0)then 'update'when (@inserted = 0) and (@deleted > 0)then 'delete'endif (@type IN ('insert','update'))insert into Audit_dbECI.CustomerDetails_Auditselect Customer_ID, current_timestamp, @type , Host_Name()from insertedelseinsert into Audit_dbECI.CustomerDetails_Auditselect Customer_ID, current_timestamp, @type , Host_Name()from deletedreturnbut every time to execute an Insert,update and delete in table CustomerDetails i get this errorMsg 208, Level 16, State 1, Procedure TRG_CustomerDetails, Line 16Invalid object name 'Audit_dbECI.CustomerDetails_Audit'.
your trigger doesn't make sense... can i ask whats your actual requirement? |
 |
|
|
chriztoph
Posting Yak Master
184 Posts |
Posted - 2009-06-16 : 01:08:49
|
| @webfredi tried already that but it doesn't work..@visakh16Just the transaction type(Insert/Update/delete),the computername(the host_name(or 'Domain Account',if possible)), the time modified and the recordID..it is just a simple trigger i need to monitor the inserting,updating and deleting of records in a table but the Audit database is separate.. |
 |
|
|
chriztoph
Posting Yak Master
184 Posts |
Posted - 2009-06-16 : 01:20:31
|
| but the trigger i posted is working if the trigger set up like this on'sampletable on sampledatabase'the auditing will be saved on table'audit_sampletable on sampledatabase' |
 |
|
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2009-06-16 : 04:36:35
|
I think Webfred is correct: quote: databasename.schemaname.tablenamemaybe: Audit_dbECI..CustomerDetails_Auditor : Audit_dbECI.dbo.CustomerDetails_Audit
If you open a query window on the real database can you confirm that you can actually SELECT from the audit table in the audit database?As he said [dbname.schema.table] or [dbname..table]Can you confirm that you can actually see that table from the real database by SELECT * FROM <x.y.z>Charlie===============================================================Msg 3903, Level 16, State 1, Line 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
 |
|
|
chriztoph
Posting Yak Master
184 Posts |
Posted - 2009-06-16 : 05:12:03
|
| Thanks Charlie,Webfred is correct..now it is working..this helped me:Audit_dbECI..CustomerDetails_Audit |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2009-06-16 : 06:55:31
|
fine  No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
|