我现在的情况是这样:
有一个数据库叫syslogd,
库里面有很多表,名字诸如:log_http_20100513,log_http_20100514,log_http_20100515,log_http_20100516,log_http_20100517........我现在的需求是:我的查询界面有两个时间控件,选择开始时间和结束时间后可以从这两个时间段内的表里面把所需要的数据全部检索出来

解决方案 »

  1.   

    declare @d1 datetime,@d2 datetime
    set @d1='2010-05-01'
    set @d2='2010-06-01'declare @d int
    declare @sql varchar(8000)
    set @d=0
    set @sql =''
    while dateadd(d,@d,@d1)<=@d2
    begin
    if object_id('log_http_'+convert(varchar(8),@d1,114)) is not null
    begin
    set @sql=@sql+'select * from log_http_'+convert(varchar(8),@d1,114)+' union all'
    end
    set @d=@d+1
    end
    if @sql<>''
    exec(@sql)
      

  2.   

    declare @d1 datetime,@d2 datetime
    set @d1='2010-05-01'
    set @d2='2010-05-03'declare @tbname sysname
    declare @sql varchar(8000)
    while datediff(d,@d1,@d2)>=0
    begin    
        begin
    set @tbname='log_http_'+convert(varchar(8),@d1,112)
    --if object_id(@tbname) is not null
            set @sql=isnull(@sql+' union all ','')+'select * from log_http_'+convert(varchar(8),@d1,112)
        end
        set @d1=@d1+1
    end
    exec(@sql)
      

  3.   

    declare @d1 datetime,@d2 datetime
    set @d1='2010-05-01'
    set @d2='2010-05-03'declare @tbname sysname
    declare @sql varchar(8000)
    while datediff(d,@d1,@d2)>=0
    begin    
        begin
    set @tbname='log_http_'+convert(varchar(8),@d1,112)
    if object_id(@tbname) is not null
            set @sql=isnull(@sql+' union all ','')+'select * from log_http_'+convert(varchar(8),@d1,112)
        end
        set @d1=@d1+1
    end
    print @sql
    --select * from log_http_20100501 union all select * from log_http_20100502 union all select * from log_http_20100503
    exec(@sql)
      

  4.   

            private void buttonQuery_Click(object sender, EventArgs e)
            {
                //string d1 = dateTimePicker1.Text;
                //string d2 = dateTimePicker2.Text;
                string dbtablename = "log_" + comboBoxTableName.Text + "_" + string.Format("{0:yyyyMMdd}", Convert.ToDateTime(dateTimePicker1.Text));            string sql = "select * from " + dbtablename;               if(this.comboBoxTableName.Text.Contains("http"))//查询log_http_2010xxxx
                  {
                      sql = "select MsgDate as 日期,MsgTime as 时间,MsgText_B as 源IP,MsgText_D as 目的网址 from " + dbtablename;
                      if (this.checkBoxText.Checked)
                          sql = sql + " where MsgText_D like '%" + this.textBoxMsgText.Text + "%' or MsgText_B like '%" + this.textBoxMsgText.Text + "%'";
                  }
               else //查询syslogd
                  {
                      sql = "select MsgDate as 日期,MsgTime as 时间,MsgText as 详细内容 from syslogd";
                      if (this.checkBoxText.Checked)
                          sql = sql + " where MsgText like '%" + this.textBoxMsgText.Text + "%' and";
                  }
      

  5.   

    四楼我给的代码是我现在按天一天一天一个表一个表的查询操作代码,
    我现在需要查询一段时间数据。
    上面htl258给的代码,在查询分析器里面执行没有问题,很好用,
    可是我需要把这段代码用在c#的查询界面里,不会做到融会贯通,我现在的需求是在c#软件界面点击查询按钮执行这个sql查询操作,自己不会融会贯通,请高手赐教。