首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

sql下标越界 怎么优化

2012-02-20 
sql下标越界如何优化?按月统计下标越界,如何更改endtime出错,因为有的月份没的31号。如何优化fori1tomonth

sql下标越界 如何优化?
按月统计   下标越界,如何更改endtime出错,因为有的月份没的31号。如何优化
for   i=1   to   month(date)
years=year(date)
starttime=years& "- "&i& "-1 "
endtime=years& "- "&i& "-31 "
date_string= "   between     ' "&starttime& " '   and   ' "&endtime& " ' "
set   rec=server.CreateObject   ( "adodb.recordset ")
      sql= "select   count(*)   as   first   from   ys_first   where   first_date "
      sql=sql&date_string
          Rec.Open   sql,session( "connection_string "),3,3
            first=Rec.Fields( "first ")
          Rec.Close  
set   rec=nothing
total(i,0)=first
next

[解决办法]
for i=1 to month(date)
years=year(date)
starttime=years& "- "&i& "-1 "
if i=12 then
endtime=(years+1) & "- "&1& "-1 "
else
endtime=years & "- "&(i+1)& "-1 "
end if

date_string= " first_date> = ' "&starttime& " ' and first_date < ' "&endtime& " ' "

set rec=server.CreateObject ( "adodb.recordset ")
sql= "select count(*) as first from ys_first where "
sql=sql&date_string
Rec.Open sql,session( "connection_string "),3,3
first=Rec.Fields( "first ")
Rec.Close
set rec=nothing
total(i,0)=first
next

热点排行