SP--report存储过程
USE [edison_prc]
GO SET ANSI_NULLS ON
GO SET QUOTED_IDENTIFIER ON
GO ALTER PROCEDURE [dbo].[SP_Load_alert_Rpt] (@loadtype varchar(50),@officecode char(10),@consigneestr varchar(300),@movement varchar(10),@containertypelist varchar(500),@day1 int,@day2 int)
AS
BEGIN
declare @database_name varchar(100),@sql varchar(6000),@current_db varchar(100),@p int,@q int,@tm_len int,@etd_sql varchar(200),
@movement_sql varchar(100),@rpt_sql varchar(2000),@cbm_sql varchar(1000),@min_cbm varchar(10),@max_kgs varchar(10),@typelist varchar(500) create table #t_Loadalert_report ([ID] [int] IDENTITY(1,1) NOT NULL,officecode varchar(10),sono varchar(20),
so_seq int,po_no varchar(50),ref_no varchar(50),[Item Reference] varchar(100),shippername char(50),
actshippername char(50),bookingdate varchar(19),cargoreadydate varchar(10),CY_ClosingDate varchar(10),lportname char(50),
dportname char(50),Movement char(7),linercode char(10),Vesselname varchar(50),Voyage char(20),etd varchar(10),
eta varchar(10),QTY int,unitcode char(2),CBM decimal(12,3),KGS decimal(18,2),
[No.of 20GP] int,[No.of 20HC] int,[No.of 40GP] int,[No.of 40HC] int,
CONSTRAINT [PK_t_Loadalert_report] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY] create table #consignee(companycode varchar(10) COLLATE SQL_Latin1_General_CP1_CI_AS )
select @p=1,@q=1,@tm_len = len(ltrim(rtrim(@consigneestr)))
while @p<=@tm_len
begin
if substring(@consigneestr,@p,1)='/'
begin
insert into #consignee values(substring(@consigneestr,@q,@p - @q))
select @q = @p + 1
end
select @p = @p + 1
end
if @tm_len>=6
begin
insert into #consignee values(substring(@consigneestr,@q ,@p - @q))
end if(charindex('/',@containertypelist)=0)
select @typelist='[NO.of '+@containertypelist+']'
else
begin
select @typelist=',[NO.of '+substring(@containertypelist,1,charindex('/',@containertypelist)-1)+']'
select @containertypelist=substring(@containertypelist,charindex('/',@containertypelist)+1,len(@containertypelist)-charindex('/',@containertypelist))
while charindex('/',@containertypelist)>0
begin
select @typelist=@typelist+',[NO.of '+substring(@containertypelist,1,charindex('/',@containertypelist)-1)+']'
select @containertypelist=substring(@containertypelist,charindex('/',@containertypelist)+1,len(@containertypelist)-charindex('/',@containertypelist))
end
select @typelist=@typelist+(',[NO.of '+@containertypelist+']')
select @typelist=substring(@typelist,2,len(@typelist))
end SET @rpt_sql = 'select [ID],officecode,sono,so_seq,po_no,ref_no,[Item Reference],shippername,actshippername,
bookingdate,cargoreadydate,CY_ClosingDate,lportname,dportname,Movement,linercode,Vesselname,Voyage,etd,eta,
QTY,unitcode,CBM,KGS,'+@typelist+'
from #t_Loadalert_report' if @movement is null
select @movement_sql=''
else
begin
if @movement='CY/CY'
select @movement_sql=' and a.movement=''CY/CY'''
else if @movement='CFS/CY'
select @movement_sql=' and a.movement=''CFS/CY'''
else
select @movement_sql=' '
end if @day1 is null and @day2 is null
select @etd_sql=' '
else
begin
if @day1 is null
select @etd_sql=' and a.createdate<=convert(char(10),getdate() + '+convert(varchar(3),@day2)+',121)'
else
if @day2 is null
select @etd_sql=' and a.createdate>=convert(char(10),getdate() + '+convert(varchar(3),@day1)+',121)'
else
select @etd_sql=' and a.createdate>=convert(char(10),getdate() + '+convert(varchar(3),@day1)+',121) and a.createdate<=convert(char(10),getdate()+ '+convert(varchar(3),@day2)+',121)'
end if @loadtype='MIN_CBM'
begin
if @movement='CY/CY'
begin
select @max_kgs='si'
select @cbm_sql='join (select d.sono,d.so_seq,d.seq from
(select s.sono,s.so_seq,min(si.itemseq) seq,sum(si.cbm) si_cbm
from so_header s,so_item si
where s.sono=si.sono and s.so_seq=si.so_seq
and s.consigneecode in (select companycode from #consignee)
group by s.sono,s.so_seq) d,
(select s.sono,s.so_seq,sum(bc.qty*cast(isnull(spec_char,'''') as int)) bc_cbm
from so_header s,bookingcontainer bc,edispecialchecking ec
where s.sono=bc.bookingno and s.so_seq=bc.bookingseq and ec.checktype=''SO_CNTR_TYPE''
and ec.checkvalue=s.consigneecode and bc.containertype=ec.checkvalue_2
and s.consigneecode in (select companycode from #consignee)
group by s.sono,s.so_seq) b
where d.sono=b.sono and d.so_seq=b.so_seq
and d.si_cbm<b.bc_cbm) c on a.sono=c.sono and a.so_seq=c.so_seq'
end
else
begin
select @max_kgs='ld'
select @cbm_sql='join lp_detail ld (nolock) on a.sono=ld.sono and a.so_seq=ld.so_seq
join (select d.sono,d.so_seq,d.seq from
(select s.sono,s.so_seq,min(itemseq) seq,sum(ld.cbm) ld_cbm
from so_header s,lp_detail ld
where s.sono=ld.sono and s.so_seq=ld.so_seq
and s.consigneecode in (select companycode from #consignee)
group by s.sono,s.so_seq) d,
(select lh.lp_no,bs.max_cbm bs_cbm
from lp_header lh,base bs,edispecialchecking ec
where lh.containertype=bs.basecode and ec.checktype=''SO_CNTR_TYPE''
and ec.checkvalue=lh.consigneecode and bs.basecode=ec.checkvalue_2
and lh.consigneecode in (select companycode from #consignee)) b
where d.ld_cbm<b.bs_cbm) c on a.sono=c.sono and a.so_seq=c.so_seq'
end
end
else if @loadtype='MAX_KGS'
begin
if @movement='CY/CY'
begin
select @max_kgs='si'
select @cbm_sql='join (select d.sono,d.so_seq,d.seq from
(select s.sono,s.so_seq,min(si.itemseq) seq,sum(si.kgs) si_kgs
from so_header s,so_item si
where s.sono=si.sono and s.so_seq=si.so_seq
and s.consigneecode in (select companycode from #consignee)
group by s.sono,s.so_seq) d,
(select s.sono,s.so_seq,sum(bc.qty*cast(isnull(spec_char2,'''') as int)) bc_kgs
from so_header s,bookingcontainer bc,edispecialchecking ec
where s.sono=bc.bookingno and s.so_seq=bc.bookingseq and ec.checktype=''SO_CNTR_TYPE''
and ec.checkvalue=s.consigneecode and bc.containertype=ec.checkvalue_2
and s.consigneecode in (select companycode from #consignee)
group by s.sono,s.so_seq) b
where d.sono=b.sono and d.so_seq=b.so_seq
and d.si_kgs>b.bc_kgs) c on a.sono=c.sono and a.so_seq=c.so_seq'
end
else
begin
select @max_kgs='ld'
select @cbm_sql='join lp_detail ld (nolock) on a.sono=ld.sono and a.so_seq=ld.so_seq
join (select d.sono,d.so_seq,d.seq from
(select s.sono,s.so_seq,min(itemseq) seq,sum(ld.kgs) ld_kgs
from so_header s,lp_detail ld
where s.sono=ld.sono and s.so_seq=ld.so_seq
and s.consigneecode in (select companycode from #consignee)
group by s.sono,s.so_seq) d,
(select lh.lp_no,bs.max_kgs bs_kgs
from lp_header lh,base bs,edispecialchecking ec
where lh.containertype=bs.basecode and ec.checktype=''SO_CNTR_TYPE''
and ec.checkvalue=lh.consigneecode and bs.basecode=ec.checkvalue_2
and lh.consigneecode in (select companycode from #consignee)) b
where d.ld_kgs<b.bs_kgs) c on a.sono=c.sono and a.so_seq=c.so_seq'
end
end
else
begin
select @max_kgs='si'
select @cbm_sql='join (select d.sono,d.so_seq,d.seq from
(select s.sono,s.so_seq,min(si.itemseq) seq,sum(si.cbm) si_cbm
from so_header s,so_item si
where s.sono=si.sono and s.so_seq=si.so_seq
and s.consigneecode in (select companycode from #consignee)
group by s.sono,s.so_seq) d,
(select s.sono,s.so_seq,sum(bc.qty*cast(isnull(spec_char,'''') as int)) bc_cbm
from so_header s,bookingcontainer bc,edispecialchecking ec
where s.sono=bc.bookingno and s.so_seq=bc.bookingseq and ec.checktype=''SO_CNTR_TYPE_2''
and ec.checkvalue=s.consigneecode and bc.containertype=ec.checkvalue_2
and s.consigneecode in (select companycode from #consignee)
group by s.sono,s.so_seq) b
where d.sono=b.sono and d.so_seq=b.so_seq
and d.si_cbm=b.bc_cbm) c on a.sono=c.sono and a.so_seq=c.so_seq'
end set @current_db = db_name()
if @officecode=''
declare database_cur cursor for
select db_name,officecode from systeminfo where linerflag<>'Y' and linercode='nvocc'
and status='Y' and officecode not in ('ASH','SGP','STO') and db_name not like '%test%' and db_name not like '%imp%' else
declare database_cur cursor for
select db_name,officecode from systeminfo where linerflag<>'Y' and linercode='nvocc'
and status='Y' and officecode=@officecode
and db_name not like '%test%' and db_name not like '%imp%' --select db_name,officecode from systeminfo where db_name='edison_prc_hkg_nvo_test2' open database_cur
fetch database_cur into @database_name,@officecode
while @@fetch_status=0 begin
set @sql='insert into #t_Loadalert_report' + ' select ''' + @officecode + ''',a.sono,
a.so_seq,
si.po_no,
si.ref_no,
(case when (select itemremarks from companyvalidpo (nolock) where si.po_no=companyvalidpo.pono and si.ref_no=companyvalidpo.itemno
and si.shipmentno=companyvalidpo.shipmentno and a.consigneecode=companyvalidpo.companycode and si.destcode=companyvalidpo.destcode
and companyvalidpo.companytype =''C'')='''' then '''' else (select itemremarks from companyvalidpo (nolock) where si.po_no=companyvalidpo.pono and si.ref_no=companyvalidpo.itemno
and si.shipmentno=companyvalidpo.shipmentno and a.consigneecode=companyvalidpo.companycode and si.destcode=companyvalidpo.destcode
and companyvalidpo.companytype =''C'') end) as [Item Reference],
company.companyname,
company_c.companyname,
convert(varchar(19),a.bookingdate,120),
convert(varchar(10),a.cargoreadydate,120),
convert(varchar(10),vv.CY_ClosingDate,120),
a.lportname,
a.dportname,
a.Movement,
vv.linercode,
ve.Vesselname,
vv.Voyage,
convert(varchar(10),vv.etd_date,120),
convert(varchar(10),vv.eta_datedport,120),
'+@max_kgs+'.QTY,
'+@max_kgs+'.unitcode,
'+@max_kgs+'.CBM,
'+@max_kgs+'.KGS,
(select qty from bookingcontainer where bookingno=a.sono and bookingseq=a.so_seq and '+@max_kgs+'.itemseq=c.seq and containertype=''20GP'') as [No.of20GP],
(select qty from bookingcontainer where bookingno=a.sono and bookingseq=a.so_seq and '+@max_kgs+'.itemseq=c.seq and containertype=''20HC'') as [No.of20HC],
(select qty from bookingcontainer where bookingno=a.sono and bookingseq=a.so_seq and '+@max_kgs+'.itemseq=c.seq and containertype=''40GP'') as [No.of40GP],
(select qty from bookingcontainer where bookingno=a.sono and bookingseq=a.so_seq and '+@max_kgs+'.itemseq=c.seq and containertype=''40HC'') as [No.of40HC]
from so_header a
join so_item si (nolock) on
a.sono=si.sono and a.so_seq=si.so_seq
join vesvoyforwarder vv (nolock) on vv.vesvoyno=a.vesvoyno
join vessel ve (nolock) on ve.vesselcode=vv.vesselcode
join companyheader company (nolock) on a.shippercode=company.companycode
join companyheader company_c (nolock) on a.act_shippercode=company_c.companycode
'+@cbm_sql+' where a.consigneecode in (select companycode from #consignee) '+ @etd_sql + @movement_sql
exec( 'USE ' + @database_name + ' ' + @sql)
fetch database_cur into @database_name ,@officecode
end
close database_cur
deallocate database_cur Exec(@rpt_sql) drop table #t_Loadalert_report
drop table #consignee
end GO
SP--report存储过程的更多相关文章
- sql的sp存储过程详解
store procedure (存储过程) http://www.cnblogs.com/xiangzhong/p/5038338.html 调优的几个关键的步骤--sp_lock,sp_who h ...
- asp.net执行SqlServer存储过程!(详解!)
ASP.NET执行存储过程 一. 执行一个没有参数的存储过程的代码如下: connectionString为连接字符串 SqlConnection conn=new SqlConnection(con ...
- SQL存储过程基础(从基础开始学,加油!)
Transact-SQL中的存储过程,非常类似于Java语言中的方法,它可以重复调用.当存储过程执行一次后,可以将语句缓存中,这样下次执行的时候直接使用缓存中的语句.这样就可以提高存储过程的性能. Ø ...
- SQL Server 存储过程
Transact-SQL中的存储过程,非常类似于Java语言中的方法,它可以重复调用.当存储过程执行一次后,可以将语句缓存中,这样下次执行的时候直接使用缓存中的语句.这样就可以提高存储过程的性能. Ø ...
- [转]SQL Server 存储过程 一些常用用法(事物、异常捕捉、循环)
最新更新请访问: http://denghejun.github.io Transact-SQL中的存储过程,非常类似于Java语言中的方法,它可以重复调用.当存储过程执行一次后,可以将语句缓存中 ...
- T-SQL存储过程、游标
Transact-SQL中的存储过程,非常类似于Java语言中的方法,它可以重复调用.当存储过程执行一次后,可以将语句缓存中,这样下次执行的时候直接使用缓存中的语句.这样就可以提高存储过程的性能. Ø ...
- java 调用 sql server存储过程
Transact-SQL中的存储过程,非常类似于Java语言中的方法,它可以重复调用.当存储过程执行一次后,可以将语句缓存中,这样下次执行的时候直接使用缓存中的语句.这样就可以提高存储过程的性能. Ø ...
- Sqlserver 存储过程
转载自:http://www.cnblogs.com/hoojo/archive/2011/07/19/2110862.html Transact-SQL中的存储过程,非常类似于Java语言中的方法, ...
- SQL Server之存储过程基础知
什么是存储过程呢?存储过程就是作为可执行对象存放在数据库中的一个或多个SQL命令. 通俗来讲:存储过程其实就是能完成一定操作的一组SQL语句. 那为什么要用存储过程呢?1.存储过程只在创造时进行编译, ...
- T-SQL 存储过程
Transact-SQL中的存储过程,非常类似于Java语言中的方法,它可以重复调用.当存储过程执行一次后,可以将语句缓存中,这样下次执行的时候直接使用缓存中的语句.这样就可以提高存储过程的性能. Ø ...
随机推荐
- 【一个小功能】从js判断ie版本,浅谈navigator对象的appName属性
判断IE版本主要的是获取两个属性,a.当前浏览器名称,b.当前浏览器版本,为此不得不了解navigator对象. 先贴代码 window.onload = function() { var brows ...
- 设计模式学习--面向对象的5条设计原则之开放封闭原则--OCP
一.OCP简介(OCP--Open-Closed Principle):Software entities(classes,modules,functions,etc.) should be open ...
- Asp.net core WebApi 使用Swagger生成帮助页实例
最近我们团队一直进行.net core的转型,web开发向着前后端分离的技术架构演进,我们后台主要是采用了asp.net core webapi来进行开发,开始每次调试以及与前端人员的沟通上都存在这效 ...
- swift 基础小结01 --delegate、Optional、GCD的使用、request请求、网络加载图片并保存到沙箱、闭包以及桥接
本文主要记录swift中delegate的使用.“?!”Optional的概念.GCD的使用.request请求.网络加载图片并保存到沙箱.闭包以及桥接. 一.delegate的使用 swift中de ...
- 设计模式(15)--Interpreter(解释器模式)--行为型
作者QQ:1095737364 QQ群:123300273 欢迎加入! 1.模式定义: 解释器模式是类的行为模式.给定一个语言之后,解释器模式可以定义出其文法的一种表示,并同时提供一个解 ...
- bzoj 2406: 矩阵 ——solution
对于100%的数据满足N,M<=200,0<=L<=R<=1000,0<=Aij<=1000 http://www.lydsy.com/JudgeOnline/pr ...
- thymeleaf标签使用方法总结
https://blog.csdn.net/quuqu/article/details/52511933 常用th标签https://www.cnblogs.com/suncj/p/4030393.h ...
- 小记SharePoint REST API Search和COM
1.管理员身份Visual Studio,新建类项目 SPCOM 2.编写逻辑实现代码 重点关注搜索结果的属性包括: Title,Author,Path,Description,HitHighligh ...
- 树莓派 引脚及接口图 AV接口顺序
树莓派 引脚图 注:本表格适用于各版本,并且兼容26Pin的树莓派B,树莓派B为26Pin,其引脚对应于上表的前26Pin. 树莓派 接口图 AV接口又称(RCA),AV接口算是出现比较早的一种接 ...
- 与HttpSession相关的监听器
概述 与HttpSession相关的监听器有四个:分别是HttpSessionListener.HttpSessionAttributeListener.HttpSessionBindingListe ...