简体   繁体   中英

using an existing MS Access query in SQL CE

I am new to SQL CE so I may be making a basic error. To recreate the sql I want I have created duplicate tables in MS Access and created the sql there. It produces what I want in Access and gives the error:

There was an error parsing the query. [ Token line number = 1,Token line offset = 11,Token in error = Sum ]" in the sql analyser.

The sql is::

TRANSFORM Sum(ToBank.Bank) AS SumOfBank
SELECT ToBank.Agent, Sum(ToBank.Bank) AS [Total Of Bank]
FROM ToBank
GROUP BY ToBank.Agent
PIVOT Format([DateSold],"yyyy");

Any help gratefully received. I am trying to produce a calculated field in a query to view it on a website, an alternative method of producing the calculation would be just as welcome

Microsoft Access uses Access SQL and SQL Server Compact Edition (SQL CE) uses Transact-SQL (T-SQL). They are two quite different dialects of the SQL language. As you have discovered, you will have rather limited success in trying to build SQL queries in Access and then use them directly in SQL Server.

One area in which the two dialects differ significantly is the PIVOT query (what Access calls a "crosstab query"). T-SQL does not use the TRANSFORM keyword, so it is not at all surprising that SQL CE would choke on an Access SQL crosstab query.

To get an idea of how you might convert an Access crosstab query into a T-SQL PIVOT query, take a look at the previous questions and answers in the search here .

However, bear in mind the following:

Crosstab queries in Access can automatically handle arbitrary values in the PIVOT columns, while T-SQL requires that the PIVOT values be specified explicitly in the SQL statement. Handling PIVOT values that can change from one invocation to the next in T-SQL usually involves Dynamic SQL - T-SQL code that builds a SQL statement "on the fly" - and that is usually accomplished by putting the T-SQL code in a Stored Procedure. Unfortunately, SQL CE does not support Stored Procedures.

So, you may find yourself gravitating toward SQL Server Express Edition. At the very least, any (T-)SQL queries you built in SQL Express would have a much better chance of running unmodified in SQL CE. And if you decide to use it instead of SQL CE you would also have the ability to use Stored Procedures (and hence Dynamic SQL). Or, if you wanted to stick with SQL CE then your application would have to determine the PIVOT values at run-time, build the T-SQL query, and then submit that to SQL CE.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM