SQL语言在ACCESS数据库中的应用Word下载.docx
《SQL语言在ACCESS数据库中的应用Word下载.docx》由会员分享,可在线阅读,更多相关《SQL语言在ACCESS数据库中的应用Word下载.docx(77页珍藏版)》请在冰豆网上搜索。
说明
predicate
下列谓词之一:
ALL、DISTINCT、DISTINCTROW或TOP。
可以使用谓词来限定返回记录的数量。
如果没有指定谓词,则默认值为ALL。
*
指定选择指定表中的所有字段。
table
表的名称,该表包含从中选择记录的字段。
field1、field2
字段名,这些字段包含了要检索的数据。
如果包括多个字段,将按它们的排列顺序对其进行检索。
alias1和alias2
用作列标题的名称,不是table中的原始列名。
tableexpression
表名称,其中包含要检索的数据。
externaldatabase
如果tableexpression中的表不在当前数据库中,则使用该参数指定该数据库名。
若要执行此项操作,Microsoft®
Jet数据库引擎会搜索指定的表,并提取选定的列,再选择符合条件的行,然后按指定的顺序对得到的行进行排序或分组。
SELECT语句不会更改数据库中的数据。
SELECT通常是SQL语句中的第一个词。
大多数SQL语句都是SELECT或SELECT...INTO语句。
SELECT语句最简化的语法为:
SELECTfieldsFROMtable
可以通过星号(*)来选择表中所有的字段。
以下的示例选择在Employees表中的所有字段:
SELECT*FROMEmployees;
如果一个字段名包括于FROM子句的多个表中,请在该字段前面加上表名和.(圆点)号。
在下面的示例中,Department字段同时存在于Employees表和Supervisors表中。
SQL语句从Employees表中选择出部门并从Supervisors表中选择出主管名:
SELECTEmployees.Department,Supervisors.SupvName
FROMEmployeesINNERJOINSupervisors
WHEREEmployees.Department=Supervisors.Department;
创建Recordset对象时,MicrosoftJet数据库引擎将使用表的字段名作为Recordset对象中的Field对象名。
如果需要其他字段名或者名称不适合用来生成该字段的表达式,请使用AS保留字。
以下示例使用标题Birth来命名生成的Recordset对象中的返回Field对象:
SELECTBirthDate
ASBirthFROMEmployees;
只要使用的聚合函数或查询返回的是不明确的或重复的Field对象名称,就必须使用AS子句为该Field对象另外提供一个替代名称。
以下示例使用标题HeadCount来命名生成的Recordset对象中的返回Field对象:
SELECTCOUNT(EmployeeID)
ASHeadCountFROMEmployees;
可以在SELECT语句中使用其他子句进一步约束和组织所返回的数据。
有关详细信息,请参阅相应子句的帮助主题。
示例
下面的一些示例假定Employees表中存在一个假想的Salary字段。
请注意,该字段实际并不存在于罗斯文数据库的Employees表中。
本例基于SQL语句创建一个动态集类型的Recordset,该语句选择Employees表中所有记录的LastName和FirstName字段。
它调用EnumFields过程,该过程将Recordset对象的容显示到调试窗口。
SubSelectX1()
DimdbsAsDatabase,rstAsRecordset
'
ModifythislinetoincludethepathtoNorthwind
onyourcomputer.
Setdbs=OpenDatabase("
Northwind.mdb"
)
Selectthelastnameandfirstnamevaluesofall
recordsintheEmployeestable.
Setrst=dbs.OpenRecordset("
SELECTLastName,"
_
&
"
FirstNameFROMEmployees;
"
Populatetherecordset.
rst.MoveLast
CallEnumFieldstoprintthecontentsofthe
Recordset.
EnumFieldsrst,12
dbs.Close
EndSub
以下示例计算PostalCode字段中有条目的记录数,并将返回的字段命名为Tally。
SubSelectX2()
CountthenumberofrecordswithaPostalCode
valueandreturnthetotalintheTallyfield.
SELECTCount"
(PostalCode)ASTallyFROMCustomers;
PopulatetheRecordset.
CallEnumFieldstoprintthecontentsof
theRecordset.Specifyfieldwidth=12.
EnumFieldsrst,12
以下示例显示雇员数以及平均薪水和最高薪水。
SubSelectX3()
Countthenumberofemployees,calculatethe
averagesalary,andreturnthehighestsalary.
SELECTCount(*)"
ASTotalEmployees,Avg(Salary)"
ASAverageSalary,Max(Salary)"
ASMaximumSalaryFROMEmployees;
CallEnumFieldstoprintthecontentsof
theRecordset.PasstheRecordsetobjectand
desiredfieldwidth.
EnumFieldsrst,17
调用过程向Sub过程EnumFields传递了一个Recordset对象。
然后该过程设置Recordset的字段的格式并将这些字段显示到调试窗口。
intFldLen
变量是需要的显示字段宽度。
有些字段可能会被截断。
SubEnumFields(rstAsRecordset,intFldLenAsInteger)
DimlngRecordsAsLong,lngFieldsAsLong
DimlngRecCountAsLong,lngFldCountAsLong
DimstrTitleAsString,strTempAsString
SetthelngRecordsvariabletothenumberof
recordsintheRecordset.
lngRecords=rst.RecordCount
SetthelngFieldsvariabletothenumberof
fieldsintheRecordset.
lngFields=rst.Fields.Count
Debug.Print"
Thereare"
lngRecords_
recordscontaining"
lngFields_
fieldsintherecordset."
Debug.Print
Formastringtoprintthecolumnheading.
strTitle="
Record"
ForlngFldCount=0TolngFields-1
strTitle=strTitle_
Left(rst.Fields(lngFldCount).Name_
Space(intFldLen),intFldLen)
NextlngFldCount
Printthecolumnheading.
Debug.PrintstrTitle
LoopthroughtheRecordset;
printtherecord
numberandfieldvalues.
rst.MoveFirst
ForlngRecCount=0TolngRecords-1
Debug.PrintRight(Space(6)&
Str(lngRecCount),6)&
;
CheckforNullvalues.
IfIsNull(rst.F