資料庫視訊總結(四)——T-sql語句
T-sql語句
分類:
1、 資料定義語言
2、 資料操縱語言
3、資料控制語言
其他常用型別:
1、事務管理語言
2、流程控制語言
3、附加的語言元素
常量與變數:
何如宣告變數
語法格式:
DECLARE
{@名稱 as 型別
|@
給變數賦值可以使用SET或SECECT
SET @變數名=表示式
Select @名稱=表示式
方法一:使用set進行賦值
declare @name nvarchar(50)
set @name=’李明’
方法二:使用select進行賦值
declare @name nvarchar(50)
set @name=’李明’
select @name=’王文華’
select @name
運算子
—
表示式:
declare @str nvarchar(50)
select @str='姓名:'+ 姓名 +'性別:'+ 性別 from 學生資訊 where 學號='123456'
select @str
效果如下:
添加註釋
方法:用雙連字元–和正斜槓註釋/*/
IF ..else語句
語法格式:
If boolean_expression
語句塊
Else
語句塊
舉例:
declare @num int
select @num=avg(成績)
from 成績資訊
where 考試編號='0802' and 課程編號='2'
if @num>40
begin
print'這門課老師教課非常成功!'
end
else
print'這門課老師還需要繼續努力!'
IF…ELSe語句巢狀
舉例:對成績進行分析
declare @num int
select @num=avg(成績)
from 成績資訊
where 考試編號='0802' and 課程編號='2'
if @num>60
begin
if @num<70
print'成績剛剛及格,還要繼續努力'
else if @num <80
print'成績中等'
else if @num <90
print'成績良好'
else
print'成績優秀,希望把經驗與大家分享!'
end
else
print'這個老師教學方法需要改進!'
CASE語句
語法格式:
Case input_表示式
When 情況一 then 執行語句塊
[…n]
Else
End
Case 語句示例
select 姓名,所任職位, 員工職稱=
case 所任職位
when '經理' then '高階職稱'
when '主管' then '中級職稱'
when '職員' then '初級職稱'
else
from 員工資訊
WHILE語句
語法格式:
While 表示式
執行語句
Break
跳出迴圈
Continue
跳出本次迴圈
舉例:求10的階乘
declare @i int,@num int
set @i=1
set @num=1
while @i<=10
begin
set @num=@num*@i
set @i=@i+1
end
print @num
While語句中使用其他語句
舉例:求3到100的素數
declare @i int
declare @j int
set @i=3
while @i<=100
begin
declare @bol int
set @bol=1
set @j=2
while @j<=sqrt(@i)
begin
if @i%@j=0
begin
set @bol=0
break
end
set @j=@j+1
end
if @bol=1 print @i
set @i=@i+1
end
WAITFOR延遲語句
語法格式:
WAITFOR
{
Delay time
|time
}
舉例:延遲5秒鐘
waitfor delay'00:00:05'
exec sp_help
Try…catch錯誤處理語句
語法格式:
BEGIN TRY
{不出錯執行結果
End try
Begin catch
錯誤處理語句
End catch
舉例:
begin try
declare @num int
set @num=1/0
select @num
end try
begin catch
select ERROR_LINE() as '錯誤行數',ERROR_MESSAGE() '錯誤訊息'
end catch
效果圖如下:
數學函式
舉例:
declare @i float,@j int,@a decimal(18,2)
set @i=-12.2324
set @j=100
print abs(round(@i,2))
字串函式:
1、獲取h的ascii編碼
declare @str nvarchar(50)
set @str='hellow world!'
print ascii('h')
2、返回hellow
declare @str nvarchar(50)
set @str='hellow world!'
print left(@str,5)
3、擷取word
declare @str nvarchar(50)
set @str='hellow world!'
print substring(@str,7,11)
字串函式使用示例
CHARINDEX()
返回字串中某個制定的子串出現的開始位置
函式不能用於TEXT和IMAGE資料型別
PATINDEX()
返回字串中某個制定的子串出現的位置,可以使用萬用字元
舉例:
declare @str1 nvarchar(20),
@str2 nvarchar(20),
@str3 nvarchar(20),
@str4 nvarchar(20),
@str5 nvarchar(20)
set @str1='上海分公司經理'
set @str2='北京分公司經理'
set @str3='天津分公司經理'
set @str4='上海分公司財務經理'
set @str5='天津分公司財務經理'
print(charindex('分公司',@str4))
print(right(@str4,len(@str4)-patindex('分公司%經理',@str4)+1))
聚合函式
聚合函式:AVG\COUNT\MAX\MIN\SUM
select avg(成績) as 平均成績,count(成績) as 考試人數,sum(成績) as 總體分數
from 成績資訊
where 考試編號='0801'and 課程編號='2'
效果如下:
日期和時間函式
print year(getdate())
print month(getdate())
print day(getdate())
效果如下:
2018
8
25
獲取年份
print datepart(yy,getdate())
增加一天
print dateadd(dd,1,getdate())
標量值函式
語法格式:
Create 函式名稱
(引數)
Returns +型別
With 建立表值函式是否加密
As
Begin
Return+表示式
End
表值函式
Create function
Returns table
With 語句
As
Return