# `Sp_calc_addData_toTable` (procedure)

> 自定义计算根据传入的data自动插入到对应表中，防止数据包含特殊符号 例如JSON


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

## Parameters

| # | Mode | Name | Type |
|---|---|---|---|
| 1 | IN | `sData` | `longtext` |

## Body

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

## Narrative

**Business context:** 自定义计算 (custom-calc engine) — generic helper for the product-card / packaging-material auto-calculation flow: takes a JSON payload describing one or more `<scope>.<table>` chunks and bulk-inserts each chunk into the corresponding `<table>_calc_tmp` staging table, side-stepping SQL escaping by passing values through `JSON_TABLE`.

**What it does:** Iterates the top-level keys of `sData` (format `master.<tbl>` or `slave.<tbl>` etc.). For each key it discovers the union of all column names appearing across array elements (recursive CTE walking up to 100 rows × 100 keys), filters out reserved keys (`key`, `iAutoNum`, `handleType`, `sDataTableName`), then dynamically builds an `INSERT INTO <tbl>_calc_tmp (...) SELECT ... FROM JSON_TABLE(@json_column, '$[*]' COLUMNS (...))` statement. Column SQL types are inferred from the first letter of the column name: `d*` → DECIMAL(18,6), `i*`/`b*` → INT(11), `t*` → DATETIME, everything else → LONGTEXT. `handleType` defaults to `'add'`. The built SQL is prepared and executed.

**Invocation:** Called by `Sp_ProductCardAddMaterials_calc` (the "自定义计算过程" engine that builds product-card / packaging-material derived tables from front-end input). Not exposed directly to any form, gdsmodule hook, or Java caller.

Flag: per-call `DROP/CREATE TEMPORARY TABLE json_array` plus PREPARE on user-supplied table names — caller is fully trusted; do not expose this proc to outside input.
