有什么方法可以存储一个 SELECT 结果并同时输出结果集吗?
Is there any way to store a SELECT result and output the resultset at the same time?
在下一个示例过程中,我可以将查询的行 table 输出为过程结果集,同时将单个字段或某些字段存储在变量中(类似于我使用时SELECT 进入)?我能想到的唯一方法是重复查询。
CREATE PROCEDURE `Panel_TerminalesForm`(IN idTerminal INT)
BEGIN
declare somefield INT:
-- this select statement returns the found row as a resultset
SELECT terminales.*
FROM terminales
WHERE id_terminal = idTerminal;
-- but I also want to have here a table field inside the variable somefield
-- do some manipulation with somefield...
END;
如果我没理解错的话,你需要创建一个临时文件 table,然后你可以按照你想要的方式操作临时文件。
MySQL:
CREATE PROCEDURE `Panel_TerminalesForm`(IN idTerminal INT)
BEGIN
declare somefield INT
CREATE TEMPORARY TABLE SampleTempTable
SELECT * INTO SampleTempTable FROM terminales WHERE id_terminal = idTerminal;
-- output the resultset
SELECT * FROM SampleTempTable
-- read the variables you want
SELECT field INTO somefield FROM SampleTempTable
-- Drop the temp table
DROP TEMPORARY TABLE SampleTempTable
END;
http://www.mysqltutorial.org/mysql-temporary-table/
SQL 服务器:
CREATE PROCEDURE `Panel_TerminalesForm`(IN idTerminal INT)
BEGIN
declare somefield INT
SELECT terminales.*
INTO #tempSample FROM terminales
WHERE id_terminal = idTerminal;
SELECT * FROM #tempSample
-- Drop the temp table
DROP TABLE #tempSample
END;
注意:使用后删除温度 table 以避免错误。
在下一个示例过程中,我可以将查询的行 table 输出为过程结果集,同时将单个字段或某些字段存储在变量中(类似于我使用时SELECT 进入)?我能想到的唯一方法是重复查询。
CREATE PROCEDURE `Panel_TerminalesForm`(IN idTerminal INT)
BEGIN
declare somefield INT:
-- this select statement returns the found row as a resultset
SELECT terminales.*
FROM terminales
WHERE id_terminal = idTerminal;
-- but I also want to have here a table field inside the variable somefield
-- do some manipulation with somefield...
END;
如果我没理解错的话,你需要创建一个临时文件 table,然后你可以按照你想要的方式操作临时文件。
MySQL:
CREATE PROCEDURE `Panel_TerminalesForm`(IN idTerminal INT)
BEGIN
declare somefield INT
CREATE TEMPORARY TABLE SampleTempTable
SELECT * INTO SampleTempTable FROM terminales WHERE id_terminal = idTerminal;
-- output the resultset
SELECT * FROM SampleTempTable
-- read the variables you want
SELECT field INTO somefield FROM SampleTempTable
-- Drop the temp table
DROP TEMPORARY TABLE SampleTempTable
END;
http://www.mysqltutorial.org/mysql-temporary-table/
SQL 服务器:
CREATE PROCEDURE `Panel_TerminalesForm`(IN idTerminal INT)
BEGIN
declare somefield INT
SELECT terminales.*
INTO #tempSample FROM terminales
WHERE id_terminal = idTerminal;
SELECT * FROM #tempSample
-- Drop the temp table
DROP TABLE #tempSample
END;
注意:使用后删除温度 table 以避免错误。