序號 |
簡述 |
Access語法 |
SqlServer語法 |
Oracle語法 |
解決方案 |
01 |
系統時間 |
Now(),Date() |
GETDATE() |
SYSDATE |
GetSysTimeStr |
02 |
連接字符串 |
& |
+ |
|| |
GetConcatStr |
03 |
截取字符串 |
Mid |
SubString |
SubString |
GetSubStr |
04 |
小寫字符串 |
LCase |
Lower |
Lower |
GetLowerStr |
05 |
大寫字符串 |
UCase |
Upper |
Upper |
GetUpperStr |
06 |
查找字符串 |
InStr |
InStr |
CharIndex |
GetFindStr |
07 |
替換空值 |
IIF+IsNull |
Coalesce |
Nvl |
GetNullStr |
08 |
條件取值 |
IIF, Switch |
Case+When+Else |
DeCode或Case |
GetCaseStr |
09 |
字段類型轉換 |
CStr,CInt,CDate |
Convert或cast |
To_Char,To_Number. |
GetConvertStr |
10 |
日期字符串 |
#2004-10-19# |
‘2004-10-9’ |
‘2004-10-9’ |
GetDateStr |
11 |
最大值加1 |
|
|
|
GetNextNumStr |
12 |
Like語句函數 |
Like ‘101* |
Like ‘101%’ |
Like ‘101%’ |
GetLikeStr |
1、select,update語句區別
select,update對於單表操作時基本一致。
但多表操作時Access與SqlServer中的update語句會有差別:
SqlServer中更新多表的update語句:
update Table1 set a.Name = b.Name from Table1 a, Table2 b where a.ID = b.ID;
Access中同樣功能的sql語句:
update Table1 a, Table2 b set a.Name = b.Name where a.ID = b.ID;
比較得出:Access中的update語句沒有from子句,所有引用的表都列在update關鍵字后
2、delete語句
SqlServer中:delete from <表名> where ID > 1;
Access中: delete * from<表名>where ID > 1;
3、as后面的計算字段區別
SqlServer中:
select sum(num) as sum_num, sum(num)*num as all_num from <表名>;
Access中:
select sum(num) as sum_num, sum_num*num as all_num from <表名>;
比較得出:
SqlServer中:不可以把as后的字段當作一個數據庫字段參與計算。
Access中:可以把as后的字段當作一個數據庫字段參與計算。
SQL Server as可以省略,Access不能
4、時間字段、日期分隔符號
SqlServer中:單引號(’)
select * from<表名> where RegDate = '2011-08-10'
Access中:要用#號分隔,轉換為日期
select * from<表名>where 生日=#2011-08-10#
select * from<表名>where 生日=2011-08-10
select * from<表名>where 生日=CDate(#2011-08-10#)
select * from<表名>where 生日=CDate(2011-08-10)
select * from<表名>where 生日=CDate('2011-08-10')
5、Boolean所表示的常量
SqlServer中:整數:1(真)、0(假)
Access中:True、False;On、Off;Yes、No;整數:-1(真)、0(假)。
6、字符串連接
SqlServer中:加號(+)
Access中:和號(&)
7、通配符
百分號(%)與零個或更多字符匹配。
下划線(_)與單個字符匹配。
上插入符(^)意味着不在列表中。
沒有與英鎊符(#)對應的字符。
Access中: 星號(*)與零個或更多字符匹配。
問號(?)與單個字符匹配。
嘆號(!)意味着不在列表中。
英鎊符(#)意味着單個數字。
8、DropIndex
SqlServer中:Drop Index<表名>.<索引名>
Access中: Drop Index<索引名> ON <表名>
9、表添加標識列
Access:alter table <表名> add 列名 Counter(1,1);
SqlServer:alter table <表名> add 列名 bigint identity(1,1) not null;
10、系統時間及常用時間函數
SqlServer: select getdate()
Access: select date()+time() SelectNow()
SqlServer 中獲取日期時間函數是convert(datetime,getdate(),0)
而在Access中為date()+time() 或者 Now()
在Access中,datediff ()和dateadd ()函數表示時間類型的部分必須用單引號括起來
select datediff('n',addtime,now()) from 表名;
select dateadd('d',5,now());
而在SqlServer中,必須寫成
select datediff(n,addtime,getdate()) from 表名;
select dateadd(d,5,getdate());
11、 Access不支持between語句和Case+When+Else語句
12、字符串函數(截取字符串、大寫、小寫、查找字符串位置)
Access: Mid、UCase、LCase、InStr
select Mid(列名,2,4) from <表名>;
select UCase(列名) from <表名>;
select LCase(列名) from <表名>;
select InStr(列名, 'abc') from <表名>;//查找字符串位置
SqlServer: SubString、Upper、Lower、CharIndex
select substring(列名,2,4) from <表名>;
select Upper(列名) from <表名>;
select Lower(列名) from <表名>;
select CharIndex('abc', 列名) from <表名>;//查找字符串位置
select CharIndex('abc', 列名,2) from <表名>;//查找字符串位置
13、 Access中的邏輯值在庫中為-1和0,而SQL Server中為1和0,所以寫BoolField= 1這樣的語句有兼容性問題,應該改為BoolField <>0
14、Access的varchar(文本)型最大只有255,所以如果一個文本型字段大於255時,最好定義成備注型(Access中)或text型(SQL Server中);
15、數據類型:
Access |
SQL Server |
SQL Server Definition |
Yes/No |
BIT |
(Integer: 0 or 1) |
Number (Byte) |
TINYINT |
(Positive Integer 0 -> 255) |
Number (Integer) |
SMALLINT |
(Signed Integer -32,768 -> 32,767) |
Number (Long Integer) |
INT |
(Signed Integer -(2^31) -> (2^31)-1) |
(no equivalent) |
BIGINT |
(Signed Integer -(2^63) -> (2^63)-1) |
Number (Single) |
REAL |
(Floating precision -1.79E + 308 -> 1.79E + 308) |
Number (Double) |
FLOAT |
(Floating precision -3.40E + 38 -> 3.40E + 38) |
Currency |
MONEY |
(4 decimal places, -(2^63)/10000 -> ((2^63)-1)/10000) |
Currency |
SMALLMONEY |
(4 decimal places, -214,748.3648 -> 214,748.3647) |
Hyperlink |
(no equivalent - use VARCHAR()) |
|
Decimal |
DECIMAL |
(Fixed precision -10^38 + 1 -> 10^38 - 1) |
Numeric |
NUMERIC |
(Fixed precision -10^38 + 1 -> 10^38 - 1) |
Date/Time |
DATETIME |
(Date+Time 1753-01-01 -> 9999-12-31, accuracy of 3.33 ms) |
Date/Time |
SMALLDATETIME |
(Date+Time 1900-01-01 -> 2079-06-06, accuracy of one minute) |
Text(n) |
CHAR(n) |
(Fixed-length non-Unicode string to 8,000 characters) |
Text(n) |
NCHAR(n) |
(Fixed-length Unicode string to 4,000 characters) |
Text(n) |
VARCHAR(n) |
(Variable-length non-Unicode string to 8,000 characters) |
Text(n) |
NVARCHAR(n) |
(Variable-length Unicode string to 4,000 characters) |
Memo |
TEXT |
(Variable-length non-Unicode string to 2,147,483,647 characters) |
Memo |
NTEXT |
(Variable-length Unicode string to 1,073,741,823 characters) |
OLE Object |
BINARY |
(Fixed-length binary data up to 8,000 characters) |
OLE Object |
VARBINARY |
(Variable-length binary data up to 8,000 characters) |
OLE Object |
IMAGE |
(Variable-length binary data up to 2,147,483,647 characters) |
AutonumberAutoincrement |
IDENTITY |
(any numeric data type, with IDENTITY property) |
Access如果用自動編號且主鍵,如下:
create table table1 (idautoincrement(1,1) primary key)
create table AAA(RID Counter,url Memo,ActNameMemo, ActDate text(150), Up_Time text(100))
連接字符串:conn = "Provider =Microsoft.Jet.OLEDB.4.0;Data Source = database.mdb";
16、隨機讀取若干條記錄
SqlServer: select top 5 * from <表名> order by newid();
Access: select top 5 * From <表名> ORDER BY Rnd(id);
17、條件取值Case+When+Else、IIF、Switch的使用
使用SQL語句用...代替過長的字符串顯示
SqlServer: select case
when len(列名)>10 then left(列名,10)+'...'
else 列名
end as 別名
from 表名;
Access: select IIF(len(列名)>2,left(列名,2)+'...', 列名) from 表名;
select Switch( On=1,'On', On=0,'Off') fromtable
18、余數
Access: a mod b
SqlServer: a % b
19、判斷字段值為空的區別
普通空:
Access和SqlServer一樣 where code is null 或where code is not null
條件空:
Access:iif(列名 is null,0, 列名) 或iif(列名 is null, 列名2, 列名)
SqlServer: isnull(列名,0) 或isnull(列名, 列名2)
20、截取字符串
Access: select Mid(列名,2,4)from <表名>;
SqlServer: select substring(列名,2,4) from <表名>;
21、字段類型轉換及一些函數異同
Access:
CDate將字符串轉化成為日期select CDate(“2011/08/11”)
CStr 將括號中的內容轉換為字符串,括號中的內容可以是值、變量或表達式
select CStr(155) 值是"155"
CInt 將 select Cint("200"&"40") 值是"20040"
Format 格式化字符串,select Format(now(),'yyyy-mm-dd')返回類似於"2011-08-11" , select Format (3/9,"0.00")返回0.33
Space產生空格select Space(4)返回4個空格
StrComp比較兩個字符串是否內容一致(不區分大小寫) select StrComp ("abc","ABC")
返回0, select StrComp("abc","123")返回-1
InStr 查詢子串在字符串中的位置select Instr("abc","a")返回1, select Instr ("abc","f")返回0
Mid 取得子字符串select Mid ("123",1,2) 返回12
Choose根據第一參數,返回后面字符串組的值, select Choose(1,"a","b","c")返回a,將1改成2后,返回b,改成3后,返回c(第一個參數也可以是某個字段)
IIF根據表達式返回特定的值select IIF("3>1","OK","False"),返回OK
SqlServer:
select cast('2011-08-11' as datetime)
select convert(varchar(10),getdate(),120) --2011-08-11
select replace(convert(varchar(10),cast('2011-08-11' as datetime),120), '-','') –20110811223308
22、Access使用Chr()將ASCII碼轉為字符,而SQL中類似函數為Char()
Access: SELECT CHR(13) & CHR(10)
SQL Server: SELECT CHAR(13) + CHAR(10)
23、字符串處理函數
Access的字符串函數不光可以在查詢數據庫的語句內部使用,也可以在VBA中直接使用。
Access |
SQL Server |
TEXT Equivalent |
CINT(), CLNG() |
CAST() |
CAST(SUBSTRING()) |
FORMAT() |
CONVERT() |
CONVERT(SUBSTRING()) |
INSTR() |
CHARINDEX() |
CHARINDEX(), PATINDEX() |
ISDATE() |
ISDATE() |
ISDATE(SUBSTRING()) |
ISNULL() |
ISNULL() |
ISNULL() |
ISNUMERIC() |
ISNUMERIC() |
ISNUMERIC(SUBSTRING()) |
LEFT() |
LEFT() |
SUBSTRING() |
LEN() |
LEN() |
DATALENGTH() |
LCASE() |
LOWER() |
LOWER(SUBSTRING()) |
LTRIM() |
LTRIM() |
LTRIM(SUBSTRING()) |
REPLACE() |
REPLACE() |
STUFF() |
RIGHT() |
RIGHT() |
SUBSTRING() |
RTRIM() |
RTRIM() |
RTRIM(SUBSTRING()) |
CSTR() |
STR() |
STR(SUBSTRING()) |
MID() |
SUBSTRING() |
SUBSTRING() |
UCASE() |
UPPER() |
UPPER(SUBSTRING()) |
StrConv() |
n/a |
n/a |
TRIM() |
n/a |
n/a |
24、Access里列別名必須使用As進行標記,SQLServer則可要可不要;表的別名則都是可用可不用。
25、Access的NOT IN速度慢
在通過VBA的JET引擎調用Access語法時,NOT IN速度非常慢,上千行的數據可能就需要好幾分鍾。替代方法是:
SELECT t1.id FROM t1 LEFT JOIN t2 ON t1.id = t2.id WHEREt2.id IS NULL
26、Currency與Money
Currency類型的好處是可以使用Format函數去添加貨幣符號和合適的小數點、千分位位置。但在SQLServer內部做不到這一點,需要先將結果取出然后使用外部函數去實現。
27、Yes/No與BIT
對比布爾類型的列,Access中可以使用True/False來比較或賦值,但SQL中最好直接使用整數。
--Access:
[...]WHERE ynColumn = FALSE
[...]WHERE ynColumn = 0
-- SQLServer:
[...]WHERE ynColumn = 0