【问题标题】:FoxPro: Append arbitrary number of views into a single view or tableFoxPro:将任意数量的视图附加到单个视图或表中
【发布时间】:2018-06-19 22:18:33
【问题描述】:

我有几个视图需要组合成一个视图或表格。这些视图都具有相同的结构,标题为view_1、...、view_x

我在以下方面获得了最好的运气。我可以得到数据库中的视图总数,所以为了简单起见,我这里只使用了12。该代码创建了一个名为test 的新视图,并按预期使用view_1 的内容填充它。执行时不会抛出错误。在状态栏中,我看到它已经处理了 XXXX 记录。这对应于最后一个视图 (view_12)。但是test只包含view_1的内容。

create sql view test as;
select * ;
from view_1 

for i = 2 to 12
    insert into test ;
    select * ;
    from view_&i. 
endfor

我错过了一些简单的事情还是有更好的方法来做到这一点?

【问题讨论】:

    标签: sql visual-foxpro foxpro


    【解决方案1】:

    不幸的是,您不能真正将视图视为表 - 它们只是存储的 SQL 语句。因此,要使用其他视图的结果创建新视图,您基本上需要基于其他视图的 SQL 语句构建新的 SQL 语句。

    请注意,这假设每个较小视图的架构都是相同的,正如您所说:

    Close All
    Clear All
    Clear
    
    Open Database mydatabase Excl
    
    lnViews = Adbobjects(laViews, "VIEW")
    
    If Ascan(laViews, "vcombined") > 0
        Delete View vcombined
    Endif
    
    lcSQL = ""
    
    * -- You'll probably find out the view count and put it here.
    For lnView = 1 to 3
    
        lcView = "view_" + Transform(lnView)
        lcSQL = lcSQL + Iif(lnView > 1, "union ", "") + DBGetProp(lcView, "View", "SQL")
    
    Endfor  
    
    Create SQL View vcombined as &lcSQL
    

    【讨论】:

    • 这绝对有效,但是,“union”应该是“union”。单词前后必须有一个空格。否则会产生关键字错误。它还给了我一个“工会太多”的错误。在 VFP8 中,这是 Error 1834: The maximum number of UNIONs (10) has been exceeded.
    • 这种情况下需要Visual FoxPro 9,没有实际限制。
    • 顺便说一句,我知道 VFP 建议使用匈牙利符号。该文档有一个专门关于命名约定的页面。但是,它只给出“字符”的“c”,“数字”的“n”等。我注意到你使用“lc”,就像在“ell-see”中一样。是“长”的意思吗?还是别的什么?
    • L 表示本地声明。只是我工作的房子风格。
    【解决方案2】:

    VFP 视图只是一个 SQL 定义 + 一些保存在 DBC 中的属性。只需使用“union ALL”,您就可以将具有相同结构的多个视图组合成一个新视图。然而,将它作为一个单一的视图并不现实,因为让它可更新是没有意义的。

    使用这些视图来创建新视图实际上并没有多大意义。相反,您可以直接使用一个视图的 SQL 定义来创建最终视图。即:如果您的视图定义是:

    select CustomerId, CustomerName from Customers1
    select CustomerId, CustomerName from Customers2
    *...
    select CustomerId, CustomerName from CustomersN
    

    那么你可以简单地将你的新定义定义为:

    create sql view vMultiple as ; 
    select CustomerId, CustomerName from Customers1 ;
    union all ;
    select CustomerId, CustomerName from Customers2 ;
    union all ;
    * ... ;
    select CustomerId, CustomerName from CustomersN
    

    要为此添加一些间接可更新性,您可能需要为源添加一列,例如:

    create sql view vMultiple as ;
    select 1 as Source, CustomerId, CustomerName from Customers1 ;
    union all ;
    select 2 as Source, CustomerId, CustomerName from Customers2 ;
    union all ;
    * ...   ;
    select N as Source, CustomerId, CustomerName from CustomersN
    

    由于创建这样的视图没有多大意义,您可能希望在需要时将其创建为光标:

    select 1 as Source, CustomerId, CustomerName from Customers1 ;
    union all ;
    select 2 as Source, CustomerId, CustomerName from Customers2 ;
    union all ;
    * ...   ;
    select N as Source, CustomerId, CustomerName from CustomersN ;
    into cursor crsMultiple ;
    nofilter   && or readwrite if it needs any updating
    

    请记住,游标和视图(也是游标)都没有持久索引,并且视图游标是缓冲的。您可以根据需要创建索引。

    三思而后行,为什么需要对此(或任何视图)的视图。

    【讨论】:

      猜你喜欢
      • 2015-02-17
      • 1970-01-01
      • 1970-01-01
      • 2014-04-09
      • 1970-01-01
      • 2017-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多