SQL stored procedure calls table of other stored procedures

There is a stored procedure that counts the fuel economy of the driver. There are 4 cases of sub-part and co-driver inquiries, and there are 2 cases in which the driver acts as a co-driver. a 6 cases

Each case is similar, consider wrapping the stored procedure additionally to simplify.

ALTER PROCEDURE [dbo].[driverSaveOilSub]
	-- Add the parameters for the stored procedure here
	@startdate datetime,
	@enddate datetime,
	@department  varchar(5),
	@driverDoAsistant bit =0 -- the driver acts as the assistant driver, the default driver is not responsible. When other values ​​are passed in, it is responsible
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;---- dismount

	 select DriverGh, 0.5* sum(dzCount+dxCount+dcCount)+sum(score) as driverValue,
	  count( distinct tb_driverReportInput.submitDate) as  driverReportCount,
	  CAST(sum(tb_driverReportDetails.comprehensiveOil) as decimal(18,2))as comprehensiveOil
  from  tb_driverReportDetails left join tb_driverReportInput on tb_driverReportInput.id=tb_driverReportDetails.reportid
	left join tb_stationMileage on tb_stationMileage.id=mileageStationId  
   where (select department from tb_driver where tb_driver.gh=(CASE when @driverDoAsistant=0 then DriverGh else assistantDriverGh end) and post='司机')=@department
   and fromDateTime>=@startdate and fromDateTime<=@enddate  group by DriverGh
END

------------The following is the entire stored procedure

Create some temporary tables first, and the type and name of the temporary target should be good.

Then call the sub-stored procedure to populate the temporary table with data. There is no problem with this table. The last temporary table deletion code is not posted cry.

There are many repetitions. splicing with sql? Haven't tried yet.

ALTER PROCEDURE [dbo].[driverSaveOil]
	-- Add the parameters for the stored procedure here
	@startdate datetime,
	@enddate datetime
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
	--set @startdate=dateadd(hour,18,@startdate)
	--set @enddate=dateadd(hour,18,@enddate)
    -- Insert statements for procedure here
if OBJECT_ID('tempdb..tianjiDriver') is not null
drop table #tianjiDriver


if OBJECT_ID('tempdb..xieqiaoDriver') is not null
drop table #xieqiaoDriver

if OBJECT_ID('tempdb..tianjiAssistant') is not null
drop table #tianjiAssistant

if OBJECT_ID('tempdb..xieqiaoAssistant') is not null
drop table #xieqiaoAssistant

if OBJECT_ID('tempdb..xieqiaoDriverDoAsistant') is not null
drop table #xieqiaoDriverDoAsistant

if OBJECT_ID('tempdb..tianjiDriverDoAsistant') is not null
drop table #tianjiDriverDoAsistant

CREATE TABLE [dbo].[#tianjiDriver](
	[DriverGh] [nvarchar](20),
	[driverValue] float,
	[driverReportCount] int,
	[comprehensiveOil] decimal(18,2) NULL
)
CREATE TABLE [dbo].[#xieqiaoDriver](
	[DriverGh] [nvarchar](20),
	[driverValue] float,
	[driverReportCount] int,
	[comprehensiveOil] decimal(18,2) NULL
)
--
CREATE TABLE [dbo].[#tianjiDriverDoAsistant](
	[DriverGh] [nvarchar](20),
	[driverValue] float,
	[driverReportCount] int,
	[comprehensiveOil] decimal(18,2) NULL
)
CREATE TABLE [dbo].[#xieqiaoDriverDoAsistant](
	[DriverGh] [nvarchar](20),
	[driverValue] float,
	[driverReportCount] int,
	[comprehensiveOil] decimal(18,2) NULL
) --


CREATE TABLE [dbo].[#tianjiAssistant](
	[assistantDriverGh] [nvarchar](20),
	[driverValue] float,
	[driverReportCount] int,
	[comprehensiveOil] decimal(18,2) NULL
)
CREATE TABLE #xieqiaoAssistant(
	[assistantDriverGh] [nvarchar](20),
	[driverValue] float,
	[driverReportCount] int,
	[comprehensiveOil] decimal(18,2) NULL
)




INSERT #tianjiDriver      EXEC   [dbo].[driverSaveOilSub] @startdate,@enddate,'田集'
INSERT #tianjiAssistant EXEC   [dbo].[driverSaveOilSubAsistant]  @startdate,@enddate,'田集'
INSERT #xieqiaoDriver    EXEC  [dbo].[driverSaveOilSub] @startdate,@enddate,'谢桥'
INSERT #xieqiaoAssistant EXEC  [dbo].[driverSaveOilSubAsistant] @startdate,@enddate,'谢桥'

INSERT #tianjiDriverDoAsistant EXEC   [dbo].[driverSaveOilSub] @startdate,@enddate,'田集',1
INSERT #xieqiaoDriverDoAsistant EXEC  [dbo].[driverSaveOilSub] @startdate,@enddate,'谢桥',1

   select  
   (select gh from tb_driver where gh=DriverGh) as  gh,
   (select drivername from tb_driver where gh=DriverGh) as  name,
   (select department from tb_driver where gh=DriverGh) as  department,
   (select post from tb_driver where gh=DriverGh) as  post,
	 'driver' as workpost,
                    CAST((select sum(actualOil) from tb_driverReportInput nb where nb.DriverGh=b.DriverGh and fromDateTime>=@startdate and fromDateTime<=@enddate) as decimal(18,2)) as actualOil,
CAST(b.comprehensiveOil -(select sum(actualOil) from tb_driverReportInput nb where nb.DriverGh=b.DriverGh and fromDateTime>=@startdate and fromDateTime<=@enddate) as decimal(18,2)) as saveOil,
	 * from
	#tianjiDriver b


   union all


  select  
     (select gh from tb_driver where gh=assistantDriverGh) as  gh,
   (select drivername from tb_driver where gh=assistantDriverGh) as  name,
   (select department from tb_driver where gh=assistantDriverGh) as  department,
    (select post from tb_driver where gh=assistantDriverGh) as  post,
	 'Co-driver' as workpost,
	                CAST((select sum(actualOil) from tb_driverReportInput nb where nb.assistantDriverGh=assistantDriverGh and fromDateTime>=@startdate and fromDateTime<=@enddate)as decimal(18,2))as actualOil,
CAST(comprehensiveOil -(select sum(actualOil) from tb_driverReportInput nb where nb.assistantDriverGh=assistantDriverGh and fromDateTime>=@startdate and fromDateTime<=@enddate) as decimal(18,2))as saveOil,
	 * from
	[#tianjiAssistant]


   union all
    select  
   (select gh from tb_driver where gh=DriverGh) as  gh,
   (select drivername from tb_driver where gh=DriverGh) as  name,
   (select department from tb_driver where gh=DriverGh) as  department,
    (select post from tb_driver where gh=DriverGh) as  post,
	 'driver' as workpost,
	               CAST((select sum(actualOil) from tb_driverReportInput nb where nb.DriverGh=DriverGh and fromDateTime>=@startdate and fromDateTime<=@enddate)  as decimal(18,2))as actualOil,
 CAST(comprehensiveOil- (select sum(actualOil) from tb_driverReportInput nb where nb.DriverGh=DriverGh  and fromDateTime>=@startdate and fromDateTime<=@enddate) as decimal(18,2))as saveOil,
	 * from
	[#xieqiaoDriver]
   union all


  	select  
	(select gh from tb_driver where gh=assistantDriverGh)   as  gh,
    (select drivername from tb_driver where gh=assistantDriverGh) as  name,
    (select department from tb_driver where gh=assistantDriverGh) as  department,
    (select post from tb_driver where gh=assistantDriverGh) as  post,
	 'Co-driver' as workpost,
	               CAST((select sum(actualOil) from tb_driverReportInput nb where nb.assistantDriverGh=assistantDriverGh and fromDateTime>=@startdate and fromDateTime<=@enddate) as decimal(18,2))as actualOil,
  CAST(comprehensiveOil-(select sum(actualOil) from tb_driverReportInput nb where nb.assistantDriverGh=assistantDriverGh and fromDateTime>=@startdate and fromDateTime<=@enddate) as decimal(18,2))as saveOil,
	 * from
[#xieqiaoAssistant]
-------------------------
  union all
    select  
   (select gh from tb_driver where gh=DriverGh) as  gh,
   (select drivername from tb_driver where gh=DriverGh) as  name,
   (select department from tb_driver where gh=DriverGh) as  department,
    (select post from tb_driver where gh=DriverGh) as  post,
	 'Co-driver' as workpost,
	               CAST((select sum(actualOil) from tb_driverReportInput nb where nb.DriverGh=DriverGh and fromDateTime>=@startdate and fromDateTime<=@enddate)  as decimal(18,2))as actualOil,
CAST(comprehensiveOil-(select sum(actualOil) from tb_driverReportInput nb where nb.DriverGh=DriverGh  and fromDateTime>=@startdate and fromDateTime<=@enddate) as decimal(18,2))as saveOil,
	 * from
#tianjiDriverDoAsistant


  union all
    select  
   (select gh from tb_driver where gh=DriverGh) as  gh,
   (select drivername from tb_driver where gh=DriverGh) as  name,
   (select department from tb_driver where gh=DriverGh) as  department,
    (select post from tb_driver where gh=DriverGh) as  post,
	 'Co-driver' as workpost,
	               CAST((select sum(actualOil) from tb_driverReportInput nb where nb.DriverGh=DriverGh and fromDateTime>=@startdate and fromDateTime<=@enddate)  as decimal(18,2))as actualOil,
  CAST(comprehensiveOil-(select sum(actualOil) from tb_driverReportInput nb where nb.DriverGh=DriverGh  and fromDateTime>=@startdate and fromDateTime<=@enddate) as decimal(18,2))as saveOil,
	 * from
	#xieqiaoDriverDoAsistant
END


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324641918&siteId=291194637