大虾们:  
    我现在想把七八张表里面的每张表的两个字段,组合到一个视图中,做查询.在设计上遇到问题向大家请教一下.(数据库用的Sysbase, )    1 有八张表,每张三个字段组合成一个视图不知道 sql server  或 Sysbase 数据库的视
图支持这么多字段不?
    2 我现在想知道,从视图中查出来的数据是从哪张表里来的,请问这在视图中怎么样实现,如果视图中不能实现,代码中能吗?     希望遇到过这种问题的高手指点一下 , 有代码最好! 解决问题立即给分!

解决方案 »

  1.   

    注意一点:如果集成到视图中的几个表字段同名,你需要在试图的定义时指定别名,这可同时为你提供了字段的表来源信息
    e.g.:create view vw_tbA2C
    select id = a.id
      ,a_field_1 = a.field_1
      ,a_field_2 = a.field_2
      ,b_field_1 = b.field_1
      ,c_field_2 = c.field_2
      ,c_field_3 = c.field_3 -- .. and so on
    from tbA a, tbB b, tbC c
    where a.id=b.id and a.id=c.id -- ..
      

  2.   

      谢谢 先试试!   请问一下:这些来源信息,在程序中是怎么样体现的?  是返回一个String  还是什么?   
      

  3.   

    其实视图在表现上和库表一样,在上例中的 id, a_field_1, a_field_2, b_field_1, c_field_2, c_field_3 就是字段名称。
      

  4.   

    当我用程序查出id,   a_field_1,   a_field_2,   b_field_1,   c_field_2,   c_field_3 以上任何一个, 能不能知道它是哪个表的
      

  5.   

    大虾们:    
        我现在想把七八张表里面的每张表的两个字段,组合到一个视图中,做查询.在设计上遇到问题 向大家请教一下.(数据库用的Sysbase,   )     1 有八张表,每张三个字段组合成一个视图不知道 sql   server     或 Sysbase   数据库的视 
    图支持这么多字段不? ---------------------------------
    create view my_view
    as
      select a.* , b.* , c.* from a,b,c where a.id = b.id = c.id
        2 我现在想知道,从视图中查出来的数据是从哪张表里来的,请问这在视图中怎么样实现,如 果视图中不能实现,代码中能吗? 
         希望遇到过这种问题的高手指点一下 , 有代码最好! 解决问题立即给分!这个直接查看视图的源码即可.
      

  6.   

    数据来自各个表,看你的字段使用的是哪个表的字段。(当然,如果在视图中使用了union另说。)
      

  7.   

    在视图中保存来源表的名称即可。如CREATE VIEW 视图 AS
        SELECT 来源表 = '表1',内容 = 表1.内容 FROM   表1
        UNION ALL
        SELECT 来源表 = '表2',内容 = 表2.内容 FROM   表2
      

  8.   

    --判断表最多可以有多少个字段
    if object_id('tempdb..#','U') is not null drop table #
    create table #(test1 int)
    set nocount on 
    declare @i int,@sql varchar(100)
    set @i = 1
    while @i >0
    begin
        begin try 
            set @i=@i+1
            set @sql='alter table # add test'+ltrim(str(@i))+' int'
            exec (@sql)
        end try
        begin catch
            select error_message()
        set @i=-@i
        end catch
    end
    select 最大列数 = -(@i+1) 
    /*
    CREATE TABLE 失败,因为表 '#' 中的列 'test1025' 超出了 1024 列的最大值。最大列数
    -----------
    1024
    */
      

  9.   

    --判断视图最多可以有多少个字段
    if object_id('视图','V') is not null drop view 视图
    GO
    create view 视图 as
        select 1 test1
    GOset nocount on 
    declare @i int,@sql varchar(max)
    set @i = 1
    while @i >0
    begin
        begin try 
            set @i=@i+1
            set @sql=replace(select object_definition(object_id('视图')),'create','alter')+',1 test'+ltrim(str(@i))
            exec (@sql)
        end try
        begin catch
            select error_message()
        set @i=-@i
        end catch
    end
    select 最大列数 = -(@i+1) 
    /*
    CREATE VIEW 失败,因为视图 '视图' 中的列 'test1025' 超过了列的最大数目 1024。最大列数
    -----------
    1024
    */
      

  10.   

    --判断视图最多可以有多少个字段
    if object_id('视图','V') is not null drop view 视图
    GO
    create view 视图 as
        select 1 test1
    GOset nocount on 
    declare @i int,@sql varchar(max)
    set @i = 1
    while @i >0
    begin
        begin try 
            set @i=@i+1
            set @sql=replace(object_definition(object_id('视图')),'create','alter')+',1 test'+ltrim(str(@i))
            exec (@sql)
        end try
        begin catch
            select error_message()
        set @i=-@i
        end catch
    end
    select 最大列数 = -(@i+1) 
    /*
    CREATE VIEW 失败,因为视图 '视图' 中的列 'test1025' 超过了列的最大数目 1024。最大列数
    -----------
    1024
    */