SQL的基本命令和幾個常用函數(shù)匯總
2024-07-21 02:06:19
供稿:網(wǎng)友
 
--創(chuàng)建對象(表、視圖、存儲過程、函數(shù))命令]
create table/view/procedure/function
--創(chuàng)建表
create table tabtestvb 
(vbname varchar(10),value numeric(10))
go
create table tabtestvb1
(vbname varchar(10),value1 numeric(10))
go
--插入數(shù)據(jù)(兩種方式)
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)
--更改數(shù)據(jù)
update tabtestvb set value=798 where vbname='aaa'
--關聯(lián)更改
update tabtestvb set value=tabtestvb1.value1 
from tabtestvb1 where tabtestvb.vbname=tabtestvb1.vbname
--刪除數(shù)據(jù)
delete tabtestvb where  vbname='aaa'
--無日志刪除數(shù)據(jù)
truncate table tabtestvb
--刪除對象(表、視圖、存儲過程、函數(shù))命令
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 @[email protected][email protected]
  set @[email protected]+1
 end
select @sum
--加上條件:當@nn=20 時退出循環(huán)(計算出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 @[email protected][email protected]
   --end
  else
   --begin
   break
   --end
  set @[email protected]+1
 end
select @sum
--全局變量
@@rowcount
--返回受上一語句影響的行數(shù)
select '1'
union all
select '3'
select @@rowcount
@@error
--返回最后執(zhí)行的 transact-sql 語句的錯誤代碼。
set @n =1
select @@error
----函數(shù)的使用
--返回當前日期
select getdate()
--生成16進制的標志列uniqueidentifier
select newid()
--轉換數(shù)據(jù)類型和格式
select convert(varchar(10),getdate(),120)