question

sheela avatar image
sheela asked

How to create a trigger for a case

Create a trigger named 'afinsert_assetlog' on asset_log table that is

triggered whenever a new record is inserted into asset_log table.

This trigger will insert the asset_count and asset_action into asset_audit table.

The action name in the affected log table asset_audit is 'New Record Inserted'.

sql-server-2008
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

anthony.green avatar image
anthony.green answered

Lookup the syntax for "CREATE TRIGGER", you will need to put the business logic in the trigger to put the required aggregations/data etc into the audit table.


If you struggle post back with what you have tried along with sample data and schema etc and then we know what your working with and can help further

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

sheela avatar image
sheela answered

create trigger afinsert_assetlog
SET asset_count = (SELECT count(id) FROM asset_log where name = );

THEN


AFTER INSERT


ON asset_log FOR EACH ROW
BEGIN
insert into asset_audit values(:new.asset_count, 'New Record Inserted'); End;



note not fully derived

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

sheela avatar image
sheela answered

TABLE:asset_logid intcreated_date DATElast_checkedout_date DATEexpected_checkdkin_date DATEstatus VARCHARlocation_id INTasset_id INTcheckoedout_to INTcreate_user INT
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

sheela avatar image
sheela answered

TABLE:asset_logid intcreated_date DATElast_checkedout_date DATEexpected_checkdkin_date DATEstatus VARCHARlocation_id INTasset_id INTcheckoedout_to INTcreate_user INT
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

anthony.green avatar image
anthony.green answered

What language are you writing in?


SQL Server - T-SQL will be

CREATE TRIGGER afinsert_assetlog
ON asset_log
AFTER INSERT
AS
BEGIN
/*
DO YOUR LOGIC HERE
*/
END
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

sheela avatar image
sheela answered

Tried this

CREATE TRIGGER afinsert_assetlog
AFTER INSERT ON asset_log
FOR EACH ROW
BEGIN
set asset_count = (select count(id) from asset_log where checkedout_to = new.checkedout_to)
insert into asset_audit(asset_count ,asset_action)
VALUES( asset_count, 'New Record Inserted');
end ;


error:


getting error as "Msg 102, Level 15, State 1, Server ip-172-31-35-13, Procedure afinsert_assetlog, Line 3 Incorrect syntax near 'AFTER'. Msg 102, Level 15, State 1, Server ip-172-31-35-13, Procedure afinsert_assetlog, Line 6 Incorrect syntax near '='.,

CREATE TRIGGER afinsert_assetlog

AFTER INSERT ON asset_log

FOR EACH ROW

BEGIN

set asset_count = (select count(id) from asset_log where checkedout_to = new.checkedout_to)

insert into asset_audit(asset_count ,asset_action)

VALUES( asset_count, 'New Record Inserted');

end ;

go



GETTING AN ERROR AS

Msg 102, Level 15, State 1, Server ip-172-31-35-13, Procedure afinsert_assetlog,

Line 3

Incorrect syntax near 'AFTER'.

Msg 102, Level 15, State 1, Server ip-172-31-35-13, Procedure afinsert_assetlog,

Line 6

Incorrect syntax near '='.




10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.