Home Database Mysql Tutorial SQL语句在Access和SQL Server里面的不同

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

Jun 07, 2016 pm 03:49 PM
access server sql different statement

做了一个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 of this Website
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

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use sql if statement How to use sql if statement Apr 09, 2025 pm 06:12 PM

SQL IF statements are used to conditionally execute SQL statements, with the syntax as: IF (condition) THEN {statement} ELSE {statement} END IF;. The condition can be any valid SQL expression, and if the condition is true, execute the THEN clause; if the condition is false, execute the ELSE clause. IF statements can be nested, allowing for more complex conditional checks.

MySQL and SQL: Essential Skills for Developers MySQL and SQL: Essential Skills for Developers Apr 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.

How to use AWS Glue crawler with Amazon Athena How to use AWS Glue crawler with Amazon Athena Apr 09, 2025 pm 03:09 PM

As a data professional, you need to process large amounts of data from various sources. This can pose challenges to data management and analysis. Fortunately, two AWS services can help: AWS Glue and Amazon Athena.

How to use Debian Apache logs to improve website performance How to use Debian Apache logs to improve website performance Apr 12, 2025 pm 11:36 PM

This article will explain how to improve website performance by analyzing Apache logs under the Debian system. 1. Log Analysis Basics Apache log records the detailed information of all HTTP requests, including IP address, timestamp, request URL, HTTP method and response code. In Debian systems, these logs are usually located in the /var/log/apache2/access.log and /var/log/apache2/error.log directories. Understanding the log structure is the first step in effective analysis. 2. Log analysis tool You can use a variety of tools to analyze Apache logs: Command line tools: grep, awk, sed and other command line tools.

Solution to MySQL encounters 'Access denied for user' problem Solution to MySQL encounters 'Access denied for user' problem Apr 11, 2025 pm 05:36 PM

How to solve the MySQL "Access denied for user" error: 1. Check the user's permission to connect to the database; 2. Reset the password; 3. Allow remote connections; 4. Refresh permissions; 5. Check the database server configuration (bind-address, skip-grant-tables); 6. Check the firewall rules; 7. Restart the MySQL service. Tip: Make changes after backing up the database.

How to configure Debian Apache log format How to configure Debian Apache log format Apr 12, 2025 pm 11:30 PM

This article describes how to customize Apache's log format on Debian systems. The following steps will guide you through the configuration process: Step 1: Access the Apache configuration file The main Apache configuration file of the Debian system is usually located in /etc/apache2/apache2.conf or /etc/apache2/httpd.conf. Open the configuration file with root permissions using the following command: sudonano/etc/apache2/apache2.conf or sudonano/etc/apache2/httpd.conf Step 2: Define custom log formats to find or

Summary of phpmyadmin vulnerabilities Summary of phpmyadmin vulnerabilities Apr 10, 2025 pm 10:24 PM

The key to PHPMyAdmin security defense strategy is: 1. Use the latest version of PHPMyAdmin and regularly update PHP and MySQL; 2. Strictly control access rights, use .htaccess or web server access control; 3. Enable strong password and two-factor authentication; 4. Back up the database regularly; 5. Carefully check the configuration files to avoid exposing sensitive information; 6. Use Web Application Firewall (WAF); 7. Carry out security audits. These measures can effectively reduce the security risks caused by PHPMyAdmin due to improper configuration, over-old version or environmental security risks, and ensure the security of the database.

What is apache server? What is apache server for? What is apache server? What is apache server for? Apr 13, 2025 am 11:57 AM

Apache server is a powerful web server software that acts as a bridge between browsers and website servers. 1. It handles HTTP requests and returns web page content based on requests; 2. Modular design allows extended functions, such as support for SSL encryption and dynamic web pages; 3. Configuration files (such as virtual host configurations) need to be carefully set to avoid security vulnerabilities, and optimize performance parameters, such as thread count and timeout time, in order to build high-performance and secure web applications.

See all articles