Database Triggers
TechCore Mini ERP uses SQL Server triggers to automatically maintain data consistency across related tables. These triggers ensure that inventory levels and payment balances are updated in real-time without requiring manual intervention.TR_DisminuirStock
Automatically decreases product stock when sale items are added, but only for valid (non-voided) sales.Purpose
- Maintain accurate real-time inventory levels
- Prevent stock updates for voided transactions
- Eliminate manual stock adjustments after sales
- Ensure data integrity between sales and inventory
SQL Definition
How It Works
- Fires After: New records are inserted into
ventasDetalletable - Checks Status: Validates that the parent sale is not voided (
v.nula = 0) - Updates Stock: Decreases product stock by the quantity sold
- Uses JOIN: Links inserted items to products and validates against the sale header
Important: This trigger only processes valid sales. If a sale is marked as voided (
nula = 1), the stock will not be decreased. This prevents inventory discrepancies when transactions are canceled.Trigger Flow
Example Scenario
The trigger uses the
inserted pseudo-table, which contains the new rows being added. This allows the trigger to process one or multiple rows efficiently in a single operation.TR_ActualizarSaldo
Automatically updates sale balances and marks installments as paid when payments are recorded.Purpose
- Update outstanding balance on credit sales
- Mark specific installments as paid
- Maintain synchronized payment records
- Automate accounts receivable management
SQL Definition
How It Works
- Fires After: New payment records are inserted into
abonosVentastable - Updates Balance: Decreases the sale’s outstanding balance (
saldo) by the payment amount - Marks Installment: Sets the corresponding installment as paid (
pagada = 1) - Dual Action: Performs both updates atomically in a single transaction
Key Behavior: This trigger performs two critical updates:
- Reduces the
saldofield in theventastable - Marks the installment as paid in the
planPagostable
Trigger Flow
Example Scenario
When recording payments in the application layer, you only need to insert into
abonosVentas. The trigger handles updating both the sale balance and installment status automatically.Trigger Best Practices
When Using These Triggers
- Never manually update stock after creating sales - let the trigger handle it
- Always specify numeroCuota when recording payments in
abonosVentas - Use transactions when inserting sale details to ensure atomicity
- Monitor for errors as trigger failures will rollback the entire operation
Testing Triggers
Error Handling
Performance Considerations
Both triggers are optimized for performance:- Use set-based operations instead of cursors
- Leverage indexes on join columns (
norden,codprod,numeroCuota) - Execute AFTER INSERT (not INSTEAD OF) to allow batch processing
- Minimal logic keeps execution time low
Related Topics
- Views - Query credit sales and payment status
- Indexes - Performance optimizations for trigger operations
- Sales and Purchases - Table structures affected by triggers
- Database Overview - Entity relationships and architecture