我有一个存储过程,该过程运行一个select语句,该语句返回一行并将内容插入到临时表中。 select语句在where子句中有6个条件语句,我基本上需要遍历3组其他条件并将这些结果插入temp表中。这是我到目前为止的内容:
CREATE DEFINER=`sleuser`@`%` PROCEDURE `CostDashboard`()
BEGIN
create temporary table TempTable (
ProjectID int,
Phase varchar(100),
OriginalCommitments float,
ApprovedCommitmentChanges float,
CurrentAssigned float,
PendingScopeChanges float,
EAC float,
PercentComplete float
);
insert into TempTable(
SELECT project_id,
'FP' as Phase,
OriginalCommitments,
ApprovedCommitmentChanges,
OriginalCommitments+ApprovedCommitmentChanges as CurrentAssigned,
sum(ProjectCostBudget.PendingChangeOrders) as PendingScopeChanges
sum(ProjectCost.CurrentWorkCompleted) +
sum(ProjectCost.EstimateToComplete) as EAC,
(sum(ProjectCost.CurrentWorkCompleted) +
sum(ProjectCost.EstimateToComplete) /
(sum(ProjectCostBudget.OriginalContractPrice +
ProjectCostBudget.RegisteredChangeOrders))) as PercentComplete
FROM `RCLY-DEV`.project
inner join ImportCost on ImportCost.ProjectID = project.pmis
inner join ProjectCostBudget on ProjectCostBudget.ProjectID =
project.project_id
inner join ProjectCost on ProjectCost.ProjectID = project.project_id
where ImportCost.ProjectID = 'RLCY-BB-01'
and ImportCost.Task = "020.0000.000"
and ProjectCostBudget.ProjectID = 2
and ProjectCostBudget.ServiceNumber = "020.0000.000"
and ProjectCost.MonthYear != ''
and ProjectCost.MonthYear like 'July%2018'
);
select * from TempTable
;
END
这有效并将一条带有硬编码值的记录插入where子句中,但是我需要为3组变量运行它,因此我创建了一个额外的临时表,如下所示:
|ImpCostID|ImpCostTask |PCBID|PCBServNum |MonthYear|
-----------------------------------------------------
|XXY-01-01|030.0000.000|3 |030.0000.000|July%2018|
|QWY-01-01|040.0000.000|4 |040.0000.000|May%2018 |
|ZXF-01-01|040.0000.000|5 |050.0000.000|June%2018|
但是我不确定如何将这些值集分配给变量,然后遍历它们。有什么建议吗?
答案 0 :(得分:1)
尽量避免对SQL语句进行序列化(可能会很昂贵)。在这种情况下,您只需在实际查询中对参数表使用JOIN。如果您只是使用临时表TempTable
来保存结果,那么在一个查询中获得所有结果时,也不需要这样做:
CREATE DEFINER=`sleuser`@`%` PROCEDURE `CostDashboard`()
BEGIN
create temporary table query_params_tmp (
ImpCostID varchar(20),
ImpCostTask varchar(20),
PCBID int,
PCBServNum varchar(20),
MonthYear varchar(20)
);
insert into query_params_tmp values
('XXY-01-01', '030.0000.000', 3, '030.0000.000', 'July%2018'),
('QWY-01-01', '040.0000.000', 4, '040.0000.000', 'May%2018'),
('ZXF-01-01', '040.0000.000', 5, '050.0000.000', 'June%2018');
SELECT project_id,
'FP' as Phase,
OriginalCommitments,
ApprovedCommitmentChanges,
OriginalCommitments+ApprovedCommitmentChanges as CurrentAssigned,
sum(ProjectCostBudget.PendingChangeOrders) as PendingScopeChanges
sum(ProjectCost.CurrentWorkCompleted) +
sum(ProjectCost.EstimateToComplete) as EAC,
(sum(ProjectCost.CurrentWorkCompleted) +
sum(ProjectCost.EstimateToComplete) /
(sum(ProjectCostBudget.OriginalContractPrice +
ProjectCostBudget.RegisteredChangeOrders))) as PercentComplete
FROM `RCLY-DEV`.project
join ImportCost on ImportCost.ProjectID = project.pmis
join ProjectCostBudget on ProjectCostBudget.ProjectID = project.project_id
join ProjectCost on ProjectCost.ProjectID = project.project_id
join query_params_tmp qp on
qp.ProjectID = ImportCost.ProjectID and
qp.ImpCostTask = ImportCost.Task and
qp.PCBID = ProjectCostBudget.ProjectID and
ProjectCost.MonthYear like qp.MonthYear
GROUP BY OriginalCommitments, ApprovedCommitmentChanges;
END
答案 1 :(得分:0)
如果我正确理解了您的问题,那么下面的问题将会解决。
创建带有标识列的临时表,然后将数据插入其中。如果临时表没有唯一列,这将有助于遍历每个记录
--declare variable
DECLARE @ID INT,
@Task VARCHAR(256),
@ProjectID INT,
...
...
...
SELECT @ID = MIN(ID)
FROM TemporaryTable
WHILE ISNULL(@ID, '') <> ''
BEGIN
SELECT @Task = ImpCostTask
,@ProjectID = PCBID
...
...
...
FROM TemporaryTable
WHERE ID = @ID
insert into TempTable(
SELECT project_id,
'FP' as Phase,
OriginalCommitments,
ApprovedCommitmentChanges,
....
....
....
....
FROM `RCLY-DEV`.project
inner join ImportCost on ImportCost.ProjectID = project.pmis
inner join ProjectCostBudget on ProjectCostBudget.ProjectID = project.project_id
inner join ProjectCost on ProjectCost.ProjectID = project.project_id
where ImportCost.ProjectID = @ProjectID
and ImportCost.Task = @Task
and ProjectCostBudget.ProjectID = @ProjectID
....
....
....
....
....
....
....
);
SELECT @ID = MIN(ID)
FROM TemporaryTable
WHERE ID > @ID
END