CREATE PROCEDURE [wzbb] 
@ny varchar(100),
@nm varchar(100),
@py varchar(100),
@pm varchar(100)
AS
truncate table ttmp
insert  ttmp(sbbm,mingcheng,xinhao,dl,zl,danwei,danjia,pnum,pmoney,nrnum,nrmoney,ncnum,ncmoney,nknum,nkmoney)
select a.申报部门,a.产品名称,a.规格型号,a.产品大类,a.产品子类,a.单位,isnull(a.单价,0) 单价,
isnull(a.库存数量+d.上月入库数量-e.上月数量,0) pnum,
isnull(a.金额+d.上月入库金额-e.上月总金额,0) pmoney,
isnull(b.入库数量,0) nrnum,
isnull(b.入库金额,0) nrmoney,
isnull(c.数量,0) ncnum,
isnull(c.总金额,0) ncmoney,
isnull(a.库存数量,0) nknum,
isnull(a.金额,0) nkmoney
from t_ck_tmp a
left join (select 产品名称,规格型号,单价,sum(isnull(入库数量,0)) 入库数量,sum(isnull(入库金额,0)) 入库金额 from t_ck_rk  where year(入库日期)=@ny and month(入库日期)=@nm group by 产品名称,规格型号,单价) b on a.产品名称=b.产品名称 and a.规格型号=b.规格型号 and a.单价=b.单价
left join (select 产品名称,规格型号,单价,sum(isnull(入库数量,0)) 上月入库数量,sum(isnull(入库金额,0)) 上月入库金额 from t_ck_rk  where year(入库日期)=@py and month(入库日期)=@pm group by 产品名称,规格型号,单价) d on a.产品名称=d.产品名称 and a.规格型号=d.规格型号 and a.单价=d.单价
left join (select 产品名称,规格型号,单价,sum(isnull(数量,0)) 数量,sum(isnull(总金额,0)) 总金额 from t_ck_ll  where year(领料日期)=@ny and month(领料日期)=@nm group by 产品名称,规格型号,单价) c on a.产品名称=c.产品名称 and a.规格型号=c.规格型号 and a.单价=c.单价
left join (select 产品名称,规格型号,单价,sum(isnull(数量,0)) 上月数量,sum(isnull(总金额,0)) 上月总金额 from t_ck_ll  where year(领料日期)=@py and month(领料日期)=@pm group by 产品名称,规格型号,单价) e on a.产品名称=e.产品名称 and a.规格型号=e.规格型号 and a.单价=e.单价
where (year(a.入库日期)=convert(int,@py) and month(a.入库日期)=convert(int,@pm)) or  (year(a.入库日期)=convert(int,@ny) and month(a.入库日期)=convert(int,@nm))
order by 申报部门,产品大类,产品子类
GO

解决方案 »

  1.   

    CREATE PROCEDURE [wzbb] 
    @ny varchar(2),
    @nm varchar(2),
    @py varchar(2),
    @pm varchar(2)
    -------------
    变量类型长度的不对!!
      

  2.   

    应为
    @ny varchar(2),
    @nm varchar(2),
    @py varchar(2),
    @pm varchar(2)长度不够
      

  3.   

    --从你的查询来看,应该是参数定义的太小,试试:CREATE PROCEDURE [wzbb] 
    @ny varchar(10),
    @nm varchar(20),
    @py varchar(20),
    @pm varchar(20)
    AS
    truncate table ttmp
    insert  ttmp(sbbm,mingcheng,xinhao,dl,zl,danwei,danjia,pnum,pmoney,nrnum,nrmoney,ncnum,ncmoney,nknum,nkmoney)
    select a.申报部门,a.产品名称,a.规格型号,a.产品大类,a.产品子类,a.单位,isnull(a.单价,0) 单价,
    isnull(a.库存数量+d.上月入库数量-e.上月数量,0) pnum,
    isnull(a.金额+d.上月入库金额-e.上月总金额,0) pmoney,
    isnull(b.入库数量,0) nrnum,
    isnull(b.入库金额,0) nrmoney,
    isnull(c.数量,0) ncnum,
    isnull(c.总金额,0) ncmoney,
    isnull(a.库存数量,0) nknum,
    isnull(a.金额,0) nkmoney
    from t_ck_tmp a
    left join (select 产品名称,规格型号,单价,sum(isnull(入库数量,0)) 入库数量,sum(isnull(入库金额,0)) 入库金额 from t_ck_rk  where year(入库日期)=@ny and month(入库日期)=@nm group by 产品名称,规格型号,单价) b on a.产品名称=b.产品名称 and a.规格型号=b.规格型号 and a.单价=b.单价
    left join (select 产品名称,规格型号,单价,sum(isnull(入库数量,0)) 上月入库数量,sum(isnull(入库金额,0)) 上月入库金额 from t_ck_rk  where year(入库日期)=@py and month(入库日期)=@pm group by 产品名称,规格型号,单价) d on a.产品名称=d.产品名称 and a.规格型号=d.规格型号 and a.单价=d.单价
    left join (select 产品名称,规格型号,单价,sum(isnull(数量,0)) 数量,sum(isnull(总金额,0)) 总金额 from t_ck_ll  where year(领料日期)=@ny and month(领料日期)=@nm group by 产品名称,规格型号,单价) c on a.产品名称=c.产品名称 and a.规格型号=c.规格型号 and a.单价=c.单价
    left join (select 产品名称,规格型号,单价,sum(isnull(数量,0)) 上月数量,sum(isnull(总金额,0)) 上月总金额 from t_ck_ll  where year(领料日期)=@py and month(领料日期)=@pm group by 产品名称,规格型号,单价) e on a.产品名称=e.产品名称 and a.规格型号=e.规格型号 and a.单价=e.单价
    where (year(a.入库日期)=convert(int,@py) and month(a.入库日期)=convert(int,@pm)) or  (year(a.入库日期)=convert(int,@ny) and month(a.入库日期)=convert(int,@nm))
    or
      

  4.   

    CREATE PROCEDURE [wzbb] 
    @ny varchar(2),
    @nm varchar(2),
    @py varchar(2),
    @pm varchar(2)
    AS
    truncate table ttmp
    insert  ttmp(sbbm,mingcheng,xinhao,dl,zl,danwei,danjia,pnum,pmoney,nrnum,nrmoney,ncnum,ncmoney,nknum,nkmoney)
    select a.申报部门,a.产品名称,a.规格型号,a.产品大类,a.产品子类,a.单位,isnull(a.单价,0) 单价,
    isnull(a.库存数量+d.上月入库数量-e.上月数量,0) pnum,
    isnull(a.金额+d.上月入库金额-e.上月总金额,0) pmoney,
    isnull(b.入库数量,0) nrnum,
    isnull(b.入库金额,0) nrmoney,
    isnull(c.数量,0) ncnum,
    isnull(c.总金额,0) ncmoney,
    isnull(a.库存数量,0) nknum,
    isnull(a.金额,0) nkmoney
    from t_ck_tmp a
    left join (select 产品名称,规格型号,单价,sum(isnull(入库数量,0)) 入库数量,sum(isnull(入库金额,0)) 入库金额 from t_ck_rk  where year(入库日期)=@ny and month(入库日期)=@nm group by 产品名称,规格型号,单价) b on a.产品名称=b.产品名称 and a.规格型号=b.规格型号 and a.单价=b.单价
    left join (select 产品名称,规格型号,单价,sum(isnull(入库数量,0)) 上月入库数量,sum(isnull(入库金额,0)) 上月入库金额 from t_ck_rk  where year(入库日期)=@py and month(入库日期)=@pm group by 产品名称,规格型号,单价) d on a.产品名称=d.产品名称 and a.规格型号=d.规格型号 and a.单价=d.单价
    left join (select 产品名称,规格型号,单价,sum(isnull(数量,0)) 数量,sum(isnull(总金额,0)) 总金额 from t_ck_ll  where year(领料日期)=@ny and month(领料日期)=@nm group by 产品名称,规格型号,单价) c on a.产品名称=c.产品名称 and a.规格型号=c.规格型号 and a.单价=c.单价
    left join (select 产品名称,规格型号,单价,sum(isnull(数量,0)) 上月数量,sum(isnull(总金额,0)) 上月总金额 from t_ck_ll  where year(领料日期)=@py and month(领料日期)=@pm group by 产品名称,规格型号,单价) e on a.产品名称=e.产品名称 and a.规格型号=e.规格型号 and a.单价=e.单价
    where (year(a.入库日期)=convert(int,@py) and month(a.入库日期)=convert(int,@pm)) or  (year(a.入库日期)=convert(int,@ny) and month(a.入库日期)=convert(int,@nm))
    order by 申报部门,产品大类,产品子类select * from ttmp        --显示结果!GO