大家好,数据库里面有20多个表,每个表里面只有1列都是数字,数字要被写入的,我想查看这所有表里面的最小值是什么,请问这个语言怎么写呢?用视图 还是用什么呢~~ 谢谢各位高手咯~~

解决方案 »

  1.   

    select min(数字列)
    from
    (select 数字列 from 表1
     union all
     select 数字列 from 表2
     union all
     select 数字列 from 表3
     union all
     select 数字列 from 表4
      )t
      

  2.   

    create table TempTable(t int)
    exec sp_msforeachtable 'insert TempTable(t) select min(ColName) from ?',@whereand=' and name <>''TempTable'''
    select min(t) from TempTable
    drop table TempTable
      

  3.   

    --try
    select min(数字列)
    from
    (select min(数字列)数字列 from 表1
     union all
     select min(数字列) from 表2
     union all
     select min(数字列) from 表3
     union all
     select min(数字列) from 表4
      )t
      

  4.   


    用了 显示  消息 209,级别 16,状态 1,第 1 行
    列名 'name' 不明确。
      

  5.   

    把  name 改成o.name 就好了, 谢谢chuifengde
     方法很牛~~ 也谢谢其他朋友哈~~
      

  6.   

    --使用游标与系统表吧
    declare @i int
    declare @j int
    declare @sql Nvarchar(4000)
    declare @name Nvarchar(50)
    set @j=-1
    declare cur cursor for
    select name from sys.objects where type='U'open cur 
    fetch next cur into @namewhile @@fetch_status=0
    begin
     set @sql=N'select @i=min(id) from ['+@name+']'--id为数字列
    exec sp_executesql @sql,N'@i int output',@i output
     if(@i<@j)
      @j=@i fetch next cur into @name
    end
    close cur
    deallocate cur
    select @j