笨笨向前冲吧 关注:24贴子:1,512
  • 1回复贴,共1

【计算机--SQL】常用的四种SQL命令

只看楼主收藏回复

常用的四种SQL命令:

   1.查询数据记录(Select)

  语法:Select 字段串行 From table Where 字段=内容
  例子:想从book表中找出作者为"cancer"的所有记录,SQL语句便如下:


CODE:[Copy to clipboard]select * from book where author=’cancer’
  "*"是取出book表所有的字段,如查询的字段值为数字,则其后的"内容"便无须加上单引号,如是日期,则在Access中用(#)包括,而在SQL server中则用(’)包括,如:


CODE:select * from book where id=1
select * from book where pub_date=#2002-1-7# (Access)
select * from book where pub_date=’2002-1-7’ (SQL Server)
  提示:
  日期函数to_date不是标准sql文,不是所有的数据库适用,所以大家在使用的时候要参考数据库具体语法

  另外如果是查询传入的变量,则如下:


CODE:strau=request.form("author")
strsql="select * from book where author=’"&strau&"’"
  如果查询的是数字,则:


CODE:intID=request.form("id")
strsql="select * from book where id="&intID
  在很多数据库中,如:oracle,上面的语句是可以写成:


CODE:strsql="select * from book where id='"&intID&"'"
  但是字符型一定不能按照数字格式写,需要注意。

  2.添加记录(Insert)

  语法:


CODE:Insert into table(field1,field2,....) Values (value1,value2,....)
  例子:添加一作者是"cancer"的记录入book表:


CODE:insert into book (bookno,author,bookname) values (’CF001’,’cancer’,’Cancer无组件上传程序’)
  同样,如果用到变量就如下:


CODE:strno=request.form("bookno")
strau=request.form("author")
strname=request.form("bookname")
strsql="insert into book (bookno,author,bookname) values (’"&strno&"’,’"&strau&"’,’"&strname&"’)"
 
  3.用Recordset对象的Addnew插入数据的方法:

  语法:


CODE:rs.addnew
rs("field1").value=value1
rs("field2").value=value2
...
rs.update
 
  4.修改数据记录(Update)

  语法:


CODE:update table set field1=value1,field2=value2,...where fieldx=valuex
  例子:


CODE:update book set author=’babycrazy’ where bookno=’CF001’
  如果用到变量就如下:


CODE:strno=request.form("bookno")
strau=request.form("author")
strsql="update book set author=’"&strau&"’ where bookno=’"&strno"’"
 
  5.Recordset对象的Update方法:

  语法:


CODE:rs("field1").value=value1
rs("field2").value=value2
...
rs.update
  注意:使用语法3和语法5的时候,一定要注意字段的类型(尤其是日期型)一致,否则出错的几率非常的高。

  例子:


CODE:strno=request.form("bookno")
strau=request.form("author")
set adocon=server.createobject("adodb.connection")
adocon.open "Driver={Microsoft Access Driver(*.mdb)};DBQ=" & _
Server.Mappath=("/cancer/cancer.mdb")
strsql="select * from book where bookno=’"&strno&"’"
set rs=server.createobject("adodb.recordset")
rs.open strsql,adconn,1,3
if not rs.eof then ’如果有此记录的话
rs("author").value=strau
rs.update
end if
rs.close
set rs=nothing
adocon.close
set adocon=nothing
 
  6.删除一条记录(Delete)

  语法:


CODE:Delete table where field=value
  例子:删除book表中作者是cancer的记录


CODE:delete book where author=’cancer’
  (注意:如果book表中author字段的值为cancer的记录有多条,将会删除所有author为cancer的记录)

  好了,学会了用这些操作,大家在用asp操作数据库的时候,该是没有什么问题了。


1楼2006-07-05 18:35回复
    • 123.13.50.*
    很好,力鼎!!!!


    2楼2009-03-19 13:46
    回复