有12张月表,366张天表
月表命名规则 log+month  如log1 log2 ... log12
天表命名规则 log_month_day  如log_1_1  log_2_29 ... log_8_31 ... log_10_1 ... log_12_31现在要给这些表建立13张视图,建立的规则为
CREATE VIEW dbo.v_log
AS
SELECT * FROM 表名
UNION ALL
SELECT * FROM 表名
UNION ALL
...要求建立13张视图,一张年视图v_log 要求关联12张月表  ,12张月视图 命名规则为v_log_month  如v_log_1 ... v_log_12
要求每个月视图关联该月的所有天视图。脑子彻底乱了,老大们帮帮忙拼个动态SQL解决吧,不要用体力活~~~

解决方案 »

  1.   

    建一个存储过程,取相应的数据,
    然后create view as
    select * from openrowset(....,'exec 这个存储过程 参数,...)
    即可。不同的的视图建时使用不同的参数。可以简化你的工作量,不过不推荐。还是老老实实写吧
      

  2.   

    先结了这贴再说:http://topic.csdn.net/u/20080728/16/962eb6b6-7a81-423e-8341-20e62a23669d.html
      

  3.   

     这个不是已经结了.. 有366张表,命名规则为log+月份+日期 
    比如log_1_1 log_2_29  log_10_31 log_12_31 
    现在要给所有的表加一列newcol  int, 并且给所有表以前的记录该列赋值为9,这个SQL应该怎么写 另外,每个表里大概有1000W的记录 这个效率也是要考虑的~
    set nocount on
    declare @dt datetime set @dt='2008-01-01'
    declare @tbname varchar(50),@sql varchar(8000)
    while @dt<=cast('2008-12-31' as datetime)
    begin
    set @sql='alter table log_'+rtrim(month(@dt))+'_'+rtrim(day(@dt))+' add newcol int'
    exec(@sql)
    set @sql='update log_'+rtrim(month(@dt))+'_'+rtrim(day(@dt))+' set newcol=9'
    exec(@sql)
    set @dt=dateadd(day,1,@dt)
    print('log_'+rtrim(month(@dt))+'_'+rtrim(day(@dt))+' 处理完毕')
    end
      

  4.   

    use Test
    go
    --生成测试表
    Declare 
    @sql varchar(8000),
    @Date datetime,
    @TableName nvarchar(128)
    Select @Date='20080101'
    While @Date<='20081231'
    Begin
    Set @TableName=Quotename('log_'+Rtrim(Month(@Date))+'_'+Rtrim(Day(@Date)))
    Set @sql=Isnull(@sql+Char(13)+Char(10),'')+'If Object_id('''+@TableName+''') Is Not Null Drop Table '+@TableName+Char(13)+Char(10)+' Create Table '+@TableName+'(id int identity(1,1),x nvarchar(20))'
    If Datediff(day,'20080101',@Date)%20=0
    Begin
    Exec(@sql)
    Set @sql=null
    End
    Set @Date=@Date+1
    End
    If @sql>'' Exec(@sql)
    Go--创建视图
    Declare @x1 varchar(8000)
    Declare @x2 varchar(8000)
    Declare @x3 varchar(8000)
    Declare @x4 varchar(8000)If Object_id('tempdb..#') is not null
    Drop Table #
    Select id=identity(int,1,1),name Into #
    From sys.sysobjects 
    Where xtype='U' And name like 'log[_]%[_]%'Select @x1=Isnull(@x1+Char(13)+Char(10)+'Union All ','Create view v_log1 As'+Char(13)+Char(10)) +'Select * From '+Quotename(name)
    From # Where id<=100
    Select @x2=Isnull(@x2+Char(13)+Char(10)+'Union All ','Create view v_log2 As'+Char(13)+Char(10)) +'Select * From '+Quotename(name)
    From # Where id Between 101 And 200
    Select @x3=Isnull(@x3+Char(13)+Char(10)+'Union All ','Create view v_log3 As'+Char(13)+Char(10)) +'Select * From '+Quotename(name)
    From # Where id Between 201 And 300
    Select @x4=Isnull(@x4+Char(13)+Char(10)+'Union All ','Create view v_log4 As'+Char(13)+Char(10)) +'Select * From '+Quotename(name)
    From # Where id >300Exec ('If Object_id(''v_log1'') Is Not Null Drop View v_log1 ')
    Exec (@x1)
    Exec ('If Object_id(''v_log2'') Is Not Null Drop View v_log2 ')
    Exec (@x2)
    Exec ('If Object_id(''v_log3'') Is Not Null Drop View v_log3 ')
    Exec (@x3)
    Exec ('If Object_id(''v_log4'') Is Not Null Drop View v_log4 ')
    Exec (@x4)If Object_id('v_log') Is Not null
    Drop View v_log
    Go
    Create View v_log
    As
    Select * From v_log1 Union All
    Select * From v_log2 Union All
    Select * From v_log3 Union All
    Select * From v_log4--之所以分开4个视图,是因为“单个查询支持最多有 256 个表源”
      

  5.   

    回复7楼:基本上用这个可以解决了,完成以后给分。先问个问题,可以帮我详解一下这句SQL么?Select @x1=Isnull(@x1+Char(13)+Char(10)+'Union All ','Create view v_log1 As'+Char(13)+Char(10)) +'Select * From '+Quotename(name) 
    From # Where id <=100 
      

  6.   

    谁给解释下这句SQL ,完了好结贴啊Select @x1=Isnull(@x1+Char(13)+Char(10)+'Union All ','Create view v_log1 As'+Char(13)+Char(10)) +'Select * From '+Quotename(name) 
    From # Where id <=100 
      

  7.   

    构造第一个视图语句Char(13)+Char(10) 回车换行
    Quotename(name) 返回带有分隔符"[]"的 Unicode 字符串
    其他就没有什么好解释的了,
    要看Create View语句可以使用,Print @x1来查。
      

  8.   

    他是如何把后面的select * from tb 用union all联合在一起的呢?