public static string StructSql(int startYear, int startMonth, int endYear, int endMonth)
{
StringBuilder tempSql = new StringBuilder();
DateTime dtStart = new DateTime(startYear, startMonth, 1);
DateTime dtEnd = new DateTime(endYear, endMonth, 1);
TimeSpan ts = dtEnd - dtStart;
int tempStartYear = startYear;
int tempStartMonth = startMonth;
if (ts.TotalDays > 25)
{
for (int i = startMonth; i <= 12; i++)
{
if (i == startMonth && tempStartYear == startYear)
tempSql.AppendFormat("select '{0}' as A,'{1}' as B from dual\n", tempStartYear.ToString(), tempStartMonth.ToString());
else
tempSql.AppendFormat(" union all select '{0}' as A,'{1}' as B from dual\n", tempStartYear.ToString(), tempStartMonth.ToString());
if (tempStartYear >= endYear && tempStartMonth >= endMonth)
break;
if (i == 12)
{
tempStartYear++;
i = 0;
tempStartMonth = 0;
}
tempStartMonth++;
}
}
else
{
// Page.RegisterStartupScript("message", "<script type='text/javascript'>alert('查询的截至时间小于一个月不能查询!')</script>");
return "";
//Response.End();
}
return tempSql.ToString();
}
本文介绍了一个用于生成SQL查询字符串的方法,该方法接受起始年月和结束年月作为参数,并根据日期范围构建相应的SQL查询。如果指定的时间跨度超过一个月,则会生成一系列的SQL查询语句;反之则返回空字符串。
840

被折叠的 条评论
为什么被折叠?



