SQL Server Transact-SQL編程詳解
T-SQL語句用于管理SQL Server數(shù)據(jù)庫引擎實(shí)例,創(chuàng)建和管理數(shù)據(jù)庫對(duì)象,以及查詢、插入、修改和刪除數(shù)據(jù)。
變量
1、 局部變量(Local Variable)
局部變量是用戶可以自定義的變量,它的作用范圍是僅在程序內(nèi)部,在程序中通常用來儲(chǔ)存從表中查詢到的數(shù)據(jù)或當(dāng)做程序執(zhí)行過程中的暫存變量。使用局部變量必須以@開頭,而且必須用declare命令后才能使用。
基本語法:
聲明變量 declare @變量名 變量類型 [@變量名 變量類型] 為變量賦值 set @變量名 = 變量值; select @變量名 = 變量值;
示例:
--局部變量
declare @id char(10)--聲明一個(gè)長度的變量id
declare @age int --聲明一個(gè)int類型變量age
select @id = 22 --賦值操作
set @age = 55 --賦值操作
print convert(char(10), @age) + '#' + @id
select @age, @id
go
簡單hello world示例
declare @name varchar(20);
declare @result varchar(200);
set @name = 'jack';
set @result = @name + ' say: hello world!';
select @result;
查詢數(shù)據(jù)示例
declare @id int, @name varchar(20);
set @id = 1;
select @name = name from student where id = @id;
select @name;
select賦值
declare @name varchar(20);
select @name = 'jack';
select * from student where name = @name;從上面的示例可以看出,局部變量可用于程序中保存臨時(shí)數(shù)據(jù)、傳遞數(shù)據(jù)。Set賦值一般用于賦值指定的常量個(gè)變量。而select多用于查詢的結(jié)果進(jìn)行賦值,當(dāng)然select也可以將常量賦值給變量。
注意:在使用select進(jìn)行賦值的時(shí)候,如果查詢的結(jié)果是多條的情況下,會(huì)利用最后一條數(shù)據(jù)進(jìn)行賦值,前面的賦值結(jié)果將會(huì)被覆蓋。
2、 全局變量(Global Variable)
全局變量是系統(tǒng)內(nèi)部使用的變量,其作用范圍并不局限于某一程序而是任何程序均可隨時(shí)調(diào)用的。全局變量一般存儲(chǔ)一些系統(tǒng)的配置設(shè)定值、統(tǒng)計(jì)數(shù)據(jù)。
--局部變量
declare @id char(10)--聲明一個(gè)長度的變量id
declare @age int --聲明一個(gè)int類型變量age
select @id = 22 --賦值操作
set @age = 55 --賦值操作
print convert(char(10), @age) + '#' + @id
select @age, @id
go
簡單hello world示例
declare @name varchar(20);
declare @result varchar(200);
set @name = 'jack';
set @result = @name + ' say: hello world!';
select @result;
查詢數(shù)據(jù)示例
declare @id int, @name varchar(20);
set @id = 1;
select @name = name from student where id = @id;
select @name;
select賦值
declare @name varchar(20);
select @name = 'jack';
select * from student where name = @name;輸出語句
T-SQL支持輸出語句,用于顯示結(jié)果。常用輸出語句有兩種:
基本語法
print 變量或表達(dá)式 select 變量或表達(dá)式
示例
select 1 + 2; select @@language; select user_name(); print 1 + 2; print @@language; print user_name();
print在輸出值不少字符串的情況下,需要用convert轉(zhuǎn)換成字符串才能正常輸出,而且字符串的長度在超過8000的字符以后,后面的將不會(huì)顯示。
邏輯控制語句
1、 if-else判斷語句
語法
if <表達(dá)式>
<命令行或程序塊>
else if <表達(dá)式>
<命令行或程序塊>
else
<命令行或程序塊>示例
if簡單示例
if 2 > 3
print '2 > 3';
else
print '2 < 3';
if (2 > 3)
print '2 > 3';
else if (3 > 2)
print '3 > 2';
else
print 'other';
簡單查詢判斷
declare @id char(10),
@pid char(20),
@name varchar(20);
set @name = '廣州';
select @id = id from ab_area where areaName = @name;
select @pid = pid from ab_area where id = @id;
print @id + '#' + @pid;
if @pid > @id
begin
print @id + '%';
select * from ab_area where pid like @id + '%';
end
else
begin
print @id + '%';
print @id + '#' + @pid;
select * from ab_area where pid = @pid;
end
go2、 while…continue…break循環(huán)語句
基本語法
while <表達(dá)式> begin <命令行或程序塊> [break] [continue] <命令行或程序塊> end
示例
--while循環(huán)輸出到
declare @i int;
set @i = 1;
while (@i < 11)
begin
print @i;
set @i = @i + 1;
end
go
--while continue 輸出到
declare @i int;
set @i = 1;
while (@i < 11)
begin
if (@i < 5)
begin
set @i = @i + 1;
continue;
end
print @i;
set @i = @i + 1;
end
go
--while break 輸出到
declare @i int;
set @i = 1;
while (1 = 1)
begin
print @i;
if (@i >= 5)
begin
set @i = @i + 1;
break;
end
set @i = @i + 1;
end
go3、 case
基本語法
case when <條件表達(dá)式> then <運(yùn)算式> when <條件表達(dá)式> then <運(yùn)算式> when <條件表達(dá)式> then <運(yùn)算式> [else <運(yùn)算式>] end
示例
select *,
case sex
when 1 then '男'
when 0 then '女'
else '火星人'
end as '性別'
from student;
select areaName, '區(qū)域類型' = case
when areaType = '省' then areaName + areaType
when areaType = '市' then 'city'
when areaType = '區(qū)' then 'area'
else 'other'
end
from ab_area;4、 其他語句
批處理語句go Use master Go 延時(shí)執(zhí)行,類似于定時(shí)器、休眠等 waitfor delay '00:00:03';--定時(shí)三秒后執(zhí)行 print '定時(shí)三秒后執(zhí)行';
總結(jié)
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
Sql?Server高版本數(shù)據(jù)庫數(shù)據(jù)備份后還原到低版本數(shù)據(jù)庫詳細(xì)步驟
不同版本SQL?Server數(shù)據(jù)庫備份還原存在問題,不能從高版本的數(shù)據(jù)庫導(dǎo)入到低版本數(shù)據(jù)中,這篇文章主要給大家介紹了關(guān)于Sql?Server高版本數(shù)據(jù)庫數(shù)據(jù)備份后還原到低版本數(shù)據(jù)庫的詳細(xì)步驟,需要的朋友可以參考下2023-10-10
sql 查詢記錄數(shù)結(jié)果集某個(gè)區(qū)間內(nèi)記錄
sqlserver如何實(shí)現(xiàn)查詢記錄數(shù)某個(gè)區(qū)間內(nèi)記錄,本文將提供多種解決方法,需要了解的朋友可以參考下2012-11-11
sqlserver登陸后報(bào)不能為空不能為null的錯(cuò)誤
sqlserver登陸后報(bào)不能為空的錯(cuò)誤,參數(shù)名:viewinfo (microsoft.sqlserver.managemenmen),這個(gè)情況的解決方法如下2014-07-07
在SQL中使用convert函數(shù)進(jìn)行日期的查詢的代碼
在SQL中使用convert函數(shù)進(jìn)行日期的查詢的代碼...2007-08-08

