六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 324|回复: 0

Sybase存储过程中查询动态表(按天分表)的处理

[复制链接]

升级  81.33%

50

主题

50

主题

50

主题

秀才

Rank: 2

积分
172
 楼主| 发表于 2013-1-30 00:20:51 | 显示全部楼层 |阅读模式
在ASE12.5.1环境下测试:
--exec sp_test_count '2009-10-01', '2009-10-08'--drop PROCEDURE  sp_test_count    CREATE PROCEDURE sp_test_count  (      @starttime datetime,      @endtime datetime  )  AS  declare @totalCount numeric(18,0)   declare @tempCount  numeric(18,0)  declare @day datetime  declare @nextday datetime  declare @datestr varchar(24)  declare @sql varchar(200)  declare @tempday datetime  select @totalCount=0   select @tempCount=0   select @day=@starttime  select @nextday=@endtime    create table #t1(total numeric(18,0))    --创建临时表,用来存放所需统计数据    while(@day<@nextday)  begin     select @tempday=@day     --convert转换后的结果为2009/10/01     select @datestr=convert(varchar(20), @day,111 )    --获得日期的字符串格式:20090720,以匹配t_test_20091001,t_test_20091002 ... 07      select @datestr=substring(@datestr,1,4)+substring(@datestr,6,2)+substring(@datestr,9,2)       select @sql ="insert into #t1 select count(*) from t_test_"+@datestr + " a  where a.name='abc'  and exists (select number from t_test2 where a.no=number)"    exec(@sql)  --对日期中的分量——天进行加1      select @day=dateadd(dd ,1, @tempday)  end    select @totalCount=sum(total) from #t1   drop table #t1
注:
1.exec(@sql)不能返回查询结果,所以需要将动态查询结果先插入到临时表,再select * from #t1返回结果。
2.匹配好的t_test_20091001,必须进行重命名,如上面的a,a.name。否则,会报如下的错误:
Server Message:  Number  102, Severity  15Server 'SYBASE', Line 1:Incorrect syntax near '='.
3.SQL Advantage中查看一条语句的执行时间,在这条语句的前后加上:
select convert(varchar(24) ,  getdate(),109 )
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表