Hi Claudia
There's a lot of details here. I suggest to read all the BOL info on
triggers again.
I think your syntax is close to being correct, but without more information
about your table and your processes, it's hard to know for sure how to get
the behavior you want.
COLUMNS_UPDATED is a bitmap and can be useful for checking if a set of
columns has been specified in the UPDATE clause. Since you are only
interested in a single column, COLUMNS_UPDATED shouldn't be needed. Instead,
take a look at IF UPDATE(TarType)
IF UPDATE will be true if the column (TarType) was specified in the SET
clause of your UPDATE, but it doesn't guarantee that the value changed.
You'll need to also look at the virtual table called 'deleted', which holds
the old value, and compare it to 'inserted' which holds the new values, to
see if the value of TarType changed.
Checking IF EXISTS (SELECT 1 FROM INSERTED) will tell you if there are any
rows in INSERTED. You need to be aware that a trigger will fire no matter
how many rows are affected by your update. What do you want to do if 100 or
1000 rows were updated? We need more info about your processes. Are you
expected many rows to be updated at once? It's much easier to write triggers
to handle only a single row. You can check @@rowcount right when the trigger
starts to determine how many rows were affected. You'll need code to compare
the old (deleted) with the new (inserted) value for each changed row,
hopefully based on the primary key, which you haven't specified. So we need
more info about your table(s).
BEGIN/END, BEGIN TRAN/COMMIT TRAN, and GO are 3 completely different things.
BEGIN/END is a syntactic construct, used to group statements together, like
after an IF. All the statements to be executed if the IF condition is TRUE
will be grouped by BEGIN / END.
BEGIN TRAN/COMMIT TRAN controls the work being done and enforces certain
conditions, in particular, that all the modifications between the BEGIN TRAN
and COMMIT are treated as a unit; they either all succeed or none of them
do. By definition, triggers are ALWAYS part of the same transaction that the
original data modification statement is part of .. in your case, the UPDATE.
So no BEGIN TRAN/COMMIT is needed. You can however include ROLLBACK if you
want to cancel and rollback the operation that fired the trigger. There is a
lot more information you can read about transactions in Books Online.
GO is not a SQL command at all. It is a directive to the query tool that a
group of statements will be sent from your tool (Query Analyzer? You didn't
say what version you are using) to the SQL Server to be processed as a
batch. It marks one communication from the client tool to your SQL Server.
Please read more about batches in your Books Online.
I tried writing some code to give you a start, but realized I'm not at all
clear on what you are trying to do. Can you provide some sample data and
show us what actions you need to take under what condition?
--
HTH
Kalen Delaney, SQL Server MVP
http://sqlblog.com [quoted text, click to view] "Claudia" <Claudia@discussions.microsoft.com> wrote in message
news:5442A511-07BD-4FD3-8ACE-C2C2A5B134E3@microsoft.com...
> To all, I thank you in advance for your help.
>
> I am quite new to triggers.
>
> 1. I want to see if my grammar is correct.
> 2. I want to ONLY take action based on a column "TarType" column in the
> Cmd
> table is the item modified. I saw a BOL article about "COLUMNS_UPDATED".
> Is
> this what I need to modify my trigger?
> 3. Does the BEGIN and END replace BEGIN TRANS in a trigger? Or do I need
> both? (I see "GO" statement in Microsoft's BOL, so I'm confused a bit on
> which to use).
> ------------------------
> CREATE TRIGGER Upd_ThenInsValTracking
> ON Cmd
> AFTER UPDATE
> AS
> IF EXISTS(SELECT 1 FROM INSERTED) BEGIN
> SET NOCOUNT ON;
> DECLARE @ClientID int, @CID int
> set @ClientID = (SELECT Cmpid from inserted)
> -- reach over and get an ID the audit(Tracking) table needs
> SET @CID = (SELECT CmpClientID from CmpMainInfo WHERE CmpID = @ClientID)
>
> INSERT INTO TargetInfoHistory
> (TarType, CmpClientID)
> SELECT TarType, @CID
> FROM inserted
> -- something like "WHERE TarType <> TarType.inserted"
> ------------------------
>
>
> RELATED LINK:
> COLUMNS_UPDATED (Transact-SQL)
>
http://msdn2.microsoft.com/en-us/library/ms186329.aspx >
> CREATE TRIGGER (Transact-SQL) - help file
> ms-help://MS.SQLCC.v9/MS.SQLSVR.v9.en/tsqlref9/html/edeced03-decd-44c3-8c74-2c02f801d3e7.htm