SQL Server Transact-SQL 编程

转自:http://blog.csdn.net/ibm_hoojo/article/details/6608704

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.     <命令行或程序块>  

               示例

  1. if简单示例  
  2. if 2 > 3  
  3.     print '2 > 3';  
  4. else  
  5.     print '2 < 3';  
  6.   
  7. if (2 > 3)  
  8.     print '2 > 3';  
  9. else if (3 > 2)  
  10.     print '3 > 2';  
  11. else  
  12.     print 'other';  
  13.   
  14. 简单查询判断  
  15. declare @id char(10),  
  16.         @pid char(20),  
  17.         @name varchar(20);  
  18. set @name = '广州';  
  19. select @id = id from ab_area where areaName = @name;  
  20. select @pid = pid from ab_area where id = @id;  
  21. print @id + '#' + @pid;  
  22.   
  23. if @pid > @id  
  24.     begin  
  25.         print @id + '%';  
  26.         select * from ab_area where pid like @id + '%';  
  27.     end  
  28. else  
  29.     begin  
  30.         print @id + '%';  
  31.         print @id + '#' + @pid;  
  32.         select * from ab_area where pid = @pid;  
  33.     end  
  34. 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、 其他语句

  1. 批处理语句go  
  2. Use master  
  3. Go  
  4.   
  5. 延时执行,类似于定时器、休眠等  
  6. waitfor delay '00:00:03';--定时三秒后执行  
  7. print '定时三秒后执行';  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值