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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 beginner in triggers

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
- customers

i 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

Posted - 2009-06-12 : 07:24:52
Go through this u get an idea..

http://www.devarticles.com/c/a/SQL-Server/Using-Triggers-In-MS-SQL-Server/1/


Senthil.C
------------------------------------------------------
[Microsoft][ODBC SQL Server Driver]Operation canceled
Go to Top of Page

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
- customers

i 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 like

CREATE TRIGGER YourTrigger
ON customers
AFTER INSERT
AS
BEGIN
INSERT INTO article (fields..)
SELECT customerno,'0',...
FROM INSERTED
END
Go to Top of Page

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?
Go to Top of Page

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
Go to Top of Page

chriztoph
Posting Yak Master

184 Posts

Posted - 2009-06-14 : 07:02:39
how about creating a trigger with separate database?

example:

sampletable on sampledatabase

and the audit will be saved in

audit_sampletable on auditsampledatabase
Go to Top of Page

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 sampledatabase

and the audit will be saved in

audit_sampletable on auditsampledatabase


yup. you can create a trigger to populate a table in another database as long as its on the same server.
Go to Top of Page

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 delete

on 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, delete
as
declare @inserted int,
@deleted int,
@type char(6)
select @inserted = count(*) from inserted
select @deleted = count(*) from deleted
set @type = case
when (@inserted > 0) and (@deleted = 0)then 'insert'
when (@inserted > 0) and (@deleted > 0)then 'update'
when (@inserted = 0) and (@deleted > 0)then 'delete'
end
if (@type IN ('insert','update'))
insert into Audit_dbECI.CustomerDetails_Audit
select Customer_ID, current_timestamp, @type , Host_Name()
from inserted
else
insert into Audit_dbECI.CustomerDetails_Audit
select Customer_ID, current_timestamp, @type , Host_Name()
from deleted
return



but every time to execute an Insert,update and delete in table CustomerDetails i get this error


Msg 208, Level 16, State 1, Procedure TRG_CustomerDetails, Line 16
Invalid object name 'Audit_dbECI.CustomerDetails_Audit'.



Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-06-15 : 06:18:47
databasename.schemaname.tablename
maybe: Audit_dbECI..CustomerDetails_Audit
or : Audit_dbECI.dbo.CustomerDetails_Audit


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

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 delete

on 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, delete
as
declare @inserted int,
@deleted int,
@type char(6)
select @inserted = count(*) from inserted
select @deleted = count(*) from deleted
set @type = case
when (@inserted > 0) and (@deleted = 0)then 'insert'
when (@inserted > 0) and (@deleted > 0)then 'update'
when (@inserted = 0) and (@deleted > 0)then 'delete'
end
if (@type IN ('insert','update'))
insert into Audit_dbECI.CustomerDetails_Audit
select Customer_ID, current_timestamp, @type , Host_Name()
from inserted
else
insert into Audit_dbECI.CustomerDetails_Audit
select Customer_ID, current_timestamp, @type , Host_Name()
from deleted
return



but every time to execute an Insert,update and delete in table CustomerDetails i get this error


Msg 208, Level 16, State 1, Procedure TRG_CustomerDetails, Line 16
Invalid object name 'Audit_dbECI.CustomerDetails_Audit'.






your trigger doesn't make sense... can i ask whats your actual requirement?
Go to Top of Page

chriztoph
Posting Yak Master

184 Posts

Posted - 2009-06-16 : 01:08:49
@webfred
i tried already that but it doesn't work..


@visakh16
Just 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..
Go to Top of Page

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'
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2009-06-16 : 04:36:35
I think Webfred is correct:
quote:

databasename.schemaname.tablename
maybe: Audit_dbECI..CustomerDetails_Audit
or : 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 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

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
Go to Top of Page

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.
Go to Top of Page
   

- Advertisement -