1、T_SQL語(yǔ)言屬于SQL語(yǔ)言中的一種:
SQL結(jié)構(gòu)化查詢語(yǔ)言
所有的數(shù)據(jù)庫(kù)編程語(yǔ)言均對(duì)ANSI SQL向下兼容,如MS SQL Server的SQL語(yǔ)言、Oracle的PL/SQL語(yǔ)言
2、SQL語(yǔ)言主要包括三類:
1)DCL——數(shù)據(jù)控制語(yǔ)言:主要用于控制權(quán)限
Grant:賦權(quán)
Deny:拒絕
Revoke:恢復(fù)初始默認(rèn)
2)DDL——數(shù)據(jù)定義語(yǔ)言:主要用于定義數(shù)據(jù)庫(kù)對(duì)象
Create:創(chuàng)建數(shù)據(jù)庫(kù)對(duì)象
Alter:修改數(shù)據(jù)庫(kù)對(duì)象的定義
Drop:刪除數(shù)據(jù)庫(kù)對(duì)象
3)DML——數(shù)據(jù)操縱語(yǔ)言:主要用于操縱數(shù)據(jù)
Insert:添加數(shù)據(jù)
Update:修改數(shù)據(jù)
Delete:刪除數(shù)據(jù)
Select:查詢數(shù)據(jù)(有一些資料中將Select單獨(dú)分類為DQL數(shù)據(jù)查詢語(yǔ)言)
3、變量
在SQL Server中定義變量,變量名前加@(局部變量)或@@(全局變量)
使用Declare聲明變量,使用Set或Select語(yǔ)句為變量賦值。如:
1declare @i int
2set @i=100
3
4declare @sum int
5select @sum=sum(UnitPrice)
6from Products
7
8declare @price int
9select @price=UnitPrice
10from Products
11where ProductID=1
12
13declare @singlePrice int
14select @singlePrice=UnitPrice
15from Products
16
17declare @sumPrice int
18set @sumPrice=0
19select @wumPrice=@sumPrice+UnitPrice
20from Products
4、函數(shù)
在SQL Server中函數(shù)可以分為以下三類:
1)標(biāo)量函數(shù):確定的參數(shù),一個(gè)返回值,如常規(guī)的函數(shù)均屬于標(biāo)量函數(shù)。
declare @value int
set @value=100
declare @valueString varchar(10)
set @valueString=Convert(varchar(10),@value)
print('Value is '+@valueString)
2)聚焦函數(shù):參數(shù)為一個(gè)集合(表中的列),返回為一個(gè)值,如數(shù)學(xué)上的統(tǒng)計(jì)函數(shù)均屬于標(biāo)量函數(shù)。
Select sum(UnitPrice) as [SUM] --取所有單價(jià)的和
from Products
Select avg(UnitPrice) as [AVG] --取所有單價(jià)的平均值
from Products
Select max(UnitPrice) as [Max] --取所有單價(jià)的值
from Products
Select min(UnitPrice) as [Min] --取所有單價(jià)的最小值
from Products
Select count(Region) as [Count] --取所有Region不為空的行數(shù)
from Employees
Select count(*) as [Count] --取員工表所有的行數(shù)
from Employees
3)行集函數(shù):參數(shù)為確定的參數(shù),返回為一個(gè)“結(jié)果集”。
select * from
OpenQuery(
OracleSvr, --打開一個(gè)鏈接服務(wù)器
'SELECT ENAME, EMPNO FROM SCOTT.EMP' --在鏈接服務(wù)器上執(zhí)行查詢語(yǔ)句
) --將OpenQuery返回的結(jié)果集作為查詢的源
5、語(yǔ)句
1)Begin...End:相當(dāng)于C、Java、C#中的一對(duì)大括號(hào),表示范圍限定,沒有具體含義,如果其中只有一條語(yǔ)句則可以省略。
2)While:循環(huán)語(yǔ)句
--計(jì)算1+2+3++100
declare @i int
declare @sum int
set @i=1
set @sum=0
while @i<=100
begin
set @sum=@sum+@i
set @i=@i+1
end
Print(@sum)
3)If...Else:條件語(yǔ)句
declare @rowCount int
select @rowCount=count(*) from SomeTable
if @rowCount=0
begin
Print('沒有數(shù)據(jù)')
end
else if @rowCount>0 and @rowCount<100
begin
Print('100條以內(nèi)記錄')
end
else
begin
Print('100條以上記錄')
end
4)Case語(yǔ)句:屬于行級(jí)語(yǔ)句(前三種屬于語(yǔ)句級(jí)),相當(dāng)于一個(gè)函數(shù)的作用
Select ProductID,ProductName,UnitPrice,
Level=
case
when UnitPrice<=30 then 'Low Price'
when UnitPrice>30 and UnitPrice<=90 then 'Mid Price'
else 'High Price'
end
from Products
SQL結(jié)構(gòu)化查詢語(yǔ)言
所有的數(shù)據(jù)庫(kù)編程語(yǔ)言均對(duì)ANSI SQL向下兼容,如MS SQL Server的SQL語(yǔ)言、Oracle的PL/SQL語(yǔ)言
2、SQL語(yǔ)言主要包括三類:
1)DCL——數(shù)據(jù)控制語(yǔ)言:主要用于控制權(quán)限
Grant:賦權(quán)
Deny:拒絕
Revoke:恢復(fù)初始默認(rèn)
2)DDL——數(shù)據(jù)定義語(yǔ)言:主要用于定義數(shù)據(jù)庫(kù)對(duì)象
Create:創(chuàng)建數(shù)據(jù)庫(kù)對(duì)象
Alter:修改數(shù)據(jù)庫(kù)對(duì)象的定義
Drop:刪除數(shù)據(jù)庫(kù)對(duì)象
3)DML——數(shù)據(jù)操縱語(yǔ)言:主要用于操縱數(shù)據(jù)
Insert:添加數(shù)據(jù)
Update:修改數(shù)據(jù)
Delete:刪除數(shù)據(jù)
Select:查詢數(shù)據(jù)(有一些資料中將Select單獨(dú)分類為DQL數(shù)據(jù)查詢語(yǔ)言)
3、變量
在SQL Server中定義變量,變量名前加@(局部變量)或@@(全局變量)
使用Declare聲明變量,使用Set或Select語(yǔ)句為變量賦值。如:
1declare @i int
2set @i=100
3
4declare @sum int
5select @sum=sum(UnitPrice)
6from Products
7
8declare @price int
9select @price=UnitPrice
10from Products
11where ProductID=1
12
13declare @singlePrice int
14select @singlePrice=UnitPrice
15from Products
16
17declare @sumPrice int
18set @sumPrice=0
19select @wumPrice=@sumPrice+UnitPrice
20from Products
4、函數(shù)
在SQL Server中函數(shù)可以分為以下三類:
1)標(biāo)量函數(shù):確定的參數(shù),一個(gè)返回值,如常規(guī)的函數(shù)均屬于標(biāo)量函數(shù)。
declare @value int
set @value=100
declare @valueString varchar(10)
set @valueString=Convert(varchar(10),@value)
print('Value is '+@valueString)
2)聚焦函數(shù):參數(shù)為一個(gè)集合(表中的列),返回為一個(gè)值,如數(shù)學(xué)上的統(tǒng)計(jì)函數(shù)均屬于標(biāo)量函數(shù)。
Select sum(UnitPrice) as [SUM] --取所有單價(jià)的和
from Products
Select avg(UnitPrice) as [AVG] --取所有單價(jià)的平均值
from Products
Select max(UnitPrice) as [Max] --取所有單價(jià)的值
from Products
Select min(UnitPrice) as [Min] --取所有單價(jià)的最小值
from Products
Select count(Region) as [Count] --取所有Region不為空的行數(shù)
from Employees
Select count(*) as [Count] --取員工表所有的行數(shù)
from Employees
3)行集函數(shù):參數(shù)為確定的參數(shù),返回為一個(gè)“結(jié)果集”。
select * from
OpenQuery(
OracleSvr, --打開一個(gè)鏈接服務(wù)器
'SELECT ENAME, EMPNO FROM SCOTT.EMP' --在鏈接服務(wù)器上執(zhí)行查詢語(yǔ)句
) --將OpenQuery返回的結(jié)果集作為查詢的源
5、語(yǔ)句
1)Begin...End:相當(dāng)于C、Java、C#中的一對(duì)大括號(hào),表示范圍限定,沒有具體含義,如果其中只有一條語(yǔ)句則可以省略。
2)While:循環(huán)語(yǔ)句
--計(jì)算1+2+3++100
declare @i int
declare @sum int
set @i=1
set @sum=0
while @i<=100
begin
set @sum=@sum+@i
set @i=@i+1
end
Print(@sum)
3)If...Else:條件語(yǔ)句
declare @rowCount int
select @rowCount=count(*) from SomeTable
if @rowCount=0
begin
Print('沒有數(shù)據(jù)')
end
else if @rowCount>0 and @rowCount<100
begin
Print('100條以內(nèi)記錄')
end
else
begin
Print('100條以上記錄')
end
4)Case語(yǔ)句:屬于行級(jí)語(yǔ)句(前三種屬于語(yǔ)句級(jí)),相當(dāng)于一個(gè)函數(shù)的作用
Select ProductID,ProductName,UnitPrice,
Level=
case
when UnitPrice<=30 then 'Low Price'
when UnitPrice>30 and UnitPrice<=90 then 'Mid Price'
else 'High Price'
end
from Products

