Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
476 views
in Technique[技术] by (71.8m points)

sql - ERROR in CREATE VIEW

I tried to create a new view in my MS Access database so I can select better from it but I wonder what's happening here.

CREATE VIEW new 
AS 
  SELECT msthread.id, 
         msthread.threadname, 
         Count(msthread.threadname) AS TotalPost, 
         threadcategory 
  FROM   msthread 
         LEFT OUTER JOIN msposts 
                      ON msthread.threadname = msposts.threadname 
  GROUP  BY msthread.id, 
            msthread.threadname, 
            msthread.threadcategory 

Access gives me this error message when I try to execute that statement.

Syntax error in create table statement

Is there specific problems in creating view with JOINs? I'm trying to access 2 tables.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

CREATE VIEW was introduced with Jet 4 in Access 2000. But you must execute the statement from ADO/OleDb. If executed from DAO, it triggers error 3290, "Syntax error in CREATE TABLE statement", which is more confusing than helpful.

Also CREATE VIEW can only create simple SELECT queries. Use CREATE PROCEDURE for any which CREATE VIEW can't handle.

But CREATE VIEW should handle yours. I used a string variable to hold the DDL statement below, and then executed it from CurrentProject.Connection in an Access session:

CurrentProject.Connection.Execute strSql

That worked because CurrentProject.Connection is an ADO object. If you will be doing this from outside Access, use an OleDb connection.

Notice I made a few changes to your query. Most were minor. But I think the query name change may be important. New is a reserved word so I chose qryNew instead. Reserved words as object names seem especially troublesome in queries run from ADO/OleDb.

CREATE VIEW qryNew
AS
SELECT
    mst.id,
    mst.threadname,
    mst.threadcategory,
    Count(mst.threadname) AS TotalPost
FROM
    msthread AS mst
    LEFT JOIN msposts AS msp
    ON mst.threadname = msp.threadname
GROUP BY
    mst.id,
    mst.threadname,
    mst.threadcategory;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...