1

Refer to the below SQL. We need to save STD formula in table, but not able to evaluate this.

    DROP TEMPORARY TABLE IF EXISTS _std ;
CREATE TEMPORARY TABLE _std(stdformula VARCHAR(4000), mu DECIMAL(18,9), newValue DECIMAL(18,9));

INSERT INTO _std VALUES('SQRT((POWER((5-mu), 2) + POWER((6-mu), 2)  + POWER((7-mu), 2) + POWER((7-mu), 2) )/4)',5+6+7+7/4,7);

SET @temp=(SELECT REPLACE(stdformula,'mu',mu) FROM _std );
SELECT @temp;
-- @temp=> SQRT(( POWER((5-19.750000000), 2) + POWER((6-19.750000000), 2)  + POWER((7-19.750000000), 2) + POWER((7-19.750000000), 2) )/2);
-- which is valid statement
SELECT SQRT(( POWER((5-19.750000000), 2) + POWER((6-19.750000000), 2)  + POWER((7-19.750000000), 2) + POWER((7-19.750000000), 2) )/2);

-- How can we evaluate @temp using dynamic sql approach?? -- Below prepare is not working

PREPARE StmtStd FROM @temp;
EXECUTE StmtStd; 
DEALLOCATE PREPARE Stmtstd;*
4

1 回答 1

0

SELECT@temp. 尝试

...
SET @temp= CONCAT('SELECT ', (SELECT REPLACE(stdformula,'mu',mu) FROM _std ), ' result');
-- SELECT @temp;    
PREPARE StmtStd FROM @temp;
EXECUTE StmtStd;

输出:

|          RESULT |
-------------------
| 13.525438994724 |

这是SQLFiddle演示

于 2013-05-30T09:02:06.190 回答