February 3, 2007
Access (Jet SQL)中的function
MS Access是一种桌面型数据库引擎,基于Microsoft Jet database engine。一般用在很轻量级的应用上,以“方便”著称。其实某些方面也很强大,因为Jet SQL是有VBA集成在里面的。在写一些SQL作为query的时候,曾经因为发现Jet SQL内置的函数很少,而写了很多Dirty code。起码来说,没看到任何可用于非线性的计算函数(比如计算个人所得税时需要用到)。和T-SQL比较起来差的很远。直到最近发现可以使用VBA在SQL中,才发现这些问题不但可以解决,而且更容易了。
The Microsoft Jet database engine uses the Microsoft® Visual Basic® for Applications (or VBA) expression service to perform simple arithmetic and function evaluation. All of the operators used in Microsoft Jet SQL expressions (except Between, In, and Like) are defined by the VBA expression service. In addition, the VBA expression service offers over 100 VBA functions that you can use in SQL expressions.
SQL中的Function可以这样分为两种:Calculating fields和 SQL aggregate。自定义的聚集函数在SQL Server 2000里面也不支持,要到SQL Server 2005中才有。Calculating fields却可以利用VBA做出很多的扩展。
工具太容易上手,反而会使用户认识不到它全面的功能。微软的Office就是一个例子。
北京个人所得税计算的JET SQL Query:
SELECT
SalaryRMB,
IIF(SalaryRMB>1600,
(SalaryRMB-1600)*
IIF(SalaryRMB>=21600,0.25,
IIF(SalaryRMB>=6600,0.20,
IIF(SalaryRMB>=3600,0.15,
IIF(SalaryRMB>=2100,0.10,0.05) ) ))
-IIF(SalaryRMB>=21600,1375,
IIF(SalaryRMB>=6600,375,
IIF(SalaryRMB>=3600,125,
IIF(SalaryRMB>=2100,25, 0) ))),
0) AS SalaryTaxRMB
FROM tb_Salary
Filed by
charlie
at 8:52 pm under 
[...] Original post by charlie zhu and software by Elliott Back Share and Enjoy:These icons link to social bookmarking sites where readers can share and discover new web pages. [...]
[...] on Access. Access database uses JETSQL engine, it’s not as powerful as T-SQL, but it supports VBA functions. VBA code can easily visit exernal dll function, so JETSQL could be extended greatly.So, the [...]
[...] is so smart and smoothly for small applications those don’t have to be scaled. You can use almost all VBA programming utilities in it. The TRANSFORM/PIVOT feature is powerful and more then 5 years before the SQL Server’s [...]