一、有区别的函数及解决方案
以下所示的解决方案中的函数定义在untDataBase单元中TAdoConn类的方法中。
序号 | 简述 | Access语法 | SqlServer语法 | Oracle语法 | DB2语法 | 解决方案 |
01 | 系统时间 | Date()/Now() | GETDATE() | SYSDATE |
| GetSysTimeStr |
02 | 连接字符串 | & | + | || | + | GetConcatStr |
03 | 截取字符串 | Mid | SubString | SubStr | SubString | GetSubStr |
04 | 小写字符串 | LCase | Lower | Lower | Lower | GetLowerStr |
05 | 大写字符串 | UCase | Upper | Upper | Upper | GetUpperStr |
06 | 查找字符串 | InStr | CharIndex | CharIndex | InStr | GetFindStr |
07 | 替换空值 | IIF+IsNull | Coalesce | Nvl | Coalesce | GetNullStr |
08 | 条件取值 | IIF | Case+When+Else | DeCode或Case | IIF | GetCaseStr |
09 | 字段类型转换 | Str、var、…. | Convert或cast | To_Char,To_Number. |
| GetConvertStr |
10 | 日期字符串 | ‘2004-10-9’ | #2004-10-19# | ‘2004-10-9’ |
| GetDateStr |
11 | 最大值加1 |
|
|
|
| GetNextNumStr |
12 | Like语句函数 | Like ‘101*’ | Like ‘101%’ | Like ‘101%’ |
| GetLikeStr |
13 | 日期比较 | DateDiff('d',....... | DateDiff(d,...... |
|
|
|
14 |
|
|
|
|
|
|
15 |
|
|
|
|
|
|
16 |
|
|
|
|
|
|
17 |
|
|
|
|
|
|
18 |
|
|
|
|
|
|
19 |
|
|
|
|
|
|
二、Access与SQLSERVER部分相同数据库函数及关键字列表
1、 函数
序号 | 简述 |
|
01 | 记数函数 | Count |
02 | 最大值 | Max |
03 |
|
|
04 |
|
|
2、 关键字
序号 | 简述 |
|
01 |
| Like |
02 | 连接 | Join |
03 | 判断空 | Is Null |
04 |
|
|
三、Access与语句SqlServer的语句语法区别
1、 Inser Into …..Select …From 语句:
在ACCESS中以下语句
INSERT INTO
PubSubJectAccCopys(Copy_id,Acc_id,Acc_Pid,Acc_name,acc_short,Acc_Comment,Acc_Pro,acc_type,Sub_id_flag,acc_index) (SELECT 200201,Acc_id,Acc_Pid,Acc_name,acc_short,Acc_Comment,Acc_Pro,acc_type,Sub_id_flag,acc_index FROM PubSubJectAcc WHERE PubSubJectAcc.co_type='03')
中后面"(select 200201******.co_Type='03')"中的小括号("(",")")必须去掉才能执行,如下:
INSERT INTO
PubSubJectAccCopys(Copy_id,Acc_id,Acc_Pid,Acc_name,acc_short,Acc_Comment,Acc_Pro,acc_type,Sub_id_flag,acc_index) SELECT 200201,Acc_id,Acc_Pid,Acc_name,acc_short,Acc_Comment,Acc_Pro,acc_type,Sub_id_flag,acc_index FROM PubSubJectAcc WHERE PubSubJectAcc.co_type='03'
在SQL SERVER 中都可以
2、 Inner Join 语句1
StrSql:='select a.user_id,a.user_opcode,b.copy_name from sysuser a inner join (syscopysuser c inner join syscopys b on c.copy_id=c.copy_id) on a.user_id=c.user_id where
a.user_opcode=''' +EdtUserOpCode.text+''' And copy_name='''+Tmpcopyname +'''';
应该改为
StrSql:='select a.user_id,a.user_opcode,b.copy_name from sysuser a inner join (syscopysuser c inner join syscopys b on c.copy_id=d.copy_id) on a.user_id=c.user_id where
a.user_opcode=''' +EdtUserOpCode.text+''' And copy_name='''+Tmpcopyname +'''';
该行代码的检索条件错误:应该把C.copy_id=C.Copy_id 改为c.copy_id=d.copy_id
注:两种写法都能在SQL-SERVER中运行,但c.copy_id=C.copy_id在ACCESS中不能运行
3、 Inner Join 语句2
StrSql:='select copy_year,copy_name,a.copy_id from SysCopys a inner join SysCopysUser b on a.curcopy_flag=1 and a.copy_id=b.copy_id where b.user_id=' + '''' +TmpPubUserID+ '''';
该为
StrSql:='select copy_year,copy_name,a.copy_id from SysCopys a inner join SysCopysUser b on a.copy_id=b.copy_id where a.curcopy_flag=''1'' and b.user_id=' + '''' +TmpPubUserID+ '''';
注:两种写法都能在SQL-SERVER中运行,但第一种在ACCESS中不能运行
4、 Inner Join语句3
SQl server 中可以执行以下语句
'SELECT distinct sysoption.opti_id,sysoption.opti_name,sysoption.opti_code,sysroleoption.opti_sort From sysoption inner join sysroleoption ON sysoption.opti_id=sysroleoption.opti_id AND sysroleoption.role_id=:roleid'
但ACCESS中不能,只能
'SELECT distinct sysoption.opti_id,sysoption.opti_name,sysoption.opti_code,sysroleoption.opti_sort From sysoption inner join sysroleoption ON sysoption.opti_id=sysroleoption.opti_id Where sysroleoption.role_id=:roleid'
5、 Update语句
Sql SerVer 中能执行但Access 中不能
'UPDATE sysuserrole SET sysuserrole.role_sort = (SELECT sysrole.role_sort FROM sysrole WHERE sysuserrole.role_id = sysrole.role_id and sysuserrole.user_id='01')'
6、 日期比较
SQL SERVER 中用
StrSql:='select copy_year,Start_month,Cur_month,Start_Flag,Start_date,End_date '
+'From SysCopys '
+'where copy_id='''+LoginCopyID+''' '
+'and start_date<='''+datetostr(LoginDate)+''' '
+'and end_date>='''+datetostr(LoginDate)+'''';
ACCESS中用
StrSql:='select copy_year,Start_month,Cur_month,Start_Flag,Start_date,End_date '
+'From SysCopys '
+'where copy_id='''+LoginCopyID+''' '
+'and start_date<=#'+datetostr(LoginDate)+'# '
+'and end_date>=#'+datetostr(LoginDate)+'#'
参考以上的第10个函数“GetDateStr”
7、 最大数值获取语句
StrSql:='insert into sysRoleOption '
+'select '''+fidRoleId+''' as Role_ID,opti_id,'
+'convert(numeric,opti_id)-(convert(numeric,opti_parentid)*100)+'+ MaxOptiSort
+' as opti_Sort from sysoption where opti_parentid='''
+PCoTypeID(self.trvRoles.Selected.data)^.StrCoTypeID
+''' and opti_bottom=''1'+'''';
改为
StrSql:='insert into sysRoleOption '
+'select '''+fidRoleId+''' as Role_ID,opti_id,'
+'opti_id-opti_parentid*100+'+ MaxOptiSort
+' as opti_sort from sysoption where opti_parentid='''
+PCoTypeID(self.trvRoles.Selected.data)^.StrCoTypeID
+''' and opti_bottom=''1'+''''
注:两种写法都能在SQL-SERVER中运行,但第一种在ACCESS中不能运行
二
1,对于日期字段字段
access表示为:#1981-28-12#
SQLSERVER2000表示为:‘‘1981-02-12‘‘
2,SQL语句区别,select ,update 在对单表操作时都差不多,
但多表操作时update语句的区别ACCESS与SQLSERVER中的Update语句对比:
SQLSERVER中更新多表的Update语句:
Update Tab1
SET a.Name = b.Name
FROM Tab1 a,Tab2 b
Where a.ID = b.ID;
同样功能的SQL语句在ACCESS中应该是
Update Tab1 a,Tab2 b
SET a.Name = b.Name
Where a.ID = b.ID;
即:ACCESS中的Update语句没有FROM子句,所有引用的表都列在Update关键字后.
更新单表时:都为:
Update table1 set ab=‘12‘,cd=444 where ....
3,delete语句
access中删除时用:delete * from table1 where a>2 即只要把select 语句里的select 换成delete就可以了。
sqlserve 中则为: delete from table1 where a>2 即没有*号
4,as 后面的计算字段区别
access中可以这样:select a,sum(num) as kc_num,kc_num*num as all_kc_num 即可以把AS后的字段当作一个数据库字段参与计算。
sqlserver 中则为:select a,sum(num) as kc_num,sum(num)*num as all_kc_num 即不可以把AS后的字段当作一个数据库字段参与计算。
5,[.]与[!]的区别
access中多表联合查询时:select tab1!a as tab1a,tab2!b tab2b from tab1,tab2 ,中间的AS可以不要。
sqlserve 中则:select tab1.a as tab1a,tab2.b tab2b from tab1,tab2 ,中间的AS可以不要。
6,联合查询时,
access中多表联合查询:‘select a,b from(
select a,b from tab1 where a>3 union select c,d from tab2 ) group by a,b
sqlserve 中则‘select a,b from(
select a,b from tab1 where a>3 union select c,d from tab2 ) tmptable group by a,b即要加一个虚的表tmptable,表名任意。---
7,access升级到sqlserver时,
可以用sqlserver的数据导入工具导入数据,但要做必要的处理。
access中的自动编号,不会自动转换SQL中的自动编号,只能转换为int型,要把它手工改成标识字段,种子为1,把所有导入被sqlserver 转化成的以n开头的字段类型的n去掉,如nvarchar->varchar.把需要有秒类型的日期字段改成datatime类型(SQL会把所有 的日期开转化成smalldatetime型)
8,true与1=1
access用where true表示条件为真,
sqlserver用where 1=1表示条件为真
9,判断字段值为空的区别
普通空:
Access和sql server一样 where code is null 或 where code is nol null
条件空:
Access:iif([num] is null,0,[num]) 或 iif([num] is null,[num1],[num])
SQLServer: isnull([num],0) 或 isnull([num],[num1])
10,SQL语句取子串的区别
access:MID(字段,n1,[n2]),LEFT(字段,n),RIGHT(字段,n)
如:select left(cs1,4)+‘-‘+cs2 as cs3
SQLServer: SUBSTRING(expression, start, length)
如:select substring(cs1, 1, 2) + substring(cs1, 4, 2) + ‘-‘ + cs2 as cs3
三
ISNULL
使用指定的替换值替换 NULL。
语法
ISNULL ( check_expression , replacement_value )
参数
check_expression
将被检查是否为 NULL的表达式。check_expression 可以是任何类型的。
replacement_value
在 check_expression 为 NULL时将返回的表达式。replacement_value 必须与 check_expresssion
具有相同的类型。
返回类型
返回与 check_expression 相同的类型。
注释
如果 check_expression 不为 NULL,那么返回该表达式的值;否则返回 replacement_value。
对于T-SQL与Access中替换NULL值,可以用下面的方法实现:
庆幸的是Access支持iif,所以可以这样去替代:
1. Select iif(IsNull( express ), value1, value2 ) From TableName
1. iif(IsNull( express ), value1, value2 )
相当于:
1. If express Is Null Then
2. Return value1
3. Else
4. Return value2
5. End if
举个例子,在SQL Server中:
1. select IsNull(int_Filed1, 0) from Table1
换作Access,则应该被改写为:
1. select iif( IsNull(int_Filed1), 0, int_Filed1 ) from Table1