这个使用作业来做,把语句放到下面的存储过程中.定时作业的制定企业管理器 
--管理 
--SQL Server代理 
--右键作业 
--新建作业 
--"常规"项中输入作业名称 
--"步骤"项 
--新建 
--"步骤名"中输入步骤名 
--"类型"中选择"Transact-SQL 脚本(TSQL)" 
--"数据库"选择执行命令的数据库 
--"命令"中输入要执行的语句: 
                       EXEC 存储过程名 ... --该存储过程用于创建表 --确定 
--"调度"项 
--新建调度 
--"名称"中输入调度名称 
--"调度类型"中选择你的作业执行安排 
--如果选择"反复出现" 
--点"更改"来设置你的时间安排  
然后将SQL Agent服务启动,并设置为自动启动,否则你的作业不会被执行 设置方法: 
我的电脑--控制面板--管理工具--服务--右键 SQLSERVERAGENT--属性--启动类型--选择"自动启动"--确定. 

解决方案 »

  1.   

    CREATE  proc  cKqView 
      as  
      declare  @str  varchar(100), 
      @ym varchar(50),@pymd varchar(10),@py varchar(10),@pm varchar(10),@pd varchar(10),@cymd varchar(10), 
      @cy varchar(10),@cm varchar(10),@cd varchar(10),@date datetime,@date1 datetime   select @ym= 'V'+left(convert(varchar(50),getdate(),112),6) 
      --select @ym='V00902' 
    if datepart(month,getdate())=1 
    begin 
      select @py=str(datepart(year,getdate())-1) 
      select @pm='12' 
      select @pd='21' 
      select @pymd=@py+'-'+@pm+'-'+@pd 
      select @cy=str(datepart(year,getdate())) 
      select @cm='01' 
      select @cd='20' 
      select @cymd=@cy+'-'+@cm+'-'+@cd 
    --下面这两个不要转化,还保持varchar类型  
    --   select @date=convert(datetime,@pymd,121) 
    --   select @date1=convert(datetime,@cymd,121) 
    --下面这句要这么写
      select @str = 'create view '+@ym+' as select * from kq where rq>='''+@date+''' and rq <='''+@date1 +''''
      --select @str = 'create view '+@ym+' as select * from kq where rq>='+@date+' and rq <='+@date1 
    end 
    else 
    begin 
    select @py=str(datepart(year,getdate())) 
      select @pm=str(datepart(month,getdate())-1) 
      select @pd='21' 
      select @pymd=@py+'-'+@pm+'-'+@pd 
      select @cy=str(datepart(year,getdate())) 
      --if datepart(month,getdate()) <10  select @cm='0'+str(datepart(month,getdate())) 
      select @cm=str(datepart(month,getdate())) 
      select @cd='20' 
      select @cymd=@cy+'-'+@cm+'-'+@cd   select @date=convert(datetime,@pymd,121) 
      select @date1=convert(datetime,@cymd,121) 
      --下面这句要这么写
    select @str = 'create view '+@ym+' as select * from kq where rq>='''+@date+''' and rq <='''+@date1+'''' 
      --select @str = 'create view '+@ym+' as select * from kq where rq>='+@date+' and rq <='+@date1 
    end 
      --可以先打印出来看看
      print @str
      exec(@str) 
    GO 
      

  2.   

    3楼的兄弟姐妹创建出来的视图代码为
    create view V200903 as select * from kq where rq>='      2009' and rq <='      2009'
    而想要的是
    create view V200903 as select * from kq where rq>='2009-02-21' and rq <='2009-03-20'