存储过程出错,连接也报关闭。
CREATE procedure huawei_dest
@first_yy char(4), @first_mm char(2), @first_dd char(2),
@first_hh char(2),@end_hh char(2),
@scope char(12)
as
begin
declare @first_time varchar(20),@last_time varchar(20),@table_name char(15),@temp_hh int
declare @mysql varchar(1000)
create table #huawei_detail(called char(8),owner int,call_times int,cduration int,occur_time datetime null)
select @temp_hh=convert(int,@first_hh)
if (@temp_hh<10) select @first_hh='0'+convert(char(1),@temp_hh)
else select @first_hh=convert(char(2),@temp_hh)
select @temp_hh=convert(int,@end_hh)
if (@temp_hh<10) select @end_hh='0'+convert(char(1),@temp_hh)
else select @end_hh=convert(char(2),@temp_hh)
select @last_time=@first_yy + '-' + @first_mm + '-' + @first_dd + ' ' + @end_hh + ':00:00'
select @first_time=@first_yy + '-' + @first_mm + '-' + @first_dd + ' ' + @first_hh + ':00:00'
select @table_name='upcdr06' + @first_mm
if (@scope='0')
begin
select @mysql='insert into #huawei_detail select called,owner,count(*),sum(cduration),occur_time from '+ @table_name +' where occur_time>='''+ @first_time +''' and occur_time<='''+ @last_time + ''' group by called,owner,occur_time'
end
else
begin
select @mysql='insert into #huawei_detail select called,owner,count(*),sum(cduration),occur_time from '+ @table_name +' where occur_time>='''+ @first_time +''' and occur_time<='''+ @last_time +''' and called ='''+ @scope +''' group by called,owner,occur_time'
end
exec(@mysql)
insert into #huawei_detail select called,20,sum(call_times),sum(cduration),occur_time from #huawei_detail group by called,occur_time
insert into #huawei_detail select called,30,sum(call_times),sum(cduration),'2006-09-01 1:00:00' from #huawei_detail where owner=20 group by called
select * from #huawei_detail order by called,occur_time,owner
end
最后那句insert有问题,报"将 expression 转换为数据类型 int 时发生算术溢出错误。"我感觉是那个日期格式可能有问题。把这行注释掉就可以在SQL查询器里正确输出。