# `Sp_Bill_Used` (procedure)

> 单据是否被下级使用存储过程


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

## Parameters

| # | Mode | Name | Type |
|---|---|---|---|
| 1 | IN | `sTableName` | `varchar(100)` |
| 2 | IN | `sLoginId` | `varchar(100)` |
| 3 | IN | `sGuid` | `varchar(100)` |
| 4 | OUT | `sReturn` | `varchar(1000)` |
| 5 | IN | `sBrId` | `varchar(100)` |
| 6 | IN | `sSuId` | `varchar(100)` |
| 7 | IN | `sType` | `varchar(100)` |
| 8 | OUT | `sCode` | `int` |
| 9 | OUT | `sReturnIds` | `text` |

## Body

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

## Narrative

**Business context:** Cross-module / 单据是否被下级使用 — the universal "is this document referenced downstream?" guard, called from every `Sp_Calc_s*` audit/un-audit proc before allowing a check/uncheck (or delete) to proceed. Comment header: `单据是否被下级使用存储过程`.

**What it does:** Dispatches on `sTableName` (e.g. `SalDeliverGoodsMaster`, `PurPurchaseOrderMaster`, `MftWorkOrderMaster`, ...) and runs the matching downstream-existence query — counts non-invalidated rows in the consuming slave table whose `sSrcSlaveId` ties back to this document, and on a hit returns `sCode=0` plus a human message in `sReturn` and the offending IDs in `sReturnIds`. `sType` ("Audit", "SaleTrial", "Delete", ...) varies the strictness. The body has dozens of `IF sTableName = '...'` branches covering AccMaterialsInitMaster, AccProductDoingMaster, CahPayMentMaster, CahReceiptMaster, EleCustomer, EleEmployee, EleMaterialsStock, MftProductionPlanMaster, mftworkordermaster, salsaleschancemaster, and many others.

**Invocation:** Called by ~60 `Sp_Calc_s*` audit/un-audit procedures (e.g. `Sp_Calc_sDgd`, `Sp_Calc_sSod`, `Sp_Calc_sWod`, `Sp_Calc_sPpa`, …) when a user clicks 审核/反审核/作废 on the corresponding document; also called by `Sp_AfterPost_sQtt`, `sp_btn_action`, `Sp_System_BatchPriceMoney`, `Sp_UpdateAdd_Check`, and the templated `sProcName.sql` macros in `xlyEntry` / `xlyFlow`. Multiple customer overrides at `script/客户/{千彩,统兴}/Sp_Bill_Used.sql`.
