如何在没有在PC上安装MSAccess的情况下使用Delphi Application在MS Access数据库中创建VIEW?

时间:2012-08-31 07:49:18

标签: database delphi ms-access view

我想在MS Access上创建VIEW定义。我使用了以下CREATE VIEW声明:

SELECT 
   MFP.FollowUpPlan_Id, 
   MFP.FollowUpPlan_Name AS PlanName, 
   DFP.Sequence_No AS SequenceNo, 
   MFS.FollowUpSchedule_Name AS ScheduleName    
FROM 
   MAS_FollowUp_Plan AS MFP, 
   DET_FollowUp_Plan AS DFP, 
   MAS_FollowUp_Schedule AS MFS
WHERE 
   (((MFP.FollowUpPlan_Id)=DFP.FollowUpPlan_Id) AND
   ((DFP.FollowUpSchedule_Id)=MFS.FollowUpSchedule_Id)) AND
   MFP.is_Deleted = FALSE AND
   DFP.is_Deleted = false     
ORDER BY 
   MFP.FollowUpPlan_Id, DFP.Sequence_No;       

但它会引发错误:

Only Simple Select Queries are allowed in view.

请提供帮助,提前致谢。

3 个答案:

答案 0 :(得分:6)

这里的问题,正如Jeroen所解释的,是Access'CREATE VIEW声明的限制。对于这种情况,您可以使用CREATE PROCEDURE代替。它将创建db的QueryDefs集合的新成员---因此,从Access用户界面将显示为新的命名查询。

以下声明适用于我使用VBScript中的ADO。从以前的Delphi问题来看,我的理解是Delphi也可以使用ADO,所以我相信这也适合你。

CREATE PROCEDURE ViewSubstitute AS
SELECT
    MFP.FollowUpPlan_Id,
    MFP.FollowUpPlan_Name AS PlanName,
    DFP.Sequence_No AS SequenceNo,
    MFS.FollowUpSchedule_Name AS ScheduleName
FROM
    (MAS_FollowUp_Plan AS MFP
    INNER JOIN DET_FollowUp_Plan AS DFP
    ON MFP.FollowUpPlan_Id = DFP.FollowUpPlan_Id)
    INNER JOIN MAS_FollowUp_Schedule AS MFS
    ON DFP.FollowUpSchedule_Id = MFS.FollowUpSchedule_Id
WHERE
    MFP.is_Deleted=False AND DFP.is_Deleted=False
ORDER BY
    MFP.FollowUpPlan_Id,
    DFP.Sequence_No;

答案 1 :(得分:4)

You cannot mix ORDER BY with JOIN when creating views in Access。它会给你错误"Only simple SELECT queries are allowed in VIEWS."(注意复数VIEWS)

FROM中拥有多个表格是JOIN的一种表格。

  • 删除ORDER BY
  • FROM中没有JOIN只有一个表。

我记得从过去(当我做了比现在更多的访问时)看到这个用于大型查询的单个表选择也带有ORDER BY

The consensus is that you should not have ORDER BY in views anyway,这是你最好的事情。

Another reason that you can get the same error message是您添加参数或子选择。 Access也不喜欢视图中的那些,但在您看来并非如此。

答案 2 :(得分:1)

在Delphi中声明变量olevarCatalog,cmd为OleVariant,使用ComObj

olevarCatalog := CreateOleObject('ADOX.Catalog');
olevarCatalog.create(YourConnectionString); //This Will create MDB file.
// Using ADO Query(CREATE TABLE TABLEName....) add the required Tables.

// To Insert View Definition on MDB file.
cmd := CreateOleObject('ADODB.Command');
cmd.CommandType := cmdText;
cmd.CommandText := 'ANY Kind of SELECT Query(JOIN, OrderBy is also allowed)';
olevarCatalog.Views.Append('Name of View',cmd);
cmd := null;

这是使用Delphi创建MS ACCESS文件(.MDB)和VIEW的最佳方法。