# `Sp_afterSave_sDgda` (procedure)

> 材料送货单单据保存后反写数量


- **Type:** PROCEDURE
- **Deterministic:** NO
- **SQL data access:** CONTAINS SQL

## Parameters

| # | Mode | Name | Type |
|---|---|---|---|
| 1 | IN | `sGuid` | `varchar(2000)` |
| 2 | IN | `sBrId` | `varchar(100)` |
| 3 | IN | `sSuId` | `varchar(100)` |
| 4 | OUT | `sCode` | `int` |
| 5 | OUT | `sReturn` | `varchar(4000)` |

## Body

_Body is not pre-cached. To inspect: `mysql --defaults-file=~/.my.cnf -e 'SHOW CREATE PROCEDURE `Sp_afterSave_sDgda`'`._

## Narrative

**Business context:** 材料送货单 (materials-delivery note) — outlier in the historic `Sp_afterSave_s<Code>` family. Unlike its thin-wrapper siblings, this one inlines the reverse-write logic directly instead of delegating to `Sp_saveReturn_sDgda` (companion was removed or renamed). The work it does is the same: after a materials-delivery note is saved, the proc syncs the upstream 送货通知 row's `dDeliverQty`/`dMaterialsDeliverQty` to the SUM of all related 送货单 detail rows and renumbers the line numbers.

**What it does:** Builds three dynamic UPDATE statements in `p_sMasterSql`, then splits by `;` and `PREPARE`/`EXECUTE`s each: (1) `UPDATE saldelivernotifyslave SET dDeliverQty / dMaterialsDeliverQty = SUM(non-invalid saldelivergoodsslave qty grouped by sSrcSlaveId)` for the notify-rows referenced by the saved delivery; (2) `UPDATE saldelivergoodsslave SET iDeliverLineNo = iIncrement % 1000000` to renumber lines on the saved delivery; (3) `UPDATE saldelivergoodsslave SET dReturnQty = |SUM(red-strike rows by sMinusSrcSlaveId)|` to write back 红冲 (red-strike) reversals; and a fourth block updating `mitmaterialsstorebarcode.dReturnQty/dReturnMaterialsQty` from `salrejectgoodsmaster` aggregates.

**Invocation:** Status: appears orphaned. No caller found in any channel (`gdsmodule.sSaveProName`/`sCalcProName`/`sProcName`, form-master, other routines, xly-src grep) — same decommissioned-legacy condition as the rest of the `Sp_afterSave_*` family; production reverse-write for delivery notes flows through `Sp_Check_sDgd` instead. Candidate for maintainer audit.

Caveats: builds and executes raw dynamic SQL via PREPARE/EXECUTE, splitting on `;` — fragile against any literal `;` inside the SQL (none present today but no escaping); JOIN-against-self pattern means the proc can be expensive on large `saldelivergoodsslave` tables.
