Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3442454
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:40:14+00:00 2026-05-18T08:40:14+00:00

I’ve having trouble getting a TSQL trigger to even work correctly. I’ve run it

  • 0

I’ve having trouble getting a TSQL trigger to even work correctly. I’ve run it through the debugger and it’s not setting any of the variables according to SQL Server Management Studio. The damnedest thing is that the trigger itself is executing correctly and there are no errors when it is executed (just says ‘execution successful’).

The code is as follows (it’s a work in progress…. just getting my self familiar):

USE TestDb

IF EXISTS (SELECT name FROM sysobjects
      WHERE name = 'OfficeSalesQuotaUpdate' AND type = 'TR')
   DROP TRIGGER OfficeSalesQuotaUpdate
GO

CREATE TRIGGER OfficeSalesQuotaUpdate
ON SalesReps
AFTER UPDATE, DELETE, INSERT 
AS
    DECLARE @sales_difference int,  @quota_difference int
    DECLARE @sales_original int,    @quota_original int
    DECLARE @sales_new int,         @quota_new int

    DECLARE @officeid int
    DECLARE @salesrepid int

    --UPDATE(Sales) returns true for INSERT and UPDATE.
    --Not for DELETE though.    

    IF ((SELECT COUNT(*) FROM inserted) = 0)
        SET @salesrepid = (SELECT SalesRep FROM deleted)
    ELSE    
        SET @salesrepid = (SELECT SalesRep FROM inserted)   

    --If you address the @salesrepid variable, it does not work. Doesn't even 
    --print out the 'this should work line.
    PRINT 'This should work...' --+ convert(char(30), @salesrepid)

    IF (@salesrepid = NULL)
        PRINT 'SalesRepId is null'
    ELSE
        PRINT 'SalesRepId is not null'

    PRINT convert(char(50), @salesrepid)


    SET @officeid = (SELECT RepOffice 
                       FROM SalesReps 
                      WHERE SalesRep = @salesrepid)

    SELECT @sales_original =    (SELECT Sales FROM deleted)
    SELECT @sales_new =         (SELECT Sales FROM inserted)

    --Sales can not be null, so we'll remove this later.
    --Use this as a template for quota though, since that can be null.
    IF (@sales_new = null) 
    BEGIN
        SET @sales_new = 0
    END

    IF (@sales_original = 0)
    BEGIN
        SET @sales_original = 0
    END

    SET @sales_difference = @sales_new - @sales_original

    UPDATE Offices
    SET Sales = Sales + @sales_difference
    WHERE Offices.Office = @officeid
GO

So, any tips? I’ve completely stumped on this one. Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T08:40:15+00:00Added an answer on May 18, 2026 at 8:40 am

    Your main problem seems to be that there is a difference between @foo = NULL and @foo IS NULL:

    declare @i int
    set @i = null -- redundant, but explicit
    
    if @i = null print 'equals'
    if @i is null print 'is'
    

    The ‘This should work’ PRINT statement doesn’t work because concatenating a NULL with a string gives a NULL, and PRINT NULL doesn’t print anything.

    As for actually setting the value of @salerepid, it seems most likely that the inserted and/or deleted table is in fact empty. What statements are you using to test the trigger? And have you printed out the COUNT(*) value?

    You should also consider (if you haven’t already) what happens if someone changes more than one row at once. Your current code assumes that only one row is changed at a time, which may be a reasonable assumption in your environment, but it can easily break if someone bulk loads data or does other ‘batch processing’.

    Finally, you should always mention your MSSQL version and edition; it can be relevant for some syntax questions.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I am trying to loop through a bunch of documents I have to put
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.