search
HomeDatabaseMysql TutorialSQL语句在Access和SQL Server里面的不同

做了一个Winform的营养测量软件,来回的捣腾着Access数据库,还是那几句增删改查,不过用多了,发现Access数据库下的SQL语句和SQL Server下正宗的SQL还有有很大的不同。 我的感觉是,Accees数据库虽然可以称得上是小型的关系型数据库,并且也是使用的结构化

做了一个Winform的营养测量软件,来回的捣腾着Access数据库,还是那几句增删改查,不过用多了,发现Access数据库下的SQL语句和SQL Server下正宗的SQL还有有很大的不同。

 

我的感觉是,Accees数据库虽然可以称得上是小型的关系型数据库,并且也是使用的结构化查询语言SQL,但它的语法(主要体现在函数上),却类似vbscript的语法,我想,这应该和Access属于Office系列有关,基于它的开发和应用,自然就与VBA扯上关系,因而Access的函数库也就是VBA的函数库,而非SQL函数库。下面,我们来具体看下Access和SQL Server在查询语句的编写上具体的不同。

 

一、数据类型转换:

  1. Access: SELECT '调查'+CStr(Did) as diaocha FROM CZdengji
  2. SQL Server: select artid,'调查'+Cast(listid As varchar) as did from kingart where artdate>=getdate()

      Access中SQL查询的数据类型转换有很多函数,每一个函数都可以强制将一个表达式转换成某种特定数据类型。具体如下:CBool(expression) 、CByte(expression) 、CCur(expression) 、CDate(expression) 、CDbl(expression) 、CDec(expression) 、CInt(expression) 、CLng(expression) 、CSng(expression) 、CStr(expression) 、CVar(expression) 、CStr(expression),相信不用说明,大家也看得懂这各种转换符的意思。

 

二、IIF函数(ACCESS专用)

  1. Access: SELECT Id,IIF(Isfinished=1,'已完成','进行中'),Name,Diaocharen FROM CZdiaocha ORDER BY Isfinished ASC
  2. SQL Server 的写法就相对更多,可以自定义函数,也可以使用case语句,如select Id,case Isfinished when 1 then '已完成'
    when 0 then '进行中'  end,Name,Diaocharen FROM CZdiaocha ORDER BY Isfinished ASC

三、DISTINCT关键字用法: Access里面distinct(expression)的用法单一, expression 只能为指定字段,而不能为表达式,SQL Server则不同, expression 除了可以为单个字段,也可以为一个子查询等复杂表达式。

 

四、日期格式化

  1. Access: 使用format()函数,SELECT TOP 5 arttitle,format(artdate,'YY-MM-DD') as arttime FROM kingart WHERE listid=9,这里使用format()函数,指定artdate(日期型)查询出来的格式为YY-MM-DD。
  2. SQL Server: 使用convert()函数,select Id,lanmu,title,content, convert(varchar(10),updatetime ,120) as update_time from news,这里的120,是指定格式为YY-MM-DD,如果是其他数字(101-120),就是其他的格式。

 

其它一些常用的SQL语句集锦

  1. SELECT Top 5 * From Food WHERE Fid not in(Select top 5 Fid FROM Food) //查询第5到第10条记录
  2. delete from Food where charindex('2007',uptime)>0 or charindex('2008', uptime )>0  //删除Food表中uptime列包含有2007或2008的记录,即删除时间为2007年和2008年的更新记录
  3. select Id,lanmu,title,content, convert(varchar(10),updatetime ,120) as update_time from news  // SQL Server里面的convert函数,转换成 varchar(10)类型, 120为指定转换后的格式。
  4. select Mealtype as 餐别,Fname as 食物名称,sum(rjjingshisz)/2 as 人均净食生重总和 from CZdengji WHERE Did=6 group by Mealtype,Fname    //分组查询,GROUP BY的本意就是重复的字段合为一,对应的信息,进行汇总(count、sum等运算)
  5. Select Count(ID) from Flow where convert(varchar(21),visittime,120) like '2007-01-26% ' ,日期型数据模糊查询。
  6. select * from ('MSDASQL','DRIVER={SQL Server};SERVER=服务器名字;UID=对端数据库的账号;PWD=对端密码', 对端数据库.权限者.对端的表) as 表   //跨服务器查询
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
When should you use a composite index versus multiple single-column indexes?When should you use a composite index versus multiple single-column indexes?Apr 11, 2025 am 12:06 AM

In database optimization, indexing strategies should be selected according to query requirements: 1. When the query involves multiple columns and the order of conditions is fixed, use composite indexes; 2. When the query involves multiple columns but the order of conditions is not fixed, use multiple single-column indexes. Composite indexes are suitable for optimizing multi-column queries, while single-column indexes are suitable for single-column queries.

How to identify and optimize slow queries in MySQL? (slow query log, performance_schema)How to identify and optimize slow queries in MySQL? (slow query log, performance_schema)Apr 10, 2025 am 09:36 AM

To optimize MySQL slow query, slowquerylog and performance_schema need to be used: 1. Enable slowquerylog and set thresholds to record slow query; 2. Use performance_schema to analyze query execution details, find out performance bottlenecks and optimize.

MySQL and SQL: Essential Skills for DevelopersMySQL and SQL: Essential Skills for DevelopersApr 10, 2025 am 09:30 AM

MySQL and SQL are essential skills for developers. 1.MySQL is an open source relational database management system, and SQL is the standard language used to manage and operate databases. 2.MySQL supports multiple storage engines through efficient data storage and retrieval functions, and SQL completes complex data operations through simple statements. 3. Examples of usage include basic queries and advanced queries, such as filtering and sorting by condition. 4. Common errors include syntax errors and performance issues, which can be optimized by checking SQL statements and using EXPLAIN commands. 5. Performance optimization techniques include using indexes, avoiding full table scanning, optimizing JOIN operations and improving code readability.

Describe MySQL asynchronous master-slave replication process.Describe MySQL asynchronous master-slave replication process.Apr 10, 2025 am 09:30 AM

MySQL asynchronous master-slave replication enables data synchronization through binlog, improving read performance and high availability. 1) The master server record changes to binlog; 2) The slave server reads binlog through I/O threads; 3) The server SQL thread applies binlog to synchronize data.

MySQL: Simple Concepts for Easy LearningMySQL: Simple Concepts for Easy LearningApr 10, 2025 am 09:29 AM

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

MySQL: A User-Friendly Introduction to DatabasesMySQL: A User-Friendly Introduction to DatabasesApr 10, 2025 am 09:27 AM

The installation and basic operations of MySQL include: 1. Download and install MySQL, set the root user password; 2. Use SQL commands to create databases and tables, such as CREATEDATABASE and CREATETABLE; 3. Execute CRUD operations, use INSERT, SELECT, UPDATE, DELETE commands; 4. Create indexes and stored procedures to optimize performance and implement complex logic. With these steps, you can build and manage MySQL databases from scratch.

How does the InnoDB Buffer Pool work and why is it crucial for performance?How does the InnoDB Buffer Pool work and why is it crucial for performance?Apr 09, 2025 am 12:12 AM

InnoDBBufferPool improves the performance of MySQL databases by loading data and index pages into memory. 1) The data page is loaded into the BufferPool to reduce disk I/O. 2) Dirty pages are marked and refreshed to disk regularly. 3) LRU algorithm management data page elimination. 4) The read-out mechanism loads the possible data pages in advance.

MySQL: The Ease of Data Management for BeginnersMySQL: The Ease of Data Management for BeginnersApr 09, 2025 am 12:07 AM

MySQL is suitable for beginners because it is simple to install, powerful and easy to manage data. 1. Simple installation and configuration, suitable for a variety of operating systems. 2. Support basic operations such as creating databases and tables, inserting, querying, updating and deleting data. 3. Provide advanced functions such as JOIN operations and subqueries. 4. Performance can be improved through indexing, query optimization and table partitioning. 5. Support backup, recovery and security measures to ensure data security and consistency.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.