一个很复杂的查询,代码一段一段执行速度正常。但是放到一个存储过程就完了,几个小时都出不来,这是为什么啊

解决方案 »

  1.   

    --procedure中加2句可能比較好
    create procedure XX 
    as
    set Lock_TimeOut 5000
    set XACT_Abort on
    .....
      

  2.   

    CREATE proc Report_stockcost
    (
    @ssdate char(10)
    )
    as
    set nocount on
    begin tran
    declare @name varchar(8000)
    create table #stock_price1_report (jiezhi int,begin_price1 decimal(12,2),in_in_i decimal(12,2),in_in_t decimal(12,2),
    in_in_sale decimal(12,2),end_price1 decimal(12,2),DD_month_sale decimal(12,2))
    insert into #stock_price1_report(jiezhi) select jiezhi from otherDatebase.dbo.jiezhis----set @name='select isnull(jiezhi,0)jiezhi,sum(chgstock*isnull(price1,0))price1 into #temp from DDreport_Stock_'+cast(year(dateadd(month,-1,@ssdate))as varchar)
    +'Month'+cast(month(dateadd(month,-1,@ssdate))as varchar)+' group by isnull(jiezhi,0);update #stock_price1_report set begin_price1=#temp.price1
    from #temp where #stock_price1_report.jiezhi=#temp.jiezhi;drop table #temp;
    select isnull(jiezhi,0)jiezhi,sum(chgstock*isnull(price1,0))price1 into #temp1 from DDreport_Stock_'+cast(year(@ssdate)as varchar)
    +'Month'+cast(month(@ssdate)as varchar)+' group by isnull(jiezhi,0);update #stock_price1_report set end_price1=#temp1.price1
    from #temp1 where #stock_price1_report.jiezhi=#temp1.jiezhi;drop table #temp1;
    select isnull(jiezhi,0)jiezhi,sum(chgstock*isnull(price1,0))price1 into #temp2 from DDreport_Stockhistory_'+cast(year(@ssdate)as varchar)
    +'Month'+cast(month(@ssdate)as varchar)+' where status=''I'' group by isnull(jiezhi,0);update #stock_price1_report set in_in_i=#temp2.price1
    from #temp2 where #stock_price1_report.jiezhi=#temp2.jiezhi;drop table #temp2;
    select isnull(jiezhi,0)jiezhi,sum(chgstock*isnull(price1,0))price1 into #temp3 from DDreport_Stockhistory_'+cast(year(@ssdate)as varchar)
    +'Month'+cast(month(@ssdate)as varchar)+' where status=''T'' group by isnull(jiezhi,0);update #stock_price1_report set in_in_t=#temp3.price1
    from #temp3 where #stock_price1_report.jiezhi=#temp3.jiezhi;drop table #temp3;'
    exec(@name)
    ----
    create table #stock_sale(someid int,order_id int,type int,t_type int,jiezhi int,productid int,price1 decimal(12,2),num int,dep varchar(50))
    ----select a.order_id into #temp4 from otherDatebase.dbo.orders a(nolock) left join otherDatebase.dbo.table3 b (nolock) on b.order_id=a.order_id
    left join otherDatebase.dbo.table2 c (nolock) on c.order_id=a.order_id where a.status<>'B'and
    (datediff(day,fachu_time,qita_time)>0 or datediff(day,fachu_time,qita_time)<0) and b.somemoney>=0 and b.qita_time 
    between @ssdate and dateadd(month,1,@ssdate)
    insert into #temp4 select a.order_id from otherDatebase.dbo.table2 a (nolock) left join otherDatebase.dbo.orders b (nolock) on b.order_id=a.order_id where fachu_time 
    between @ssdate and dateadd(month,1,@ssdate)and b.status<>'B'
    ---insert into #stock_sale(order_id,type,productid,num,jiezhi)
    select #temp4.order_id,0,a.productid,a.ordernum,isnull(b.jiezhi,0) from #temp4 join otherDatebase.dbo.order_item a on #temp4.order_id=a.order_id 
    join otherDatebase.dbo.products b on a.productid=b.productid where a.ordernum>0---select distinct a.order_id into #stock_sale_guke from otherDatebase.dbo.order_item_rtn a
    join otherDatebase.dbo.table2 b on a.order_id=b.order_id where b.reason like '退货1%'
    and a.lastchgdate between @ssdate and dateadd(month,1,@ssdate)
    ---
    select a.someid,a.order_id into #shangmen
    from otherDatebase.dbo.table1 a join otherDatebase.dbo.reinvoice b on a.someid=b.someid
    where a.status in('T','E')and a.somemoney<=0 and (ABS(b.money1)=0) AND (ABS(b.money2)>0)
    and a.qita_time between @ssdate and dateadd(month,1,@ssdate)
    ---select a.order_id into #stock_sale_kuaidi from otherDatebase.dbo.table3 a join otherDatebase.dbo.table2 b on 
    a.order_id=b.order_id where 
    b.status='T'and a.somemoney<=0 and b.reason like '退货3%'
    and a.qita_time between @ssdate and dateadd(month,1,@ssdate)
    ---insert into #stock_sale(order_id,type,t_type,productid,num)
    select #stock_sale_guke.order_id,1,2,a.productid,-a.num
    from #stock_sale_guke join otherDatebase.dbo.order_item_rtn a on #stock_sale_guke.order_id=a.order_id
    where a.lastchgdate between @ssdate and dateadd(month,1,@ssdate)insert into #stock_sale(someid,order_id,type,t_type,productid,num)
    select #shangmen.someid,#shangmen.order_id,1,3,a.productid,-a.returnnum
     from #shangmen join otherDatebase.dbo.some_item a on #shangmen.someid=a.someid
    where a.returnnum>0 and a.exchangenum=0update #stock_sale set #stock_sale.jiezhi=isnull(b.jiezhi,0) from otherDatebase.dbo.products b where #stock_sale.productid=b.productidinsert into #stock_sale(order_id,type,t_type,productid,num,jiezhi)
    select #stock_sale_kuaidi.order_id,1,1,a.productid,-a.ordernum,isnull(b.jiezhi,0) from #stock_sale_kuaidi join otherDatebase.dbo.order_item a 
    on #stock_sale_kuaidi.order_id=a.order_id join otherDatebase.dbo.products b on a.productid=b.productid where a.ordernum>0
    ---set @name='update #stock_sale set #stock_sale.price1=a.price1 from DDreport_sale_'+cast(year(dateadd(month,-2,@ssdate))as varchar)+'Month'+cast(month(dateadd(month,-2,@ssdate))as varchar)
    +' a where #stock_sale.productid=a.productid and #stock_sale.order_id=a.order_id;
    update #stock_sale set #stock_sale.price1=a.price1 from DDreport_sale_'+cast(year(dateadd(month,-1,@ssdate))as varchar)+'Month'+cast(month(dateadd(month,-1,@ssdate))as varchar)
    +' a where #stock_sale.productid=a.productid and #stock_sale.order_id=a.order_id;
    update #stock_sale set #stock_sale.price1=a.price1 from DDreport_sale_'+cast(year(@ssdate)as varchar)+'Month'+cast(month(@ssdate)as varchar)
    +' a where #stock_sale.productid=a.productid and #stock_sale.order_id=a.order_id;'
    exec (@name)
    ----
      

  3.   

    select isnull(jiezhi,0)jiezhi,sum(num*isnull(price1,0))price1 into #tem from #stock_sale group by isnull(jiezhi,0)
    update #stock_price1_report set in_in_sale=#tem.price1 from #tem where #stock_price1_report.jiezhi=#tem.jiezhi
    ----
    select * into otherDatebase.dbo.noprice1 from #stock_sale where price1 is null
    -------set @name='select isnull(jiezhi,0)jiezhi,sum(num*isnull(price1,0))price1 into #tem1 from DD_month_sale'+cast(month(@ssdate)as varchar)+
    ' where stocktype not in(1,2,11) group by isnull(jiezhi,0);update #stock_price1_report set DD_month_sale=#tem1.price1 from #tem1 where 
    #stock_price1_report.jiezhi=#tem1.jiezhi;drop table #tem1'
    exec (@name)
    --update #stock_sale set dep='部门1'where productid in(
    select otherDatebase.dbo.DD_OrgBillnoType.productid from otherDatebase.dbo.DD_OrgBillnoType 
    join #stock_sale on otherDatebase.dbo.DD_OrgBillnoType.productid=#stock_sale.productid 
    and otherDatebase.dbo.DD_OrgBillnoType.jiezhi=#stock_sale.jiezhi
    where otherDatebase.dbo.DD_OrgBillnoType.jiezhi=0 and otherDatebase.dbo.DD_OrgBillnoType.status=1)--介质1(部门1)
    update #stock_sale set dep='部门2'where jiezhi=0 and dep is null--介质1(部门2)
    update #stock_sale set dep='部门2'where jiezhi=13--介质2(部门2)
    update #stock_sale set dep='部门2'where jiezhi=15--介质3(部门2)
    update #stock_sale set dep='部门3'where jiezhi in(1,2,3,4,5,7,8,9,10,11,14,16,18) and dep is null--介质4(部门3)
    update #stock_sale set dep='部门4'where productid in(select otherDatebase.dbo.DD_OrgBillnoType.productid from otherDatebase.dbo.DD_OrgBillnoType 
    join #stock_sale on otherDatebase.dbo.DD_OrgBillnoType.productid=#stock_sale.productid
    and otherDatebase.dbo.DD_OrgBillnoType.jiezhi=#stock_sale.jiezhi
    where otherDatebase.dbo.DD_OrgBillnoType.jiezhi=12 and otherDatebase.dbo.DD_OrgBillnoType.status=1)
    update #stock_sale set dep='部门5'where jiezhi=12 and dep is null
    update #stock_sale set dep='部门5'where jiezhi=17--介质6产品
    --select cast(jiezhi as varchar(15))jiezhi,dep,num,price1 into #next from #stock_sale
    update #next set jiezhi='介质1'where jiezhi='0'
    update #next set jiezhi='介质2'where jiezhi='13'
    update #next set jiezhi='介质3'where jiezhi='15'
    update #next set jiezhi='介质4'where jiezhi in('1','2','3','4','5','7','8','9','10','11','14','16','18')
    update #next set jiezhi='介质5'where jiezhi in('12')
    update #next set jiezhi='介质6'where jiezhi in('17')
    update #next set jiezhi='介质7'where jiezhi in('19')select isnull(jiezhi,0)jiezhi,dep,sum(num*isnull(price1,0))price1 into otherDatebase.dbo.[next] from #next group by isnull(jiezhi,0),dep
    --
    select jiezhi=case jiezhi when 0 then '介质1'when 13 then '介质2'when 15 then '介质3'
    when 1 then '介质4'when 2 then '介质4'when 3 then '介质4'when 4 then '介质4'when 5 then '介质4'when 6 then '介质4'
    when 7 then '介质4'when 8 then '介质4' when 9 then '介质4'when 10 then '介质4'when 11 then '介质4'when 14 then '介质4'when 16 then '介质4'
    when 18 then '介质4'when 12 then '介质5'when 17 then '介质6'when 19 then '介质7'end,isnull(begin_price1,0)begin_price1,
    isnull(in_in_i,0)in_in_i,isnull(in_in_t,0)in_in_t,isnull(in_in_sale,0)in_in_sale,isnull(end_price1,0)end_price1,
    isnull(DD_month_sale,0)DD_month_sale into #ttemp_stock_price1_report from #stock_price1_reportselect jiezhi,sum(begin_price1)begin_price1,sum(in_in_i)in_in_i,sum(in_in_t)in_in_t,sum(in_in_sale)in_in_sale,
    sum(end_price1)end_price1,sum(DD_month_sale)DD_month_sale into otherDatebase.dbo.stock_price1_report 
    from #ttemp_stock_price1_report group by jiezhi---删除临时表
    drop table #stock_price1_report,#stock_saleif @@error<>0
    rollback tran
    else
    commit tranGO
      

  4.   

    郁闷啊,每步执行是可以的,一放到存储过程就不行了啊。应该和lock无关:(
      

  5.   

    调试的时候果然有问题了,显示[Microsoft][ODBC SQL Server Driver]字符串数据,右截位因不是本机,单步调试不可用。可是我一段段执行的时候没这错误啊
      

  6.   

    不是,上次我调试的时候用的是'时间',所以出了错,应该是只输入时间的,所以问题继续存在,和[Microsoft][ODBC SQL Server Driver]字符串数据,右截位 无关