阳光网驿-企业信息化交流平台【DTC零售连锁全渠道解决方案】

 找回密码
 注册

QQ登录

只需一步,快速开始

扫描二维码登录本站

手机号码,快捷登录

手机号码,快捷登录

老司机
查看: 1358|回复: 4

[原创] 如何查询变量表

[复制链接]
  • TA的每日心情
    开心
    2021-1-14 22:47
  • 签到天数: 296 天

    [LV.8]以坛为家I

    发表于 2011-5-11 14:44:52 | 显示全部楼层 |阅读模式
    我的数据库中的表是按月份自动生成的,例data201102, data201103
    如何在查询中嵌套啊

    SELECT *, name AS Expr1
    FROM sysobjects
    WHERE (name LIKE N'data2011%')
    楼主热帖
    启用邀请码注册,提高发帖质量,建设交流社区
  • TA的每日心情
    开心
    2021-1-14 22:47
  • 签到天数: 296 天

    [LV.8]以坛为家I

     楼主| 发表于 2011-5-11 14:46:51 | 显示全部楼层
    因为要跨月联合查询两个表,而表是动态生成的,这个怎解决啊
    启用邀请码注册,提高发帖质量,建设交流社区
  • TA的每日心情
    开心
    2023-8-25 08:56
  • 签到天数: 635 天

    [LV.9]以坛为家II

    发表于 2011-5-12 11:32:06 | 显示全部楼层
    SQL查询语句拼接下可以做到
    启用邀请码注册,提高发帖质量,建设交流社区
  • TA的每日心情
    开心
    2023-8-25 08:56
  • 签到天数: 635 天

    [LV.9]以坛为家II

    发表于 2011-5-12 12:28:52 | 显示全部楼层
    公司业务库上用的过程和用法,希望对你有所帮助,附件主用到的取表名过程。  

    declare @BeginDate datetime;
      declare @EndDate datetime;
      declare @SQLString char(8000);
      declare @tables char(1024);
      declare @subTableName char(32);
      declare @i int;
      select  @BeginDate='20100101',@EndDate='20100501'
      exec gettablesname @BeginDate,@EndDate,data,@tables output;
      select @tables as tablesname into #tablename  
      while 1=1 begin      
      select @i=charindex(',',tablesname) from #tablename;
       print (@i)
       select * From #425103tablename
           if @i=0 begin         select @subTableName=tablesname from #425103tablename;
                select @SQLString='insert into #201102data
                   select * from '
                 + ltrim(rtrim(@subTableName))
                 + '  a where  convert(varchar(10),Sdate,120)>='''
               + convert(varchar(10),@begindate,120)
               + ''' and convert(varchar(10),Sdate,120)<='''
               + convert(varchar(10),@enddate,120)
               + '''
                ';
             print(@SQLString);
            -- execute (@SQLString);
             break;
          end      
          else begin         
              select @subTableName=substring(tablesname,1,charindex(',',tablesname)-1) from #425103tablename;
                select @SQLString='insert into #201102data
                   select * from '
                 + ltrim(rtrim(@subTableName))
                 + ' a where convert(varchar(10),Sdate,120)>='''
               + convert(varchar(10),@begindate,120)
               + ''' and convert(varchar(10),Sdate,120)<='''
               + convert(varchar(10),@enddate,120)
               + '''
              ';
             print(@SQLString);
           -- execute (@SQLString);
            update #425103tablename      set tablesname=substring(tablesname,charindex(',',tablesname)+1,len(tablesname))   where 1=1;
          end;
        end;

    GetTablesName.rar

    671 Bytes, 下载次数: 0

    启用邀请码注册,提高发帖质量,建设交流社区

    该用户从未签到

    发表于 2011-5-14 10:31:46 | 显示全部楼层
    查询语句的拼接建议在上位程序中完成,用脚本的话就如ls比较复杂了,而且也不易维护。
    启用邀请码注册,提高发帖质量,建设交流社区
    您需要登录后才可以回帖 登录 | 注册

    本版积分规则

    快速回复 返回顶部 返回列表