1 T-SQL语句用于管理SQL Server数据库引擎实例,创建和管理数据库对象,以及查询、插入、修改和删除数据。
2
3 Ø 变量
4
5 1、 局部变量(Local Variable)
6
7 局部变量是用户可以自定义的变量,它的作用范围是仅在程序内部,在程序中通常用来储存从表中查询到的数据或当做程序执行过程中的暂存变量。使用局部变量必须以@开头,而且必须用declare命令后才能使用。
8
9
10
11 基本语法:
12
13
14 声明变量
15 declare @变量名 变量类型 [@变量名 变量类型]
16 为变量赋值
17 set @变量名 = 变量值;
18 select @变量名 = 变量值;
19
20
21
22 示例:
23
24
25 --局部变量
26 declare @id char(10)--声明一个长度的变量id
27 declare @age int --声明一个int类型变量age
28 select @id = 22 --赋值操作
29 set @age = 55 --赋值操作
30 print convert(char(10), @age) + '#' + @id
31 select @age, @id
32 go
33
34 简单hello world示例
35 declare @name varchar(20);
36 declare @result varchar(200);
37 set @name = 'jack';
38 set @result = @name + ' say: hello world!';
39 select @result;
40
41 查询数据示例
42 declare @id int, @name varchar(20);
43 set @id = 1;
44 select @name = name from student where id = @id;
45 select @name;
46
47 select赋值
48 declare @name varchar(20);
49 select @name = 'jack';
50 select * from student where name = @name;
51
52 从上面的示例可以看出,局部变量可用于程序中保存临时数据、传递数据。Set赋值一般用于赋值指定的常量个变量。而select多用于查询的结果进行赋值,当然select也可以将常量赋值给变量。
53
54 注意:在使用select进行赋值的时候,如果查询的结果是多条的情况下,会利用最后一条数据进行赋值,前面的赋值结果将会被覆盖。
55
56
57
58 2、 全局变量(Global Variable)
59
60 全局变量是系统内部使用的变量,其作用范围并不局限于某一程序而是任何程序均可随时调用的。全局变量一般存储一些系统的配置设定值、统计数据。
61
62
63 全局变量
64 select @@identity;--最后一次自增的值
65 select identity(int, 1, 1) as id into tab from student;--将studeng表的烈属,以/1自增形式创建一个tab
66 select * from tab;
67 select @@rowcount;--影响行数
68 select @@cursor_rows;--返回连接上打开的游标的当前限定行的数目
69 select @@error;--T-SQL的错误号
70 select @@procid;
71
72 --配置函数
73 set datefirst 7;--设置每周的第一天,表示周日
74 select @@datefirst as '星期的第一天', datepart(dw, getDate()) AS '今天是星期';
75 select @@dbts;--返回当前数据库唯一时间戳
76 set language 'Italian';
77 select @@langId as 'Language ID';--返回语言id
78 select @@language as 'Language Name';--返回当前语言名称
79 select @@lock_timeout;--返回当前会话的当前锁定超时设置(毫秒)
80 select @@max_connections;--返回SQL Server 实例允许同时进行的最大用户连接数
81 select @@MAX_PRECISION AS 'Max Precision';--返回decimal 和numeric 数据类型所用的精度级别
82 select @@SERVERNAME;--SQL Server 的本地服务器的名称
83 select @@SERVICENAME;--服务名
84 select @@SPID;--当前会话进程id
85 select @@textSize;
86 select @@version;--当前数据库版本信息
87
88 --系统统计函数
89 select @@CONNECTIONS;--连接数
90 select @@PACK_RECEIVED;
91 select @@CPU_BUSY;
92 select @@PACK_SENT;
93 select @@TIMETICKS;
94 select @@IDLE;
95 select @@TOTAL_ERRORS;
96 select @@IO_BUSY;
97 select @@TOTAL_READ;--读取磁盘次数
98 select @@PACKET_ERRORS;--发生的网络数据包错误数
99 select @@TOTAL_WRITE;--sqlserver执行的磁盘写入次数
100
101
102
103 Ø 输出语句
104
105 T-SQL支持输出语句,用于显示结果。常用输出语句有两种:
106
107 基本语法
108
109
110 print 变量或表达式
111 select 变量或表达式
112
113
114
115 示例
116
117
118 select 1 + 2;
119 select @@language;
120 select user_name();
121
122 print 1 + 2;
123 print @@language;
124 print user_name();
125
126 print在输出值不少字符串的情况下,需要用convert转换成字符串才能正常输出,而且字符串的长度在超过8000的字符以后,后面的将不会显示。
127
128
129
130 Ø 逻辑控制语句
131
132 1、 if-else判断语句
133
134 语法
135
136
137 if <表达式>
138 <命令行或程序块>
139 else if <表达式>
140 <命令行或程序块>
141 else
142 <命令行或程序块>
143
144 示例
145
146
147 if简单示例
148 if 2 > 3
149 print '2 > 3';
150 else
151 print '2 < 3';
152
153 if (2 > 3)
154 print '2 > 3';
155 else if (3 > 2)
156 print '3 > 2';
157 else
158 print 'other';
159
160 简单查询判断
161 declare @id char(10),
162 @pid char(20),
163 @name varchar(20);
164 set @name = '广州';
165 select @id = id from ab_area where areaName = @name;
166 select @pid = pid from ab_area where id = @id;
167 print @id + '#' + @pid;
168
169 if @pid > @id
170 begin
171 print @id + '%';
172 select * from ab_area where pid like @id + '%';
173 end
174 else
175 begin
176 print @id + '%';
177 print @id + '#' + @pid;
178 select * from ab_area where pid = @pid;
179 end
180 go
181
182
183
184 2、 while…continue…break循环语句
185
186 基本语法
187
188
189 while <表达式>
190 begin
191 <命令行或程序块>
192 [break]
193 [continue]
194 <命令行或程序块>
195 end
196
197 示例
198
199
200 --while循环输出到
201 declare @i int;
202 set @i = 1;
203 while (@i < 11)
204 begin
205 print @i;
206 set @i = @i + 1;
207 end
208 go
209
210 --while continue 输出到
211 declare @i int;
212 set @i = 1;
213 while (@i < 11)
214 begin
215 if (@i < 5)
216 begin
217 set @i = @i + 1;
218 continue;
219 end
220 print @i;
221 set @i = @i + 1;
222 end
223 go
224
225 --while break 输出到
226 declare @i int;
227 set @i = 1;
228 while (1 = 1)
229 begin
230 print @i;
231 if (@i >= 5)
232 begin
233 set @i = @i + 1;
234 break;
235 end
236 set @i = @i + 1;
237 end
238 go
239
240
241
242 3、 case
243
244 基本语法
245
246
247 case
248 when <条件表达式> then <运算式>
249 when <条件表达式> then <运算式>
250 when <条件表达式> then <运算式>
251 [else <运算式>]
252 end
253
254 示例
255
256
257 select *,
258 case sex
259 when 1 then '男'
260 when 0 then '女'
261 else '火星人'
262 end as '性别'
263 from student;
264
265 select areaName, '区域类型' = case
266 when areaType = '省' then areaName + areaType
267 when areaType = '市' then 'city'
268 when areaType = '区' then 'area'
269 else 'other'
270 end
271 from ab_area;
272
273
274
275 4、 其他语句
276
277
278 批处理语句go
279 Use master
280 Go
281
282 延时执行,类似于定时器、休眠等
283 waitfor delay '00:00:03';--定时三秒后执行
284 print '定时三秒后执行';