declare @mouth int
--declare @start datetime
--declare @end datetime
declare @start nvarchar(20)
declare @end nvarchar(20)
declare @str nvarchar(3000)
declare @sql nvarchar(3000)
set @mouth=1
while @mouth<=12
begin
if @mouth=12
begin
set @start='2011-'+STR(@mouth)+'-1'
--set @start=cast(@str as datetime)
set @end='2012-'+STR(@mouth)+'-1'
--set @end=cast(@str as datetime)
end
else
begin
set @start='2011-'+ltrim(rtrim(STR(@mouth)))+'-1'
--set @start=cast(@str as datetime)
set @end='2011-'+ltrim(rtrim(STR(@mouth+1)))+'-1'
--set @end=cast(@str as datetime)
end
set @sql=N'select '+ltrim(rtrim(cast(@mouth as CHAR)))+' as ''月份'',count(1) as
'
'单数'',SUM(amount) as ''金额'' FROM [szjdw].[dbo].[mf_order] where cop_id=''DGJDW'' and createdt>
''
'+@start+''' and createdt<
'''+@end+''''
select @sql
--exec(@sql)
set @mouth=@mouth+1
end
go