# `Sp_System_ReplaceField` (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` | `text` |
| 8 | IN | `sLoginId` | `varchar(100)` |
| 9 | IN | `sBrId` | `varchar(100)` |
| 10 | IN | `sSuId` | `varchar(100)` |
| 11 | OUT | `sCode` | `int` |

## Body

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

## Narrative

**Business context:** 生产管理 / 计价 → 公式解析 — formula-language → SQL transpiler (label "公式语言转换成sql"), the engine behind 公式 fields on 工单/排产/计件/报价 rows. Given a formula id and a set of (table, id, tag) triples, substitutes each `#{tag.column}` placeholder in the formula's body with the actual value from the named table+row, then evaluates the resulting SQL.

**What it does:** Reads `SysSystemSettings` (`CbxLanguage`, `CbxMaterialsUnit`, `CbxMachineUnit`, `NetConversionRate`, `CkxMinColorCalculation`) into a JSON env, loads language-specific constants from `gdsformconst sParentId='100'`, then walks the formula text stored in `eleformula`/`eleformulaslave` keyed by `sFormulaGuid`. For each token in `sAllType` (`billMaster`, `billSlave`, `billControl`, `billMaterial`, `billProcedure`, `baseProcedure`, `baseMaterial`, `baseProduct`, `baseMachine`, `baseCustomer`, `baseSupply`, `baseTax`, `baseSalesMan`, `basePayment`, `billemployee`, `billreportemployee`, `billParam`, etc.) it reads the column from the matching table in `sAllTbName` keyed by the matching id in `sAllGuid` and splices the value into the SQL string. Assembled SQL is run via PREPARE/EXECUTE; numeric result returned in `sReturn`, per-step audit trail in `sDetail`.

**Invocation:** Called by Java `BtnCalculationServiceImpl` (the `doSetTimeCalcMachineHour` flow that recomputes 机台 `dHour` after a 排产 row is locked) — the Java code builds `p_sAllTbName="mftworkordermaster,MftWorkOrderSlave,MftWorkOrderControl,…,elemachine"`, `p_sAllType="billMaster,billSlave,billControl,…,baseMachine"`, then issues the procedure call. xly-src also ships install scripts at `script/标版/30100101/Sp_System_ReplaceField.sql` and references the name from several `Sp_Check_*` / `Sp_Calc_*` install scripts. Many DB sites embed `Sp_System_ReplaceField` calls inline at the SQL layer.
