SQL Server Transact-SQL 编程

T-SQL语句用于管理SQL Server数据库引擎实例,创建和管理数据库对象,以及查询、插入、修改和删除数据。

Ø 变量

     1、 局部变量(Local Variable)

          局部变量是用户可以自定义的变量,它的作用范围是仅在程序内部,在程序中通常用来储存从表中查询到的数据或当做程序执行过程中的暂存变量。使用局部变量必须以@开头,而且必须用declare命令后才能使用。

          基本语法:

1 声明变量
2 declare @变量名 变量类型 [@变量名 变量类型]
3 为变量赋值
4 set @变量名 = 变量值;
5 select @变量名 = 变量值;

         

          示例:

 1 --局部变量
 2 declare @id char(10)--声明一个长度的变量id
 3 declare @age int    --声明一个int类型变量age
 4     select @id = 22    --赋值操作
 5     set @age = 55    --赋值操作
 6     print convert(char(10), @age) + '#' + @id
 7     select @age, @id
 8 go
 9  
10 简单hello world示例
11 declare @name varchar(20);
12 declare @result varchar(200);
13 set @name = 'jack';
14 set @result = @name + ' say: hello world!';
15 select @result;
16  
17 查询数据示例
18 declare @id int, @name varchar(20);
19 set @id = 1;
20 select @name = name from student where id = @id;
21 select @name;
22  
23 select赋值
24 declare @name varchar(20);
25 select @name = 'jack';
26 select * from student where name = @name;
 

          从上面的示例可以看出,局部变量可用于程序中保存临时数据、传递数据。Set赋值一般用于赋值指定的常量个变量。而select多用于查询的结果进行赋值,当然select也可以将常量赋值给变量。

          注意:在使用select进行赋值的时候,如果查询的结果是多条的情况下,会利用最后一条数据进行赋值,前面的赋值结果将会被覆盖。

     2、 全局变量(Global Variable)

          全局变量是系统内部使用的变量,其作用范围并不局限于某一程序而是任何程序均可随时调用的。全局变量一般存储一些系统的配置设定值、统计数据。

 1 全局变量
 2 select @@identity;--最后一次自增的值
 3 select identity(int, 1, 1) as id into tab from student;--将studeng表的烈属,以/1自增形式创建一个tab
 4 select * from tab;
 5 select @@rowcount;--影响行数
 6 select @@cursor_rows;--返回连接上打开的游标的当前限定行的数目
 7 select @@error;--T-SQL的错误号
 8 select @@procid;
 9  
10 --配置函数
11 set datefirst 7;--设置每周的第一天,表示周日
12 select @@datefirst as '星期的第一天', datepart(dw, getDate()) AS '今天是星期';
13 select @@dbts;--返回当前数据库唯一时间戳
14 set language 'Italian';
15 select @@langId as 'Language ID';--返回语言id
16 select @@language as 'Language Name';--返回当前语言名称
17 select @@lock_timeout;--返回当前会话的当前锁定超时设置(毫秒)
18 select @@max_connections;--返回SQL Server 实例允许同时进行的最大用户连接数
19 select @@MAX_PRECISION AS 'Max Precision';--返回decimal 和numeric 数据类型所用的精度级别
20 select @@SERVERNAME;--SQL Server 的本地服务器的名称
21 select @@SERVICENAME;--服务名
22 select @@SPID;--当前会话进程id
23 select @@textSize;
24 select @@version;--当前数据库版本信息
25  
26 --系统统计函数
27 select @@CONNECTIONS;--连接数
28 select @@PACK_RECEIVED;
29 select @@CPU_BUSY;
30 select @@PACK_SENT;
31 select @@TIMETICKS;
32 select @@IDLE;
33 select @@TOTAL_ERRORS;
34 select @@IO_BUSY;
35 select @@TOTAL_READ;--读取磁盘次数
36 select @@PACKET_ERRORS;--发生的网络数据包错误数
37 select @@TOTAL_WRITE;--sqlserver执行的磁盘写入次数

Ø 输出语句

     T-SQL支持输出语句,用于显示结果。常用输出语句有两种:

     基本语法

1 print 变量或表达式

2 select 变量或表达式 

    

      示例

1 select 1 + 2;
2 select @@language;
3 select user_name();
4  
5 print 1 + 2;
6 print @@language;
7 print user_name();

     print在输出值不少字符串的情况下,需要用convert转换成字符串才能正常输出,而且字符串的长度在超过8000的字符以后,后面的将不会显示。

Ø 逻辑控制语句

     1、 if-else判断语句

          语法

 1 if <表达式>
 2     <命令行或程序块>
 3 else if <表达式>
 4     <命令行或程序块>
 5 else
 6     <命令行或程序块>
 7           示例
 8 
 9 if简单示例
10 if 2 > 3
11     print '2 > 3';
12 else
13     print '2 < 3';
14  
15 if (2 > 3)
16     print '2 > 3';
17 else if (3 > 2)
18     print '3 > 2';
19 else
20     print 'other';
21  
22 简单查询判断
23 declare @id char(10),
24         @pid char(20),
25         @name varchar(20);
26 set @name = '广州';
27 select @id = id from ab_area where areaName = @name;
28 select @pid = pid from ab_area where id = @id;
29 print @id + '#' + @pid;
30  
31 if @pid > @id
32     begin
33         print @id + '%';
34         select * from ab_area where pid like @id + '%';
35     end
36 else
37     begin
38         print @id + '%';
39         print @id + '#' + @pid;
40         select * from ab_area where pid = @pid;
41     end
42 go

    

       2、 while…continue…break循环语句

          基本语法

1 while <表达式>
2 begin
3    <命令行或程序块>
4    [break]
5    [continue]
6    <命令行或程序块>
7 end

          示例

 1 --while循环输出到
 2 declare @i int;
 3     set @i = 1;
 4 while (@i < 11)
 5     begin
 6         print @i;
 7         set @i = @i + 1;
 8     end
 9 go
10  
11 --while continue 输出到
12 declare @i int;
13     set @i = 1;
14 while (@i < 11)
15     begin                
16         if (@i < 5)
17             begin
18                 set @i = @i + 1;
19                 continue;        
20             end
21         print @i;
22         set @i = @i + 1;                
23     end
24 go
25  
26 --while break 输出到
27 declare @i int;
28     set @i = 1;
29 while (1 = 1)
30     begin        
31         print @i;        
32         if (@i >= 5)
33             begin
34                 set @i = @i + 1;
35                 break;        
36             end        
37         set @i = @i + 1;                
38     end
39 go

    

     3、 case

          基本语法

1 case
2    when <条件表达式> then <运算式>
3    when <条件表达式> then <运算式>
4    when <条件表达式> then <运算式>
5    [else <运算式>]
6 end

          示例

 1 select *,
 2     case sex 
 3         when 1 then ''
 4         when 0 then ''    
 5         else '火星人'
 6     end as '性别'
 7 from student;
 8  
 9 select areaName, '区域类型' = case
10         when areaType = '' then areaName + areaType
11         when areaType = '' then 'city'
12         when areaType = '' then 'area'
13         else 'other'
14     end
15 from ab_area;

    

       4、 其他语句

批处理语句go
1 Use master
2 Go
 

延时执行,类似于定时器、休眠等
1 waitfor delay '00:00:03';--定时三秒后执行
2 print '定时三秒后执行';

 FROM:http://www.cnblogs.com/hoojo/archive/2011/07/15/2107740.html

原文地址:https://www.cnblogs.com/kevin-kw/p/6295993.html