BEGIN
#声明一个flag变量,默认值为-99
DECLARE flag INT DEFAULT -99;
#创建companyTempTabl_Qurey临时表
CREATE TEMPORARY TABLE IF NOT EXISTS companyTempTabl_Qurey (
id VARCHAR (32) NOT NULL ,
` name ` VARCHAR (100),
attr VARCHAR (30),
parentId VARCHAR (32)
);
TRUNCATE TABLE companyTempTabl_Qurey;
#创建companyTempTabl_Result临时表
CREATE TEMPORARY TABLE IF NOT EXISTS companyTempTabl_Result (
id VARCHAR (32) NOT NULL ,
` name ` VARCHAR (100),
attr VARCHAR (30),
parentId VARCHAR (32)
);
TRUNCATE TABLE companyTempTabl_Result;
#根据参数 parentId 查询数据,并插入companyTempTabl_Qurey临时表
INSERT INTO companyTempTabl_Qurey (
id,
` name `,
attr,
parentId
) SELECT
uapcompany.id,
uapcompany.` name `,
uapcompany.attr,
uapcompany.parentId
FROM uapcompany
WHERE uapcompany.parentId = `parentId`;
#根据参数 parentId 查询数据,并插入companyTempTabl_Result临时表
INSERT INTO companyTempTabl_Result (
id,
` name `,
attr,
parentId
) SELECT
uapcompany.id,
uapcompany.` name `,
uapcompany.attr,
uapcompany.parentId
FROM uapcompany
WHERE uapcompany.parentId = `parentId`;
#根据参数 parentId 统计查询总数,并赋值给变量flag
SELECT count (1) INTO flag
FROM uapcompany
WHERE uapcompany.parentId = `parentId`;
#如果flag 大于 0,则进行循环
WHILE flag > 0 DO
#创建companyTempTabl_temp 临时表
CREATE TEMPORARY TABLE IF NOT EXISTS companyTempTabl_temp (
id VARCHAR (32) NOT NULL ,
` name ` VARCHAR (100),
attr VARCHAR (30),
parentId VARCHAR (32)
);
TRUNCATE TABLE companyTempTabl_temp;
#数据库表uapcompany关联临时表companyTempTabl_Qurey查询,并将查询结果插入 临时表companyTempTabl_temp
INSERT INTO companyTempTabl_temp (id, ` name `, attr, parentId) SELECT
uapcompany.id,
uapcompany.` name `,
uapcompany.attr,
uapcompany.parentId
FROM
uapcompany,
companyTempTabl_Qurey
WHERE
uapcompany.parentId = companyTempTabl_Qurey.id;
#删除临时表companyTempTabl_Qurey数据
DELETE FROM companyTempTabl_Qurey;
#将临时表companyTempTabl_temp的数据 插入companyTempTabl_Qurey临时表,用作下一个循环的条件
INSERT INTO companyTempTabl_Qurey (id, ` name `, attr, parentId) SELECT
companyTempTabl_temp.id,
companyTempTabl_temp.` name `,
companyTempTabl_temp.attr,
companyTempTabl_temp.parentId
FROM
companyTempTabl_temp;
#将临时表companyTempTabl_temp的数据 插入到companyTempTabl_Result临时表(该表的数据也是我们最终要返回的数据)
INSERT INTO companyTempTabl_Result (id, ` name `, attr, parentId) SELECT
companyTempTabl_temp.id,
companyTempTabl_temp.` name `,
companyTempTabl_temp.attr,
companyTempTabl_temp.parentId
FROM
companyTempTabl_temp;
#删除companyTempTabl_temp数据
DROP TABLE companyTempTabl_temp;
#数据库表uapcompany关联 临时表companyTempTabl_Qurey查询统计,并将结果赋值给变量flag
SELECT
count (1) INTO flag
FROM
uapcompany,
companyTempTabl_Qurey
WHERE
uapcompany.parentId = companyTempTabl_Qurey.id;
END WHILE;
SELECT id ,` name `,attr,parentId FROM companyTempTabl_Result;
END
|