Hello, everyone
I need to create a trigger to update a FLAG column by row modification. The table likes,
Col1 FLAG
a 0
b 0
c 0
d 0
e 0
If "a" in Col1 is changed, the "0" in same row (First row) should be changed to '1'. Other FLAG values should not be changed. The same rule for other row.
Anyhelp will be approciated.
Thanks a lot
ZYTIf col1 is a primary key, this is relatively easy. If you don't have a primary key, I don't know how to do it. Can you "fill in the blanks" a bit on your requirements?
-PatP|||Col1 doesn't have to be a primary key, but you do need a primary key. Put this in your trigger:
Update YourTable
set FLAG = 1
from YourTable
inner join Deleted on YourTable.PrimaryKey = Deleted.PrimaryKey
where YourTable.Col1 <> Deleted.Col1|||So what happens when the code that launches the trigger is something like:UPDATE yourtable SET col1 = 'a'All of the rows except for one change. Without a PK, things get ugly really fast from a SQL perspective, and I'd guess that the business rules would go berzerk!
-PatP|||All of the rows except for one change.-PatP
I think that is what he would want. All of the rows except one were modified, so set the flags on all but one record.
But yes, as I mentioned in my post, he is going to need a primary key on the table.
Friday, March 9, 2012
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment