|  | 
 
| 下列语句部分是Mssql语句,不可以在access中使用。 
 SQL分类:
 DDL—数据定义语言(CREATE,ALTER,DROP,DECLARE)
 DML—数据操纵语言(SELECT,DELETE,UPDATE,INSERT)
 DCL—数据控制语言(GRANT,REVOKE,COMMIT,ROLLBACK)
 
 首先,简要介绍基础语句:
 1、说明:创建数据库
 <!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->CREATEDATABASEdatabase-name
 
 
 2、说明:删除数据库
 
 <!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->dropdatabasedbname
 
 3、说明:备份sql server
 --- 创建 备份数据的 device
 
 <!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->USEmaster
 EXECsp_addumpdevice'disk','testBack','c:/mssql7backup/MyNwind_1.dat'
 
 --- 开始 备份
 <!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->BACKUPDATABASEpubsTOtestBack
 
 
 4、说明:创建新表
 
 <!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->createtabletabname(col1type1[notnull][primarykey],col2type2[notnull],..)
 
 根据已有的表创建新表:
 
 <!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->A:createtabletab_newliketab_old(使用旧表创建新表)
 B:createtabletab_newasselectcol1,col2…fromtab_olddefinitiononly
 
 5、说明:删除新表drop table tabname
 6、说明:增加一个列
 <!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->Altertabletabnameaddcolumncoltype
 
 
 注:列增加后将不能删除。DB2中列加上后数据类型也不能改变,唯一能改变的是增加varchar类型的长度。
 7、说明:添加主键:
 <!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->Altertabletabnameaddprimarykey(col)
 
 
 说明:删除主键:
 <!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->Altertabletabnamedropprimarykey(col)
 
 
 8、说明:创建索引:
 <!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->create[unique]indexidxnameontabname(col….)
 
 
 删除索引:drop index idxname
 注:索引是不可更改的,想更改必须删除重新建。
 9、说明:创建视图:
 <!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->createviewviewnameasselectstatement
 
 
 删除视图:drop view viewname
 10、说明:几个简单的基本的sql语句
 
    代码<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->选择:select*fromtable1where范围 插入:insertintotable1(field1,field2)values(value1,value2)
 删除:deletefromtable1where范围
 更新:updatetable1setfield1=value1where范围
 查找:select*fromtable1wherefield1like’%value1%’---like的语法很精妙,查资料!
 排序:select*fromtable1orderbyfield1,field2[desc]
 总数:selectcount*astotalcountfromtable1
 求和:selectsum(field1)assumvaluefromtable1
 平均:selectavg(field1)asavgvaluefromtable1
 最大:selectmax(field1)asmaxvaluefromtable1
 最小:selectmin(field1)asminvaluefromtable1
 
 
 
 11、说明:几个高级查询运算词
 A: UNION 运算符
 UNION 运算符通过组合其他两个结果表(例如 TABLE1 和 TABLE2)并消去表中任何重复行而派生出一个结果表。当 ALL 随 UNION 一起使用时(即 UNION ALL),不消除重复行。两种情况下,派生表的每一行不是来自 TABLE1 就是来自 TABLE2。
 B: EXCEPT 运算符
 EXCEPT 运算符通过包括所有在 TABLE1 中但不在 TABLE2 中的行并消除所有重复行而派生出一个结果表。当 ALL 随 EXCEPT 一起使用时 (EXCEPT ALL),不消除重复行。
 C: INTERSECT 运算符
 INTERSECT 运算符通过只包括 TABLE1 和 TABLE2 中都有的行并消除所有重复行而派生出一个结果表。当 ALL 随 INTERSECT 一起使用时 (INTERSECT ALL),不消除重复行。
 注:使用运算词的几个查询结果行必须是一致的。
 12、说明:使用外连接
 A、left outer join:
 左外连接(左连接):结果集几包括连接表的匹配行,也包括左连接表的所有行。
 SQL: select a.a, a.b, a.c, b.c, b.d, b.f from a LEFT OUT JOIN b ON a.a = b.c
 B:right outer join:
 右外连接(右连接):结果集既包括连接表的匹配连接行,也包括右连接表的所有行。
 C:full outer join:
 全外连接:不仅包括符号连接表的匹配行,还包括两个连接表中的所有记录。
 
 其次,大家来看一些不错的sql语句
 1、说明:复制表(只复制结构,源表名:a 新表名:b) (Access可用)
 
 
 <!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->法一:select*intobfromawhere1<>1
 法二:selecttop0*intobfroma
 
 2、说明:拷贝表(拷贝数据,源表名:a 目标表名:b) (Access可用)
 insert into b(a, b, c) select d,e,f from b;
 
 3、说明:跨数据库之间表的拷贝(具体数据使用绝对路径) (Access可用)
 insert into b(a, b, c) select d,e,f from b in ‘具体数据库’ where 条件
 例子:..from b in '"&Server.MapPath(".")&"/data.mdb" &"' where..
 
 4、说明:子查询(表名1:a 表名2:b)
 
 
 <!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->selecta,b,cfromawhereaIN(selectdfromb)或者:selecta,b,cfromawhereaIN(1,2,3)
 
 5、说明:显示文章、提交人和最后回复时间
 
 
 <!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->selecta.title,a.username,b.adddatefromtablea,(selectmax(adddate)adddatefromtablewheretable.title=a.title)b
 
 6、说明:外连接查询(表名1:a 表名2:b)
 
 
 <!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->selecta.a,a.b,a.c,b.c,b.d,b.ffromaLEFTOUTJOINbONa.a=b.c
 
 7、说明:在线视图查询(表名1:a )
 
 
 <!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->select*from(SELECTa,b,cFROMa)Twheret.a>1;
 
 8、说明:between的用法,between限制查询数据范围时包括了边界值,not between不包括
 
 
 <!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->select*fromtable1wheretimebetweentime1andtime2
 selecta,b,c,fromtable1whereanotbetween数值1and数值2
 
 9、说明:in 的使用方法
 
 
 <!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->select*fromtable1wherea[not]in(‘值1’,’值2’,’值4’,’值6’)
 
 10、说明:两张关联表,删除主表中已经在副表中没有的信息
 
 
 <!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->deletefromtable1wherenotexists(select*fromtable2wheretable1.field1=table2.field1)
 
 11、说明:四表联查问题:
 
 
 <!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->select*fromaleftinnerjoinbona.a=b.brightinnerjoincona.a=c.cinnerjoindona.a=d.dwhere.....
 
 12、说明:日程安排提前五分钟提醒
 
 
 <!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->SQL:select*from日程安排wheredatediff('minute',f开始时间,getdate())>5
 
 13、说明:一条sql 语句搞定数据库分页
 
 
 <!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->selecttop10b.*from(selecttop20主键字段,排序字段from表名orderby排序字段desc)a,表名bwhereb.主键字段=a.主键字段orderbya.排序字段
 
 14、说明:前10条记录
 
 
 <!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->selecttop10*formtable1where范围
 
 15、说明:选择在每一组b值相同的数据中对应的a最大的记录的所有信息(类似这样的用法可以用于论坛每月排行榜,每月热销产品分析,按科目成绩排名,等等.)
 
 
 <!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->selecta,b,cfromtablenametawherea=(selectmax(a)fromtablenametbwheretb.b=ta.b)
 
 16、说明:包括所有在 TableA 中但不在 TableB和TableC 中的行并消除所有重复行而派生出一个结果表
 
 
 <!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->(selectafromtableA)except(selectafromtableB)except(selectafromtableC)
 
 17、说明:随机取出10条数据
 
 
 <!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->selecttop10*fromtablenameorderbynewid()
 
 18、说明:随机选择记录
 
 
 <!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->selectnewid()
 
 19、说明:删除重复记录
 
 
 <!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->Deletefromtablenamewhereidnotin(selectmax(id)fromtablenamegroupbycol1,col2,...)
 
 20、说明:列出数据库里所有的表名
 
 
 <!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->selectnamefromsysobjectswheretype='U'
 
 21、说明:列出表里的所有的
 
 
 <!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->selectnamefromsyscolumnswhereid=object_id('TableName')
 
 22、说明:列示type、vender、pcs字段,以type字段排列,case可以方便地实现多重选择,类似select 中的case。
 
 <!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->selecttype,sum(casevenderwhen'A'thenpcselse0end),sum(casevenderwhen'C'thenpcselse0end),sum(casevenderwhen'B'thenpcselse0end)FROMtablenamegroupbytype
 
 显示结果:
 type vender pcs
 电脑 A 1
 电脑 A 1
 光盘 B 2
 光盘 A 2
 手机 B 3
 手机 C 3
 
 23、说明:初始化表table1
 
 
 <!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->TRUNCATETABLEtable1
 
 24、说明:选择从10到15的记录
 
 <!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->selecttop5*from(selecttop15*fromtableorderbyidasc)table_别名orderbyiddesc
 
 
 随机选择数据库记录的方法(使用Randomize函数,通过SQL语句实现)
 对存储在数据库中的数据来说,随机数特性能给出上面的效果,但它们可能太慢了些。你不能要求ASP“找个随机数”然后打印出来。实际上常见的解决方案是建立如下所示的循环:
 <!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->Randomize
 RNumber=Int(Rnd*499)+1
 
 WhileNotobjRec.EOF
 IfobjRec("ID")=RNumberTHEN
 ...这里是执行脚本...
 endif
 objRec.MoveNext
 Wend
 
 
 
 这很容易理解。首先,你取出1到500范围之内的一个随机数(假设500就是数据库内记录的总数)。然后,你遍历每一记录来测试ID 的值、检查其是否匹配RNumber。满足条件的话就执行由THEN 关键字开始的那一块代码。假如你的RNumber 等于495,那么要循环一遍数据库花的时间可就长了。虽然500这个数字看起来大了些,但相比更为稳固的企业解决方案这还是个小型数据库了,后者通常在一个数据库内就包含了成千上万条记录。这时候不就死定了?
 采用SQL,你就可以很快地找出准确的记录并且打开一个只包含该记录的recordset,如下所示:
 代码<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->Randomize
 RNumber=Int(Rnd*499)+1
 
 SQL="SELECT*FROMCustomersWHEREID="&RNumber
 
 setobjRec=ObjConn.Execute(SQL)
 Response.WriteRNumber&"="&objRec("ID")&""&objRec("c_email")
 
 
 
 
 不必写出RNumber 和ID,你只需要检查匹配情况即可。只要你对以上代码的工作满意,你自可按需操作“随机”记录。Recordset没有包含其他内容,因此你很快就能找到你需要的记录这样就大大降低了处理时间。
 再谈随机数
 现在你下定决心要榨干Random 函数的最后一滴油,那么你可能会一次取出多条随机记录或者想采用一定随机范围内的记录。把上面的标准Random 示例扩展一下就可以用SQL应对上面两种情况了。
 为了取出几条随机选择的记录并存放在同一recordset内,你可以存储三个随机数,然后查询数据库获得匹配这些数字的记录:
 
 <!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->SQL="SELECT*FROMCustomersWHEREID="&RNumber&"ORID="&RNumber2&"ORID="&RNumber3
 
 
 假如你想选出10条记录(也许是每次页面装载时的10条链接的列表),你可以用BETWEEN 或者数学等式选出第一条记录和适当数量的递增记录。这一操作可以通过好几种方式来完成,但是 SELECT 语句只显示一种可能(这里的ID 是自动生成的号码):
 
 
 <!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->SQL="SELECT*FROMCustomersWHEREIDBETWEEN"&RNumber&"AND"&RNumber&"+9"
 
 注意:以上代码的执行目的不是检查数据库内是否有9条并发记录。
 
 
 随机读取若干条记录,测试过
 
 <!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->Access语法:SELECTtop10*From表名ORDERBYRnd(id)
 Sqlserver:selecttopn*from表名orderbynewid()
 mysqlelect*From表名OrderByrand()Limitn
 
 Access左连接语法(最近开发要用左连接,Access帮助什么都没有,网上没有Access的SQL说明,只有自己测试, 现在记下以备后查)
 语法elect table1.fd1,table1,fd2,table2.fd2 From table1 left join table2 on table1.fd1,table2.fd1 where ...
 使用SQL语句 用...代替过长的字符串显示
 语法:
 
 <!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->SQL数据库:selectcasewhenlen(field)>10thenleft(field,10)+'...'elsefieldendasnews_name,news_idfromtablename
 Access数据库:SELECTiif(len(field)>2,left(field,2)+'...',field)FROMtablename;
 
 
 Conn.Execute说明
 Execute方法
 该方法用于执行SQL语句。根据SQL语句执行后是否返回记录集,该方法的使用格式分为以下两种:
 1.执行SQL查询语句时,将返回查询得到的记录集。用法为:
 Set 对象变量名=连接对象.Execute("SQL 查询语言")
 Execute方法调用后,会自动创建记录集对象,并将查询结果存储在该记录对象中,通过Set方法,将记录集赋给指定的对象保存,以后对象变量就代表了该记录集对象。
 
 2.执行SQL的操作性语言时,没有记录集的返回。此时用法为:
 连接对象.Execute "SQL 操作性语句" [, RecordAffected][, Option]
 ·RecordAffected 为可选项,此出可放置一个变量,SQL语句执行后,所生效的记录数会自动保存到该变量中。通过访问该变量,就可知道SQL语句队多少条记录进行了操作。
 ·Option 可选项,该参数的取值通常为adCMDText,它用于告诉ADO,应该将Execute方法之后的第一个字符解释为命令文本。通过指定该参数,可使执行更高效。
 
 ·BeginTrans、RollbackTrans、CommitTrans方法
 
 | 
 |