Monday, August 10, 2009

How to use Trriger in ASP.net

iam working in asp.net 2.0.sqlserver 2005
iam having two database tables following

1) ProductEntry

2)ProductTotal.

ProductEntry fields ---- Pid , Pname,Quantity.

ProductTotal fields ---- Pid ,Pname ,Quantity.


While inserting ProductEntry table . Trigger shoud check ProductTotal table pid is available then quantity shoud be updated. Else . It should insert in Product total table.

i am trying this one. But i dont know . How to execute. help me


create tigger check on ProductEntry
for insert
as

if (how to check )
begin
{
update query
}
end
else
begin
{
insert
}
end



Triggers are commonly used to:
  • prevent changes (e.g. prevent an invoice from being changed after it's been mailed out)
  • log changes (e.g. keep a copy of the old data)
  • audit changes (e.g. keep a log of the users and roles involved in changes)
  • enhance changes (e.g. ensure that every change to a record is time-stamped by the server's clock, not the client's)
  • enforce business rules (e.g. require that every invoice have at least one line item)
  • execute business rules (e.g. notify a manager every time an employee's bank account number changes)
  • replicate data (e.g. store a record of every change, to be shipped to another database later)
  • enhance performance (e.g. update the account balance after every detail transaction, for faster queries)
Some systems also support non-data triggers, which fire in response to Data Definition Language (DDL) events such as creating tables, or runtime events such as logon, commit, and rollback, and may also be used for auditing purposes.
The major features of database triggers, and their effects, are:
  • do not accept parameters or arguments (but may store affected-data in temporary tables)
  • cannot perform commit or rollback operations because they are part of the triggering SQL statement (only through autonomous transactions)
  • can cancel a requested operation
  • can cause mutating table errors, if they are poorly written