Solved

sql server tempdb trigger not firing based on user/process

Posted on 2012-03-30
8
436 Views
Last Modified: 2012-06-27
Hi -
I have a AFTER INSERT/UPDATE trigger on a table in TempDB (to be explained further down)
which does not fire/execute when a record is inserted or updated from "applications".

However, when I go into SSMS and update a record I can watch it execute in a profiler trace without error.

Is there any obvious reason for this I am overlooking ?
Google has not been my friend so far on this search....


Details:
----------------------
This is a local application running SQLEXPRESS and using tempdb for speed due to many, many singluar and batch INSERTS.

I tried/and thought maybe something to do with creating the trigger "with execute as owner" but that does not seem to be the issue.

Additionally - the trigger is doing an INSERT/UPDATE into a table in another database (same instance) - again, this works fine if I execute it in SSMS.

In this scenario - I happen to be logged into SSMS and the database as domain\administrator - and the application is running as that user as well.
My assumption being, for testing - that indeed both processes are running as same user, eliminating permissions as the reason....
0
Comment
Question by:bbaley
[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
  • 4
  • 3
8 Comments
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 37788892
Have you verified that the trigger is really not firing?

Such as put a non-conditioned INSERT to a dummy/trace as the *first* thing in the trigger to see if the trigger was entered.

Maybe there is just an error in the INSERT/UPDATE statement when the app fires the trigger (?).
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 37788900
The trigger code itself would be extremely helpful.

Also, certain types of statements may not fire triggers, such as BULK INSERTs.  Verify that your app isn't using that type of statement.  Not likely but I mention it just to be thorough.
0
 

Author Comment

by:bbaley
ID: 37794127
Scott,
I am running SQL Profiler trace (with SP:StatementCompleted, SP:StatementStarted selected) and I see the insert or update happen, but the trigger does not show up in the trace.

Primarily I wanted to make sure there was nothing specific to TempDB I should be aware of.

I can execute all code in a test script outside the trigger to verify no errors in the executing code by replacing the INSERTED table reference with a #temp table reference, etc. (in other words I have code that puts a record into a table called #INSERTED and I replace that in the test code etc.)


here is the trigger code:
(along with some debugging stuff in there  I was trying...)
======================
USE [tempdb]
GO
IF  EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[trg_Insert_iFixLog]'))
DROP TRIGGER [dbo].[trg_Insert_iFixLog]
GO
USE [tempdb]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE TRIGGER [dbo].[trg_Insert_iFixLog]
   ON  [tempdb].[dbo].[iFix]
   --with execute as owner  
   AFTER UPDATE,INSERT
AS
BEGIN

--PRINT 'trigger fired!'

IF (UPDATE(A_ALMEXT1) or
      UPDATE(A_ALMEXT2) or
      UPDATE(A_AUTO) or
      UPDATE(A_CV) or
      UPDATE(A_DESC) or
      UPDATE(A_ENAB) or
      UPDATE(F_CV)  or
      UPDATE(A_NAME)
      )
      begin try
            --print 'begin try'
            
            SET NOCOUNT ON;

            declare @date datetime
            SET @date = GETDATE()
            
            --EXEC iFixLogging.dbo.sp_UPSERT_LogData(
            declare @tagname varchar(60)
            SET @tagname = (SELECT TagName FROM INSERTED)
            
            --print 'got tag name ' + @tagname
            
            declare @recs int
            SELECT @recs = (SELECT COUNT(*) FROM  iFixLogging.dbo.iFix_LogData WHERE TagName = @tagname)
                              
            --print 'got rec count' + STR(@recs)
                              
            IF (@recs <= 0)
            BEGIN                  
            
                  --print 'inserting!'
                  
                  INSERT INTO
                        iFixLogging.dbo.iFix_LogData (
                                                      TagIndex
                                                        ,TagName
                                                        ,A_ALMEXT1
                                                        ,A_ALMEXT2
                                                        ,A_AUTO
                                                        ,A_CV
                                                        ,A_DESC
                                                        ,A_ENAB
                                                        ,A_NAME
                                                        ,F_CV
                                                        ,LastModified)
                        (SELECT
                              TagIndex
                              ,TagName
                              ,A_ALMEXT1
                              ,A_ALMEXT2
                              ,A_AUTO
                              ,A_CV
                              ,A_DESC
                              ,A_ENAB
                              ,A_NAME
                              ,F_CV,
                              @date
                         from inserted)       
            END
            ELSE
            BEGIN
                  UPDATE i --iFix_Logging.dbo.iFix_LogData
                  SET
                  --      i.TagIndex            = u.TagIndex
                  --   ,i.TagName            = u.TagName
                     i.A_ALMEXT1            = u. A_ALMEXT1
                     ,i.A_ALMEXT2            = u.A_ALMEXT2
                     ,i.A_AUTO            = u.A_AUTO
                     ,i.A_CV                  = u.A_CV
                     ,i.A_DESC            = u.A_DESC
                     ,i.A_ENAB            = u.A_ENAB
                     ,i.A_NAME            = u.A_NAME
                     ,i.F_CV                  = u.F_CV
                     ,i.LastModified  = @date
                  FROM
                        iFixLogging.dbo.iFix_LogData i
                  INNER JOIN
                        INSERTED u
                  ON
                        --u.TagName = i.tagName and u.TagIndex = i.TagIndex
                        u.TagIndex = i.TagIndex
                        
                  --print 'inserted record'      
            END            
      end try
      begin catch
            SELECT
            ERROR_NUMBER() AS ErrorNumber,
            ERROR_SEVERITY() AS ErrorSeverity,
            ERROR_STATE() AS ErrorState,
            ERROR_PROCEDURE() AS ErrorProcedure,
            ERROR_LINE() AS ErrorLine,
            ERROR_MESSAGE() AS ErrorMessage;
            
            RAISERROR('Error in iFixLogging Trigger' ,16,1) WITH LOG
      END CATCH
END
GO
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 37798177
I know you cannot create a trigger on a temp table, but I'm pretty sure that you can add triggers to non-temp tables in the tempdb database.

The trigger should definitely do something for INSERTs.  UPDATEs would only do something if they SET one of the tested columns (IF UPDATE(...)).

Verify that table:
 iFixLogging.dbo.iFix_LogData

exists and that the user executing the trigger has authority to INSERT and UPDATE that table at least.
0
 

Author Comment

by:bbaley
ID: 37799178
the answer is;

The application that was writing to the table, was doing so in batch mode (adodb.UpdateBatch) - which does not fire triggers!

Thank you all for your input !
0
 

Author Comment

by:bbaley
ID: 37802741
I've requested that this question be closed as follows:

Accepted answer: 0 points for bbaley's comment #37799178

for the following reason:

The solution provided solves the problem
0
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 500 total points
ID: 37801543
What about my earlier comment??:

"Also, certain types of statements may not fire triggers, such as BULK INSERTs.  Verify that your app isn't using that type of statement."

That was EXACTLY what it was, right?
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 37802743
I have re-opened this question to allow you to do the right thing.
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

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…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
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.
Viewers will learn how the fundamental information of how to create a table.

738 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