網站首頁 編程語言 正文
T-SQL語句用于管理SQL Server數據庫引擎實例,創建和管理數據庫對象,以及查詢、插入、修改和刪除數據。
變量
1、 局部變量(Local Variable)
局部變量是用戶可以自定義的變量,它的作用范圍是僅在程序內部,在程序中通常用來儲存從表中查詢到的數據或當做程序執行過程中的暫存變量。使用局部變量必須以@開頭,而且必須用declare命令后才能使用。
基本語法:
聲明變量 declare @變量名 變量類型 [@變量名 變量類型] 為變量賦值 set @變量名 = 變量值; select @變量名 = 變量值;
示例:
--局部變量 declare @id char(10)--聲明一個長度的變量id declare @age int --聲明一個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; 查詢數據示例 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;
從上面的示例可以看出,局部變量可用于程序中保存臨時數據、傳遞數據。Set賦值一般用于賦值指定的常量個變量。而select多用于查詢的結果進行賦值,當然select也可以將常量賦值給變量。
注意:在使用select進行賦值的時候,如果查詢的結果是多條的情況下,會利用最后一條數據進行賦值,前面的賦值結果將會被覆蓋。
2、 全局變量(Global Variable)
全局變量是系統內部使用的變量,其作用范圍并不局限于某一程序而是任何程序均可隨時調用的。全局變量一般存儲一些系統的配置設定值、統計數據。
--局部變量 declare @id char(10)--聲明一個長度的變量id declare @age int --聲明一個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; 查詢數據示例 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支持輸出語句,用于顯示結果。常用輸出語句有兩種:
基本語法
print 變量或表達式 select 變量或表達式
示例
select 1 + 2; select @@language; select user_name(); print 1 + 2; print @@language; print user_name();
print在輸出值不少字符串的情況下,需要用convert轉換成字符串才能正常輸出,而且字符串的長度在超過8000的字符以后,后面的將不會顯示。
邏輯控制語句
1、 if-else判斷語句
語法
if <表達式> <命令行或程序塊> else if <表達式> <命令行或程序塊> 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 go
2、 while…continue…break循環語句
基本語法
while <表達式> begin <命令行或程序塊> [break] [continue] <命令行或程序塊> end
示例
--while循環輸出到 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 go
3、 case
基本語法
case when <條件表達式> then <運算式> when <條件表達式> then <運算式> when <條件表達式> then <運算式> [else <運算式>] end
示例
select *, case sex when 1 then '男' when 0 then '女' else '火星人' end as '性別' from student; select areaName, '區域類型' = case when areaType = '省' then areaName + areaType when areaType = '市' then 'city' when areaType = '區' then 'area' else 'other' end from ab_area;
4、 其他語句
批處理語句go Use master Go 延時執行,類似于定時器、休眠等 waitfor delay '00:00:03';--定時三秒后執行 print '定時三秒后執行';
總結
原文鏈接:https://www.cnblogs.com/hoojo/archive/2011/07/15/2107740.html
相關推薦
- 2022-06-18 Redis實現單設備登錄的場景分析_Redis
- 2022-09-17 詳解python中靜態方法staticmethod用法_python
- 2022-08-01 OpenCV根據面積篩選連通域學習示例_python
- 2022-09-23 Redux中異步action與同步action的使用_React
- 2022-05-27 python中torch.nn.identity()方法詳解_python
- 2022-06-25 PyTorch模型保存與加載實例詳解_python
- 2022-09-09 使用PyTorch實現隨機搜索策略_python
- 2023-06-17 解讀C語言非void函數卻沒有return會怎么樣_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支