?
Solved

Update Trigger Sql

Posted on 2014-11-05
8
Medium Priority
?
223 Views
Last Modified: 2014-11-06
We have a trigger that needs to fire on one single column when it is updated.   The trigger doesn't fire at all...below is the code:
alter TRIGGER [dbo].[Ens_UpConversion]
   ON  [dbo].[ens_phyinv]
    after  UPDATE

AS BEGIN

      -- SET NOCOUNT ON added to prevent extra result sets from
      -- interfering with SELECT statements.
      SET NOCOUNT ON;

            declare @item       nvarchar(30),
                        @rowpointer nvarchar(60),
                        @cnt        decimal(18,6)

      if update(count_qty)
       begin

            declare cntstat cursor local static for

             select item, rowpointer,count_qty
             from inserted  

             open cntstat

              Fetch next from cntstat into @item,@rowpointer,@cnt  
              while @@fetch_status = 0
          begin
                   if ((substring(@item,4,1) in ('1','3') and @cnt <> 0) and ((select us_length from ens_dimmain where item = @item) is not null and (select us_length from ens_dimmain where item = @item) <> 0))
                     begin      
                           begin tran  
                              update ensp
                                    set cal_count = @cnt * us_length / 12
                                    from ens_phyinv ensp
                                      inner join ens_dimmain d on d.item = ensp.item
                                    where ensp.rowpointer = @rowpointer
                                    and ensp.item = @item
                                    if @@error <> 0
                                       commit tran
                                    else
                                      rollback tran
                        end /*if substring*/
              Fetch next from cntstat into @item,@rowpointer,@cnt  
                  end  /*cursor*/
                  close cntstat
                  deallocate cntstat

       end /*if update(count_qty)*/

END
0
Comment
Question by:steven
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 35

Expert Comment

by:ste5an
ID: 40424269
How have you confirmed that it is not firing? Is it disabled?

btw, why do you use an cursor?? This should be sufficient:

ALTER TRIGGER [dbo].[Ens_UpConversion] ON [dbo].[ens_phyinv]
    AFTER UPDATE
AS
    SET NOCOUNT ON;

    IF UPDATE(count_qty)
        BEGIN 
            WITH    R AS ( SELECT   I.item ,
                                    I.rowpointer ,
                                    I.count_qty
                           FROM     INSERTED I
                           WHERE    ( SUBSTRING(I.item, 4, 1) IN ( '1', '3' )
                                      AND I.count_qty <> 0
                                      AND ( SELECT  us_length
                                            FROM    ens_dimmain
                                            WHERE   item = @item
                                          ) IS NOT NULL
                                      AND ( SELECT  us_length
                                            FROM    ens_dimmain
                                            WHERE   item = @item
                                          ) <> 0
                                    )
                         )
                UPDATE  ensp
                SET     cal_count = R.count_qty * us_length / 12
                FROM    ens_phyinv ensp
                        INNER JOIN ens_dimmain d ON d.item = ensp.item
                        INNER JOIN R ON ensp.rowpointer = R.rowpointer
                                        AND ensp.item = R.item;        
        END;

Open in new window

0
 
LVL 40

Expert Comment

by:lcohan
ID: 40424270
How do you know it actually does not fire?
You could put a statement in there to raiseerror and see if that happens instead of the cursor code.
Also I would definitely NOT recommend using explicit transactions in SQL and in particular in triggers which are hidden SQL code.
Use BEGIN/TRY/CATCH instead if you really need to do that.

http://blog.sqlauthority.com/2007/04/11/sql-server-2005-explanation-of-trycatch-and-error-handling/
http://www.mssqltips.com/sqlservertip/1027/sql-server-2005-try-and-catch-exception-handling/
0
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 2000 total points
ID: 40424313
The trigger is firing, unless it's disabled.  But explicitly committing a transaction inside a trigger could cause all kinds of issues, since you're already in a transaction when the trigger starts, and SQL Server doesn't have true embedded transactions.

Cursors should be avoided if at all possible, especially inside of triggers.

Try this code instead:


ALTER TRIGGER [dbo].[Ens_UpConversion]
    ON  [dbo].[ens_phyinv]
    AFTER UPDATE
 AS
   SET NOCOUNT ON;

   if update(count_qty)
   begin

        update ensp
             set cal_count = ins.count_qty * d.us_length / 12
             from inserted ins
               inner join deleted del on del.item = ins.item and del.rowpointer = ins.rowpointer
               inner join ens_phyinv ensp on ensp.rowpointer = ins.rowpointer and ensp.item = ins.item
               inner join ens_dimmain d on d.item = ins.item
             where
               --verify that *this* row's count_qty changed before processing it
               isnull(ins.count_qty,-555) <> isnull(del.count_qty,-555) and
               substring(ins.item,4,1) in ('1','3') and
               ins.count_qty <> 0 and
               d.us_length <> 0

   end --if
 GO --end of trigger code
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

Author Comment

by:steven
ID: 40424459
I am not sure why the trigger, I like the way you have it, and we created a Table in the db and tried to insert into it, a simple insert 'One record'.   That table has nothing in it .
0
 

Author Comment

by:steven
ID: 40424968
After SET NOCOUNT ON; we place insert into testable(item) and a simple select 'test1' .   We figured that the table should at least have one value in it, but no luck nothing inserted.  We also had the insert after the IF update(count_qty), hoping that it would insert, still with no luck...
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 40425031
The trigger only specifies "AFTER UPDATE".

If you want it to run after INSERTs too, you'd have to code it "AFTER INSERT, UPDATE" and change the logic to match that (since a deleted row will not be available for an INSERT).
0
 

Author Comment

by:steven
ID: 40426068
I will try that thank you
0
 

Author Closing Comment

by:steven
ID: 40426869
Thanks so much this worked!
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Suggested Courses

741 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question