# `Sp_System_ReplaceField_Detail` (procedure)

> 公式语言转换成sql


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

## Parameters

| # | Mode | Name | Type |
|---|---|---|---|
| 1 | IN | `sAllGuid` | `longtext` |
| 2 | IN | `sAllTbName` | `longtext` |
| 3 | IN | `sAllType` | `longtext` |
| 4 | IN | `sFormulaGuid` | `varchar(100)` |
| 5 | OUT | `sReturn` | `longtext` |
| 6 | IN | `sTmpValue` | `varchar(100)` |
| 7 | OUT | `sDetail` | `longtext` |
| 8 | IN | `sLoginId` | `varchar(100)` |
| 9 | IN | `sParentGuid` | `varchar(100)` |
| 10 | IN | `sBrId` | `varchar(100)` |
| 11 | IN | `sSuId` | `varchar(100)` |
| 12 | OUT | `sCode` | `int` |

## Body

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

## Narrative

**Business context:** 生产管理 → 排产/产能 — formula-language → SQL transpiler used while computing a 排产 (production-plan) row's 工时 / 调机时间. Member of the `Sp_System_ReplaceField*` family (label "公式语言转换成sql"): each variant binds a different set of context tables (单据主表/从表/工序表/材料表 + 基础产品/材料/工序/机台/客户/供应商). The `_Detail` variant takes an extra `sParentGuid` to scope the formula evaluation to a parent context row — used when the formula resolves against a slave/control/process triple inside a parent 排产明细.

**What it does:** Reads `SysSystemSettings` (`CbxLanguage`, `CbxMaterialsUnit`, `CbxMachineUnit`, `NetConversionRate`, `CkxMinColorCalculation`) into a JSON envelope, looks up `gdsformconst` constants in the active language, then walks the formula text in `eleformula` (and adjacent `eleformulaslave`/`eleformulaparam` rows) keyed by `sFormulaGuid`, building a SQL expression by substituting tagged tokens (`#{billMaster.col}`, `#{baseProcedure.col}`, etc.) with values pulled from the named table+id pair in `sAllGuid`/`sAllTbName`/`sAllType`. Executes the assembled SQL via PREPARE/EXECUTE, captures the numeric result back into `sReturn` and a human-readable trace into `sDetail`.

**Invocation:** Called by `Sp_CalcPlan_dHour` (the per-排产-行 hour-calculation proc) twice — once with `p_sProductionReportId` (产能 formula) and once with `p_sAdjustableFormulaId` (调机 formula). `Sp_CalcPlan_dHour` is in turn invoked by the 排产 module's hour-recalc button. No `gdsmodule` binding, no form-master, no xly-src Java caller — DB-layer helper only.
