|
报纸

楼主 |
发表于 2006-2-22 14:09:11
|
只看该作者
常用的四种SQL命令:<p>1.查询数据记录(Select)<br/>语法:Select 字段串行 From table Where 字段=内容<br/>例子:想从book表中找出作者为"cancer"的所有记录,SQL语句便如下:<br/>select * from book where author=’cancer’<br/>"*"是取出book表所有的字段,如查询的字段值为数字,则其后的"内容"便无须加上单引号,</p><p>如是日期,则在Access中用(#)包括,而在SQL server中则用(’)包括,<br/>如:</p><p>select * from book where id=1<br/>select * from book where pub_date=#2002-1-7# (Access)<br/>select * from book where pub_date=’2002-1-7’ (SQL Server)</p><p>提示:<br/>日期函数to_date不是标准sql文,不是所有的数据库适用,所以大家在使用的时候要参考数据库具体语法</p><p>另外如果是查询传入的变量,则如下:</p><p>strau=request.form("author")<br/>strsql="select * from book where author=’"&strau&"’"</p><p>如果查询的是数字,则:</p><p>intID=request.form("id")<br/>strsql="select * from book where id="&intID</p><p>在很多数据库中,如:oracle,上面的语句是可以写成:<br/>strsql="select * from book where id='"&intID&"'"的。<br/>但是字符型一定不能按照数字格式写,需要注意。</p><p>2.添加记录(Insert)<br/>语法:Insert into table(field1,field2,....) Values (value1,value2,....)<br/>例子:添加一作者是"cancer"的记录入book表:<br/>insert into book (bookno,author,bookname) values (’CF001’,’cancer’,’Cancer无组件上传程序’)<br/>同样,如果用到变量就如下:</p><p>strno=request.form("bookno")<br/>strau=request.form("author")<br/>strname=request.form("bookname")<br/>strsql="insert into book (bookno,author,bookname) values (’"&strno&"’,’"&strau&"’,’"&strname&"’)"</p><p>3.用Recordset对象的Addnew插入数据的方法:<br/>语法:</p><p>rs.addnew<br/>rs("field1").value=value1<br/>rs("field2").value=value2<br/>...<br/>rs.update</p><p>4.修改数据记录(Update)<br/>语法:update table set field1=value1,field2=value2,...where fieldx=valuex<br/>例子:update book set author=’babycrazy’ where bookno=’CF001’<br/>如果用到变量就如下:</p><p>strno=request.form("bookno")<br/>strau=request.form("author")<br/>strsql="update book set author=’"&strau&"’ where bookno=’"&strno"’"</p><p>5.Recordset对象的Update方法:<br/>语法:</p><p>rs("field1").value=value1<br/>rs("field2").value=value2<br/>...<br/>rs.update</p><p>注意:使用语法3和语法5的时候,一定要注意字段的类型(尤其是日期型)一致,否则出错的几率非常的高。</p><p><br/></p> |
|