我在存储过程内的子查询中调用一个函数,该函数返回7列,在子查询中,我需要调用该函数7次以获取每个记录。由于子查询每次都调用一次以获取单列记录,因此存储过程需要很长时间来获取记录。 有没有办法一次调用函数的所有7列?
存储过程
SELECT ROW_NUMBER() OVER (ORDER BY chemicalRecord.StartDate DESC) AS ROW_NUMBER, chemicalRecord.Area, chemicalRecord.AreaBlock, chemicalRecord.FarmId, iif(chemicalRecord.totalLand is null,chemicalRecord.totalLandByArea,chemicalRecord.totalLand) as totalLand, chemicalRecord.WindSpeed
, chemicalRecord.Direction, chemicalRecord.Note, chemicalRecord.ChemicalApplicationID, chemicalRecord.StartDate, chemicalRecord.TreatmentType, chemicalRecord.Model, chemicalRecord.TankSize, chemicalRecord.TaskNumber
, chemicalRecord.FinishTime, chemicalRecord.FirstName, chemicalRecord.StartTime, iif(chemicalRecord.areaUnit is null,chemicalRecord.areaUnitByArea, chemicalRecord.areaUnit) as areaUnit, chemicalRecord.totalLandByArea, chemicalRecord.areaUnitByArea, chemicalRecord.Name, chemicalRecord.Rate, chemicalRecord.ChemicalFamily
, chemicalRecord.WHP, chemicalRecord.qty, chemicalRecord.totalMixer, chemicalRecord.RateTotalMixer, chemicalRecord.pro, chemicalRecord.block, chemicalRecord.Zone, chemicalRecord.produceCatogory, chemicalRecord.TreatmentTypeNew
FROM (
SELECT (
SELECT TOP (1) Name
FROM fnChemicalRecordForFarm(@companyID) as chemicalRecordForFarm
WHERE chemicalRecordForFarm.ChemicalApplicationID = chemicalApplication.ChemicalApplicationID
) AS Area, (
SELECT TOP (1) Name
FROM fnChemicalRecordForAreaBlock(@companyID) AS chemicalRecordForAreaBlock
WHERE chemicalRecordForAreaBlock.ChemicalApplicationID = chemicalApplication.ChemicalApplicationID
) AS AreaBlock, (
SELECT farm.FarmID
FROM Land AS land, Farm AS farm
WHERE (land.LandID = chemicalApplication.LandID) AND (farm.LandID = land.LandID)
) AS FarmId, (
SELECT SUM(LandArea)
FROM fnChemicalRecordForFarm(@companyID) as chemicalRecordForFarm
WHERE chemicalRecordForFarm.ChemicalApplicationID = chemicalApplication.ChemicalApplicationID
) AS totalLandByArea, (
SELECT TOP (1) Unit
From fnChemicalRecordForFarm(@companyID) as chemicalRecordForFarm
WHERE chemicalRecordForFarm.ChemicalApplicationID = chemicalApplication.ChemicalApplicationID
) AS areaUnitByArea, (
SELECT SUM(LandArea)
FROM fnChemicalRecordAreaUnit(@companyID) AS chemicalRecordAreaUnit
WHERE chemicalRecordAreaUnit.ChemicalApplicationID = chemicalApplication.ChemicalApplicationID
) AS totalLand, (
SELECT TOP (1) Unit
FROM fnChemicalRecordAreaUnit(@companyID) AS chemicalRecordAreaUnit
WHERE chemicalRecordAreaUnit.ChemicalApplicationID = chemicalApplication.ChemicalApplicationID
) AS areaUnit, (
SELECT Distinct chemicalRecordForChemicalApplication.Name as Name
From fnChemicalRecordForChemicalApplication(@companyID) as chemicalRecordForChemicalApplication
WHERE chemicalRecordForChemicalApplication.ChemicalApplicationID = chemicalApplication.ChemicalApplicationID
) AS Name, (
SELECT chemicalRecordForChemicalApplication.Rate as Rate
From fnChemicalRecordForChemicalApplication(@companyID) as chemicalRecordForChemicalApplication
WHERE chemicalRecordForChemicalApplication.ChemicalApplicationID = chemicalApplication.ChemicalApplicationID
) AS Rate, (
SELECT Distinct chemicalRecordForChemicalApplication.ChemicalFamily as ChemicalFamily
From fnChemicalRecordForChemicalApplication(@companyID) as chemicalRecordForChemicalApplication
WHERE chemicalRecordForChemicalApplication.ChemicalApplicationID = chemicalApplication.ChemicalApplicationID
) AS ChemicalFamily, (
SELECT Distinct chemicalRecordForChemicalApplication.WHP as WHP
From fnChemicalRecordForChemicalApplication(@companyID) as chemicalRecordForChemicalApplication
WHERE chemicalRecordForChemicalApplication.ChemicalApplicationID = chemicalApplication.ChemicalApplicationID
) AS WHP, (
SELECT Distinct chemicalRecordForChemicalApplication.qty as qty
From fnChemicalRecordForChemicalApplication(@companyID) as chemicalRecordForChemicalApplication
WHERE chemicalRecordForChemicalApplication.ChemicalApplicationID = chemicalApplication.ChemicalApplicationID
) AS qty, (
SELECT chemicalRecordForChemicalApplication.totalMixer as totalMixer
From fnChemicalRecordForChemicalApplication(@companyID) as chemicalRecordForChemicalApplication
WHERE chemicalRecordForChemicalApplication.ChemicalApplicationID = chemicalApplication.ChemicalApplicationID
) AS totalMixer, (
SELECT chemicalRecordForChemicalApplication.RateTotalMixer as RateTotalMixer
From fnChemicalRecordForChemicalApplication(@companyID) as chemicalRecordForChemicalApplication
WHERE chemicalRecordForChemicalApplication.ChemicalApplicationID = chemicalApplication.ChemicalApplicationID
) AS RateTotalMixer, (
SELECT Distinct chemicalRecordVarietyName.VarietyName as pro
From fnChemicalRecordVarietyName(@companyID) as chemicalRecordVarietyName
WHERE chemicalRecordVarietyName.ChemicalApplicationID = chemicalApplication.ChemicalApplicationID
) AS pro, (
SELECT Distinct chemicalRecordBlock.block as block
From fnChemicalRecordBlock() as chemicalRecordBlock
WHERE chemicalRecordBlock.ChemicalApplicationID = chemicalApplication.ChemicalApplicationID
) AS block, (
SELECT Distinct chemicalRecordZone.Zone as Zone
From fnChemicalRecordZone() as chemicalRecordZone
WHERE chemicalRecordZone.ChemicalApplicationID = chemicalApplication.ChemicalApplicationID
) AS Zone, (
SELECT Distinct chemicalRecordProduceCatogory.Name as produceCatogory
From fnChemicalRecordProduceCatogory(@companyID) as chemicalRecordProduceCatogory
WHERE chemicalRecordProduceCatogory.ChemicalApplicationID = chemicalApplication.ChemicalApplicationID
) AS produceCatogory, (
SELECT Distinct chemicalRecordTreatmentType.GroupName as TreatmentTypeNew
From fnChemicalRecordTreatmentType(@companyID) as chemicalRecordTreatmentType
WHERE chemicalRecordTreatmentType.ChemicalApplicationID = chemicalApplication.ChemicalApplicationID
) AS TreatmentTypeNew
, chemicalApplication.CompanyID, chemicalApplication.StartTime, chemicalApplication.WindSpeed, chemicalApplication.Direction, chemicalApplication.Note
, chemicalApplication.ChemicalApplicationID, chemicalApplication.StartDate, chemicalApplication.TreatmentType, equipment.Model
, chemicalApplication.TankSize, chemicalApplication.TaskNumber, chemicalApplication.FinishTime, member2.FirstName
FROM ChemicalApplication AS chemicalApplication
LEFT OUTER JOIN Member AS member ON member.MemberID = chemicalApplication.OperatedBy
INNER JOIN Member AS member2 ON chemicalApplication.OperatedBy = (member2.MemberID)
LEFT OUTER JOIN Equipment AS equipment ON equipment.EquipmentID = chemicalApplication.EquipmentID
) AS chemicalRecord
功能
ALTER function [dbo].[fnChemicalRecordForChemicalApplication](@companyID int)
returns @ChemicalApplication table(
ChemicalApplicationID int
,Name NVARCHAR(max)
,WHP NVARCHAR(max)
,qty NVARCHAR(max)
,Rate NVARCHAR(max)
,ChemicalFamily NVARCHAR(max)
,totalMixer NVARCHAR(max)
,RateTotalMixer NVARCHAR(max)
)
as
begin
DECLARE @Test TABLE
(
ChemicalApplicationID int
,Name NVARCHAR(max)
,WHP NVARCHAR(max)
,qty NVARCHAR(max)
,Rate NVARCHAR(max)
,ChemicalFamily NVARCHAR(max)
,totalMixer NVARCHAR(max)
,RateTotalMixer NVARCHAR(max)
)
DECLARE @Test2 TABLE
(
ChemicalApplicationID int
,Name NVARCHAR(max)
,WHP NVARCHAR(max)
,qty NVARCHAR(max)
,Rate NVARCHAR(max)
,ChemicalFamily NVARCHAR(max)
,totalMixer NVARCHAR(max)
,RateTotalMixer NVARCHAR(max)
)
insert into @Test
SELECT chemicalApplicationChemical.ChemicalApplicationID as ChemicalApplicationID, chemical.Name as Name, chemical.Whp as WHP, chemicalApplicationChemical.Quantity as qty
, chemicalApplicationChemical.Rate as Rate, chemical.ChemicalType as ChemicalFamily, chemicalApplicationChemical.TotalMixture as totalMixer
, chemicalApplicationChemical.Rate + '/' + iif(chemicalApplicationChemical.TotalMixture is null, ' ', chemicalApplicationChemical.TotalMixture) as RateTotalMixture
FROM ChemicalApplicationChemical AS chemicalApplicationChemical
INNER JOIN Chemical AS chemical ON chemical.ChemicalID = chemicalApplicationChemical.ChemicalID
WHERE (chemical.CompanyID = @companyID)
insert into @Test2
SELECT chemicalApplicationChemical.ChemicalApplicationID as ChemicalApplicationID, chemical.Name as Name, chemical.Whp as WHP, chemicalApplicationChemical.Quantity as qty
, chemicalApplicationChemical.Rate as Rate, chemical.ChemicalType as ChemicalFamily, chemicalApplicationChemical.TotalMixture as totalMixer
, chemicalApplicationChemical.Rate + '/' + iif(chemicalApplicationChemical.TotalMixture is null, ' ', chemicalApplicationChemical.TotalMixture) as RateTotalMixture
FROM ChemicalApplicationChemical AS chemicalApplicationChemical
INNER JOIN Chemical AS chemical ON chemical.ChemicalID = chemicalApplicationChemical.ChemicalID
WHERE (chemical.CompanyID = @companyID)
insert into @ChemicalApplication
select distinct ChemicalApplicationID,
STUFF((Select Distinct ','+T.Name
from @Test T
where T2.ChemicalApplicationID=T.ChemicalApplicationID
FOR XML PATH('')),1,1,''),
STUFF((Select Distinct ','+T.WHP
from @Test T
where T2.ChemicalApplicationID=T.ChemicalApplicationID
FOR XML PATH('')),1,1,''),
STUFF((Select Distinct ','+T.qty
from @Test T
where T2.ChemicalApplicationID=T.ChemicalApplicationID
FOR XML PATH('')),1,1,''),
STUFF((Select Distinct ','+T.Rate
from @Test T
where T2.ChemicalApplicationID=T.ChemicalApplicationID
FOR XML PATH('')),1,1,''),
STUFF((Select Distinct ','+T.ChemicalFamily
from @Test T
where T2.ChemicalApplicationID=T.ChemicalApplicationID
FOR XML PATH('')),1,1,''),
STUFF((Select Distinct ','+T.totalMixer
from @Test T
where T2.ChemicalApplicationID=T.ChemicalApplicationID
FOR XML PATH('')),1,1,''),
STUFF((Select Distinct ','+T.RateTotalMixer
from @Test T
where T2.ChemicalApplicationID=T.ChemicalApplicationID
FOR XML PATH('')),1,1,'')
from @Test2 T2
return
end