1 T-SQL语句用于管理SQL Server数据库引擎实例,创建和管理数据库对象,以及查询、插入、修改和删除数据。
2 3? 变量
4 51、 局部变量(Local Variable)
6 7 局部变量是用户可以自定义的变量,它的作用范围是仅在程序内部,在程序中通常用来储存从表中查询到的数据或当做程序执行过程中的暂存变量。使用局部变量必须以@开头,而且必须用declare命令后才能使用。
8 9 10 11 基本语法:
12 13 14声明变量
15declare @变量名 变量类型 [@变量名 变量类型]
16为变量赋值
17set @变量名 = 变量值;
18select @变量名 = 变量值;
19 20 21 22 示例:
23 24 25 --局部变量
26 declare @id char(10)--声明一个长度的变量id
27 declare @age int --声明一个int类型变量age
28select @id = 22 --赋值操作
29set @age = 55 --赋值操作
30 print convert(char(10), @age) + ‘#‘ + @id
31select @age, @id
32go
33 34简单hello world示例
35 declare @name varchar(20);
36 declare @result varchar(200);
37set @name = ‘jack‘;
38set @result = @name + ‘ say: hello world!‘;
39select @result;
40 41查询数据示例
42 declare @id int, @name varchar(20);
43set @id = 1;
44select @name = name from student where id = @id;
45select @name;
46 47select赋值
48 declare @name varchar(20);
49select @name = ‘jack‘;
50select * from student where name = @name;
51 52 从上面的示例可以看出,局部变量可用于程序中保存临时数据、传递数据。Set赋值一般用于赋值指定的常量个变量。而select多用于查询的结果进行赋值,当然select也可以将常量赋值给变量。
53 54 注意:在使用select进行赋值的时候,如果查询的结果是多条的情况下,会利用最后一条数据进行赋值,前面的赋值结果将会被覆盖。
55 56 57 582、 全局变量(Global Variable)
59 60 全局变量是系统内部使用的变量,其作用范围并不局限于某一程序而是任何程序均可随时调用的。全局变量一般存储一些系统的配置设定值、统计数据。
61 62 63全局变量
64select @@identity;--最后一次自增的值
65select identity(int, 1, 1) as id into tab from student;--将studeng表的烈属,以/1自增形式创建一个tab
66select * from tab;
67select @@rowcount;--影响行数
68select @@cursor_rows;--返回连接上打开的游标的当前限定行的数目
69select @@error;--T-SQL的错误号
70select @@procid;
71 72 --配置函数
73set datefirst 7;--设置每周的第一天,表示周日
74select @@datefirst as‘星期的第一天‘, datepart(dw, getDate()) AS ‘今天是星期‘;
75select @@dbts;--返回当前数据库唯一时间戳
76set language ‘Italian‘;
77select @@langId as‘Language ID‘;--返回语言id
78select @@language as‘Language Name‘;--返回当前语言名称
79select @@lock_timeout;--返回当前会话的当前锁定超时设置(毫秒)
80select @@max_connections;--返回SQL Server 实例允许同时进行的最大用户连接数
81select @@MAX_PRECISION AS ‘Max Precision‘;--返回decimal 和numeric 数据类型所用的精度级别
82select @@SERVERNAME;--SQL Server 的本地服务器的名称
83select @@SERVICENAME;--服务名
84select @@SPID;--当前会话进程id
85select @@textSize;
86select @@version;--当前数据库版本信息
87 88 --系统统计函数
89select @@CONNECTIONS;--连接数
90select @@PACK_RECEIVED;
91select @@CPU_BUSY;
92select @@PACK_SENT;
93select @@TIMETICKS;
94select @@IDLE;
95select @@TOTAL_ERRORS;
96select @@IO_BUSY;
97select @@TOTAL_READ;--读取磁盘次数
98select @@PACKET_ERRORS;--发生的网络数据包错误数
99select @@TOTAL_WRITE;--sqlserver执行的磁盘写入次数
100101102103? 输出语句
104105 T-SQL支持输出语句,用于显示结果。常用输出语句有两种:
106107 基本语法
108109110print 变量或表达式
111select 变量或表达式
112113114115 示例
116117118select1 + 2;
119select @@language;
120select user_name();
121122 print 1 + 2;
123print @@language;
124print user_name();
125126 print在输出值不少字符串的情况下,需要用convert转换成字符串才能正常输出,而且字符串的长度在超过8000的字符以后,后面的将不会显示。
127128129130? 逻辑控制语句
1311321、 if-else判断语句
133134 语法
135136137if <表达式>
138 <命令行或程序块>
139elseif <表达式>
140 <命令行或程序块>
141else142 <命令行或程序块>
143144 示例
145146147if简单示例
148if2 > 3149 print ‘2 > 3‘;
150else151 print ‘2 < 3‘;
152153if (2 > 3)
154 print ‘2 > 3‘;
155elseif (3 > 2)
156 print ‘3 > 2‘;
157else158 print ‘other‘;
159160简单查询判断
161 declare @id char(10),
162 @pid char(20),
163 @name varchar(20);
164set @name = ‘广州‘;
165select @id = id from ab_area where areaName = @name;
166select @pid = pid from ab_area where id = @id;
167 print @id + ‘#‘ + @pid;
168169if @pid > @id
170 begin
171 print @id + ‘%‘;
172select * from ab_area where pid like @id + ‘%‘;
173 end
174else175 begin
176 print @id + ‘%‘;
177 print @id + ‘#‘ + @pid;
178select * from ab_area where pid = @pid;
179 end
180go
1811821831842、 while…continue…break循环语句
185186 基本语法
187188189while <表达式>
190begin
191 <命令行或程序块>
192 [break]
193 [continue]
194 <命令行或程序块>
195end
196197 示例
198199200 --while循环输出到
201 declare @i int;
202set @i = 1;
203while (@i < 11)
204 begin
205 print @i;
206set @i = @i + 1;
207 end
208go
209210 --whilecontinue 输出到
211 declare @i int;
212set @i = 1;
213while (@i < 11)
214 begin
215if (@i < 5)
216 begin
217set @i = @i + 1;
218continue;
219 end
220 print @i;
221set @i = @i + 1;
222 end
223go
224225 --whilebreak 输出到
226 declare @i int;
227set @i = 1;
228while (1 = 1)
229 begin
230 print @i;
231if (@i >= 5)
232 begin
233set @i = @i + 1;
234break;
235 end
236set @i = @i + 1;
237 end
238go
2392402412423、 case243244 基本语法
245246247case248 when <条件表达式> then <运算式>
249 when <条件表达式> then <运算式>
250 when <条件表达式> then <运算式>
251 [else <运算式>]
252end
253254 示例
255256257select *,
258case sex
259 when 1 then ‘男‘260 when 0 then ‘女‘261else‘火星人‘262 end as‘性别‘263from student;
264265select areaName, ‘区域类型‘ = case266 when areaType = ‘省‘ then areaName + areaType
267 when areaType = ‘市‘ then ‘city‘268 when areaType = ‘区‘ then ‘area‘269else‘other‘270 end
271from ab_area;
2722732742754、 其他语句
276277278批处理语句go
279Use master
280Go
281282延时执行,类似于定时器、休眠等
283 waitfor delay ‘00:00:03‘;--定时三秒后执行
284 print ‘定时三秒后执行‘;
原文:http://www.cnblogs.com/yangpeng-jingjing/p/4710689.html
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!