现在的位置: 首页 > 综合 > 正文

SQL的基本命令和几个常用函数汇总

2012年05月23日 ⁄ 综合 ⁄ 共 1358字 ⁄ 字号 评论关闭
--创建对象(表、视图、存储过程、函数)命令]
create table/view/procedure/function
--创建表
create table tabtestvb
(vbname varchar(10),value numeric(10))
go
create table tabtestvb1
(vbname varchar(10),value1 numeric(10))
go
--插入数据(两种方式)
insert into tabtestvb(vbname,value)
select aaa,123
insert into tabtestvb1(vbname,value1)
select aaa,456

insert into tabtestvb(vbname,value) values (bbb,345)
insert into tabtestvb1(vbname,value1) values (ccc,1002)
--更改数据
update tabtestvb set value=798 where vbname=aaa
--关联更改
update tabtestvb set value=tabtestvb1.value1
from tabtestvb1 where tabtestvb.vbname=tabtestvb1.vbname
--删除数据
delete tabtestvb where  vbname=aaa
--无日志删除数据
truncate table tabtestvb
--删除对象(表、视图、存储过程、函数)命令
drop table/view/proc/function
--删除表
drop table tabtestvb
drop table tabtestvb1
--赋值命令
set
--定义变量
declare

--流程控制语句
while ... break
begin ... end
if ...else
----1...100 的和
declare @nn numeric(3)
declare @sum numeric(8)
set @nn=1
set @sum=0
while @nn<=100
 begin
  set @sum=@sum+@nn
  set @nn=@nn+1
 end
select @sum

--加上条件:当@nn=20 时退出循环(计算出1...19的和)
declare @nn numeric(3)
declare @sum numeric(8)
set @nn=1
set @sum=0
while @nn<=100
 begin
  if @nn<>20
   --begin
   set @sum=@sum+@nn
   --end
  else
   --begin
   break
   --end
  set @nn=@nn+1
 end
select @sum

--全局变量
@@rowcount
--返回受上一语句影响的行数
select 1
union all
select 3
select @@rowcount

@@error
--返回最后执行的 transact-sql 语句的错误代码。
set @n =1
select @@error

----函数的使用
--返回当前日期
select getdate()

--生成16进制的标志列uniqueidentifier
select newid()

--转换数据类型和格式
select convert(varchar(10),getdate(),120)

抱歉!评论已关闭.