How do I use where clause in the sql trigger?

sam s

I am trying to create a trigger where the account balance is automatically updated from the transaction table, but I am not being able to update a particular table. My trigger changes the account balance of every customer. I want to know how I can use where clause to make it customer-specific. Here is what I have:

create trigger trig_one
on dbo.transactions
for insert, update, delete
as begin
    declare @amount int
    select @amount = (select amount from inserted)

    update dbo.account set balance = balance + @amount

end

Please help me out. Thanks!

Gordon Linoff

You structure the query like this:

update dbo.account
    set balance = balance + i.amount
    from dbo.account a join
         inserted i
         on i.accountid = a.accountid ;

(The join key is whatever the join key should be.)

Of course, you need to do the same thing for the deleted. Note: this will work for multiple rows being inserted. That is handy. Your version will fail unexpectedly.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I use the WHERE LIKE SQL clause?

How do I use the WHERE clause to query data between two date ranges? - SQL

How do I use an arithmetic expression in the where clause of a SQL query that has a Group By clause without getting arithmetic overflow?

How do you use sql selected data in where/and clause

How do I use the `group` in where clause for the comparison in following query?

How do i use JOIN with a get_where clause in CodeIgniter

How can i use SQL where clause with multiple parameters

How can I use WHERE clause in this SQL query?

How do I properly input an sql where clause in VBA?

How can I do this SQL query (aggregate group by and where in clause)?

SQL how do I add 3 or more conditions into the WHERE clause?

How do I create a variable/parameter that is a string of values in SQL SSMS that I can use as a substitute in my where clause?

How do I use a LIKE or CONTAINS clause in SQL on the same row?

How to use multiple And conditions in SQL Where clause

How to use sum in where clause in SQL?

how to use Alias Name in where clause in SQL

how to use like and substring in where clause in sql

How to use alias in where clause in SQL Server

How can i use IFNULL in Where clause

How i can use If Else in where clause

How to do a conditional statement in SQL where clause

How do I add criteria to a WHERE clause?

How to use OR in this WHERE clause?

How to use the WITH clause in an SQLite trigger

How do i use to the input value in the form in the where clause of my model using codeigniter

How do I tell Dapper to use varchar for a list of params in a "WHERE" clause that uses "IN"?

How do I use multiple values from the same column in the WHERE clause?

How do I use a WHERE clause on a user-defined variable from a CASE WHEN?

How I can extract year from a date in sql and use it in where clause