简体   繁体   English

在SQL CE中使用现有的MS Access查询

[英]using an existing MS Access query in SQL CE

I am new to SQL CE so I may be making a basic error. 我是SQL CE的新手,所以我可能犯了一个基本错误。 To recreate the sql I want I have created duplicate tables in MS Access and created the sql there. 要重新创建sql,我想在MS Access中创建重复的表并在其中创建sql。 It produces what I want in Access and gives the error: 它产生我想要的Access中的并给出错误:

There was an error parsing the query. 解析查询时出错。 [ Token line number = 1,Token line offset = 11,Token in error = Sum ]" in the sql analyser. [令牌行号= 1,令牌行偏移量= 11,令牌错误=总和]“,在sql分析器中。

The sql is:: 的SQL是::

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). Microsoft Access使用Access SQL,而SQL Server Compact Edition(SQL CE)使用Transact-SQL(T-SQL)。 They are two quite different dialects of the SQL language. 它们是SQL语言的两种完全不同的方言。 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. 如您所发现,尝试在Access中构建SQL查询然后直接在SQL Server中使用它们的成功将非常有限。

One area in which the two dialects differ significantly is the PIVOT query (what Access calls a "crosstab query"). 两种方言有很大不同的一个领域是PIVOT查询(Access称之为“交叉表查询”)。 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. T-SQL不使用TRANSFORM关键字,因此SQL CE会阻塞Access SQL交叉表查询也就不足为奇了。

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 . 要了解如何将Access交叉表查询转换为T-SQL PIVOT查询,请在此处查看搜索中的先前问题和解答。

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. Access中的交叉表查询可以自动处理PIVOT列中的任意值,而T-SQL要求在SQL语句中显式指定PIVOT值。 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. 处理可能在T-SQL中从一个调用更改为另一个调用的PIVOT值通常涉及动态SQL-动态生成SQL语句的T-SQL代码-通常是通过将T-SQL代码放入存储过程。 Unfortunately, SQL CE does not support Stored Procedures. 不幸的是,SQL CE不支持存储过程。

So, you may find yourself gravitating toward SQL Server Express Edition. 因此,您可能会发现自己偏向于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. 至少,您在SQL Express中构建的任何(T-)SQL查询都将有更好的机会在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). 而且,如果您决定使用它而不是 SQL CE,则也可以使用存储过程(因此使用动态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. 或者,如果您想坚持使用SQL CE,则您的应用程序将必须在运行时确定PIVOT值,构建T-SQL查询,然后将其提交给SQL CE。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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