2

我在做什么。

我有一个删除行的过程。

var_SelectedIds越来越,分离UUID()

然后

SET     @var_SQLStr = '';   
SET     @var_SQLStr = CONCAT(@var_SQLStr ,'Delete ');
SET     @var_SQLStr = CONCAT(@var_SQLStr ,'FROM DemoTable');        
SET     @var_SQLStr = CONCAT(@var_SQLStr ,'WHERE DemoTableId IN (''',Replace(var_SelectedIds,',',''','''),'''); ');

-- SELECT @var_SQLStr;
PREPARE stmt FROM @var_SQLStr;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

我已经尝试过的。

// This is throwing SQL Syntax ERROR.

SET     @var_SQLStr = '';   
SET     @var_SQLStr = CONCAT(@var_SQLStr ,'Delete ');
SET     @var_SQLStr = CONCAT(@var_SQLStr ,'FROM DemoTable');        
SET     @var_SQLStr = CONCAT(@var_SQLStr ,'WHERE DemoTableId IN (''',Replace(var_SelectedIds,',',''','''),'''); ');
SET     @var_SQLStr = CONCAT(@var_SQLStr ,' SELECT ROWS_COUNT() INTO @var_AffectedRows; ');

-- SELECT @var_SQLStr;
PREPARE stmt FROM @var_SQLStr;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

MySQL 参考

文本必须代表单个语句,而不是多个语句。

我也试过

START TRANSACTION;

-- Other statements here

SET     @var_SQLStr = '';   
SET     @var_SQLStr = CONCAT(@var_SQLStr ,'Delete ');
SET     @var_SQLStr = CONCAT(@var_SQLStr ,'FROM DemoTable');        
SET     @var_SQLStr = CONCAT(@var_SQLStr ,'WHERE DemoTableId IN (''',Replace(var_SelectedIds,',',''','''),'''); ');

-- SELECT @var_SQLStr;
PREPARE stmt FROM @var_SQLStr;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SELECT ROW_COUNT() INTO @var_AffectedRows
COMMIT;

SELECT @var_AffectedRows AS NoOfRowsDeleted; // 0 output

我无法了解如何删除行。

4

1 回答 1

6

您需要在SELECT ROW_COUNT()之后EXECUTE和之前执行DEALLOCATE...

START TRANSACTION;

-- Other statements here

SET     @var_SQLStr = '';   
SET     @var_SQLStr = CONCAT(@var_SQLStr ,'Delete ');
SET     @var_SQLStr = CONCAT(@var_SQLStr ,'FROM DemoTable');        
SET     @var_SQLStr = CONCAT(@var_SQLStr ,' WHERE DemoTableId IN (''',Replace(var_SelectedIds,',',''','''),'''); ');
/*you need an additional whitespace here---^ */


-- SELECT @var_SQLStr;
PREPARE stmt FROM @var_SQLStr;
EXECUTE stmt;
SELECT ROW_COUNT() INTO @var_AffectedRows;
DEALLOCATE PREPARE stmt;

SELECT @var_AffectedRows; /*TADAAA!*/

COMMIT;
于 2015-10-19T09:07:04.113 回答