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

  • Home
  • SEARCH
  • 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 4087154
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:49:10+00:00 2026-05-20T18:49:10+00:00

I am rather confused, because my trigger in SQL Server cannot insert the value

  • 0

I am rather confused, because my trigger in SQL Server cannot insert the value what I expected it would. The situation is as follows :

  • I have transaction table which can have two types of transactions in it – saldo and buy. If it is saldo, the trigger in transaction table will insert the amount of the transaction total to saldo table, but with Debit in its saldo_type field.

  • So if the case in transaction table is buy, the same amount will be inserted in saldo table, but with credit in its saldo_type field.

What confuses me is that the trigger will only insert the correct amount of value if the situation is saldo, but not if the situation is buy

What did I do wrong? Here is the code:

declare @last_saldo int
declare @transaction_ammount int

set @last_saldo = (select sum(saldo_ammount) from saldo)
if @last_saldo is null set @last_saldo=0

set @transaction_ammount = (select transaction_ammount from inserted)
IF (select transaction_type from inserted) = 'Saldo'
begin
/* this will insert correct amount */
INSERT INTO saldo
    (id_transaction,transaction_type,saldo_ammount,saldo)
SELECT id_transaction,'Debit',@transaction_ammount,@last_saldo + @transaction_ammount
FROM inserted
RETURN 
END else IF (select transaction_type from inserted)='Buy'
begin
    /* this will not insert the correct ammount. It will always zero! */
INSERT INTO saldo
    (id_transaction,transaction_type,saldo_ammount,saldo)
SELECT id_transaction,'Credit',@transction_ammount,(@last_saldo - @transaction_ammount)
FROM inserted
RETURN 
END 

Many Thanks!

  • 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-20T18:49:10+00:00Added an answer on May 20, 2026 at 6:49 pm

    Perhaps you can refactor your trigger to be a bit simpler:

    declare @last_saldo int
    
    select @last_saldo = ISNULL(sum(saldo_ammount),0)
     from saldo
    
    INSERT INTO saldo
            (id_transaction,transaction_type,saldo_ammount,saldo)
    
        SELECT id_transaction,
               CASE WHEN transaction_type = 'Saldo' 
                 THEN 'Debit'
                 ELSE 'Credit' 
               END,
               transaction_ammount,
               CASE WHEN transaction_type = 'Saldo' 
                 THEN (@last_saldo + transaction_ammount)
                 ELSE (@last_saldo - transaction_ammount)
               END
        FROM inserted
    RETURN
    

    Is the problem of zero solved with this code? If not, determine what @last_saldo and transaction_ammount values are. That’ll lead you to the root of your problem.

    Caveat: be aware that inserted can have more than one row!

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

Sidebar

Related Questions

I have a rather classic UI situation - two ListBoxes named SelectedItems and AvailableItems
I have a rather large file (150 million lines of 10 chars). I need
I'd rather not have to resort to calling the command line. I'm looking for
We have a rather simple site (minimal JS) with plain html and CSS. It
I'm learning C++, and am rather confused as to the different types of initialization.
Rather than population said DOM object with an external page such as HTML CFM
Rather than scraping a Ruby version of this algorithm off the net I wanted
A rather odd experience. Using the latest PDK (v7.3) from ActiveState, I used perlctrl
I know this rather goes against the idea of enums, but is it possible
I run a rather complex project with several independent applications. These use however a

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.