欢迎来到.net学习网

欢迎联系站长一起更新本网站!QQ:879621940

您当前所在位置:首页 » Sql随手笔记 » 正文

热门阅读

临时表

创建时间:2011年06月09日 20:39  阅读次数:(5216)
分享到:

ALTER proc [dbo].[e_pro_cost]
@type tinyint,
@IsWhere nvarchar(1000)
as
 begin
  if exists(select * from tempdb.dbo.sysobjects where id=object_id(N'tempdb.dbo.#t_equ_cost'))
   drop table #t_equ_cost
  create table #t_equ_cost
   (
   Batch varchar(50),
   eCostPrice float,
   MOID varchar(50),
   Date Datetime,
   PMCPlanner varchar(50),
   PMCPlannerName varchar(50)
   )
  insert into #t_equ_cost exec e_equ_cost @type
 end

 --将人工信息存入临时表
 begin
  if exists(select * from tempdb.dbo.sysobjects where id=object_id(N'tempdb.dbo.#t_hum_cost'))
   drop table #t_hum_cost
  create table #t_hum_cost
  (
   Batch varchar(50),
   hCostPrice float,
   MOID varchar(50),
   Date Datetime,
   PMCPlanner varchar(50),
   PMCPlannerName varchar(50)
  )
  insert into #t_hum_cost exec e_hum_cost @type
 end

 begin
  if exists(select * from tempdb.dbo.sysobjects where id=object_id(N'tempdb.dbo.#t_met_cost'))
   drop table #t_met_cost
  create table #t_met_cost
   (
   Batch varchar(50),
   mCostPrice float,
   MOID varchar(50),
   Date Datetime,
   PMCPlanner varchar(50),
   PMCPlannerName varchar(50)
   )
  insert into #t_met_cost exec e_met_cost @type
 end 

declare @sql varchar(2000)
 set @sql='select *,(eCostPrice+hCostPrice+mCostPrice) as TotalCostPrice from (
   select
   isnull(A.Batch,B.Batch) as Batch,
   isnull(A.MOID,B.MOID) as MOID,
   isnull(A.Date,B.Date) as Date,
   isnull(A.PMCPlanner,B.PMCPlanner) as PMCPlanner,
   isnull(A.PMCPlannerName,B.PMCPlannerName) as PMCPlannerName,
   isnull(A.eCostPrice,0) as eCostPrice,
   isnull(A.hCostPrice,0) as hCostPrice,
   isnull(B.mCostPrice,0) as mCostPrice from
    (
     select
     isnull(A.Batch,B.Batch) as Batch,
     isnull(A.MOID,B.MOID) as MOID,
     isnull(A.Date,B.Date) as Date,
     isnull(A.PMCPlanner,B.PMCPlanner) as PMCPlanner,
     isnull(A.PMCPlannerName,B.PMCPlannerName) as PMCPlannerName,
     A.eCostPrice,B.hCostPrice from #t_equ_cost A full outer join #t_hum_cost B on A.Batch=B.Batch
    )
   A full outer join #t_met_cost B on A.Batch=B.Batch
  ) A where '+@IsWhere;

execute (@sql)

 drop table #t_met_cost
 drop table #t_hum_cost
 drop table #t_equ_cost
GO

来源:
说明:所有来源为 .net学习网的文章均为原创,如有转载,请在转载处标注本页地址,谢谢!
【编辑:Wyf

打赏

取消

感谢您的支持,我会做的更好!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

最新评论

共有评论0条
  • 暂无任何评论,请留下您对本文章的看法,共同参入讨论!
发表评论:
留言人:
内  容:
请输入问题 35+7=? 的结果(结果是:42)
结  果: