51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

查看: 1476|回复: 0

SQL Server Transact-SQL 编程

[复制链接]

该用户从未签到

发表于 2018-4-12 16:19:54 | 显示全部楼层 |阅读模式
  1. T-SQL语句用于管理SQL Server数据库引擎实例,创建和管理数据库对象,以及查询、插入、修改和删除数据。

  2. Ø 变量

  3.      1、 局部变量(Local Variable)

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



  5.           基本语法:

  6. 声明变量
  7. declare @变量名 变量类型 [@变量名 变量类型]
  8. 为变量赋值
  9. set @变量名 = 变量值;
  10. select @变量名 = 变量值;
  11.          

  12.           示例:

  13. --局部变量
  14. declare @id char(10)--声明一个长度的变量id
  15. declare @age int    --声明一个int类型变量age
  16.     select @id = 22    --赋值操作
  17.     set @age = 55    --赋值操作
  18.     print convert(char(10), @age) + '#' + @id
  19.     select @age, @id
  20. go

  21. 简单hello world示例
  22. declare @name varchar(20);
  23. declare @result varchar(200);
  24. set @name = 'jack';
  25. set @result = @name + ' say: hello world!';
  26. select @result;

  27. 查询数据示例
  28. declare @id int, @name varchar(20);
  29. set @id = 1;
  30. select @name = name from student where id = @id;
  31. select @name;

  32. select赋值
  33. declare @name varchar(20);
  34. select @name = 'jack';
  35. select * from student where name = @name;
  36.           从上面的示例可以看出,局部变量可用于程序中保存临时数据、传递数据。Set赋值一般用于赋值指定的常量个变量。而select多用于查询的结果进行赋值,当然select也可以将常量赋值给变量。

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



  38.      2、 全局变量(Global Variable)

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

  40. 全局变量
  41. select @@identity;--最后一次自增的值
  42. select identity(int, 1, 1) as id into tab from student;--将studeng表的烈属,以/1自增形式创建一个tab
  43. select * from tab;
  44. select @@rowcount;--影响行数
  45. select @@cursor_rows;--返回连接上打开的游标的当前限定行的数目
  46. select @@error;--T-SQL的错误号
  47. select @@procid;

  48. --配置函数
  49. set datefirst 7;--设置每周的第一天,表示周日
  50. select @@datefirst as '星期的第一天', datepart(dw, getDate()) AS '今天是星期';
  51. select @@dbts;--返回当前数据库唯一时间戳
  52. set language 'Italian';
  53. select @@langId as 'Language ID';--返回语言id
  54. select @@language as 'Language Name';--返回当前语言名称
  55. select @@lock_timeout;--返回当前会话的当前锁定超时设置(毫秒)
  56. select @@max_connections;--返回SQL Server 实例允许同时进行的最大用户连接数
  57. select @@MAX_PRECISION AS 'Max Precision';--返回decimal 和numeric 数据类型所用的精度级别
  58. select @@SERVERNAME;--SQL Server 的本地服务器的名称
  59. select @@SERVICENAME;--服务名
  60. select @@SPID;--当前会话进程id
  61. select @@textSize;
  62. select @@version;--当前数据库版本信息

  63. --系统统计函数
  64. select @@CONNECTIONS;--连接数
  65. select @@PACK_RECEIVED;
  66. select @@CPU_BUSY;
  67. select @@PACK_SENT;
  68. select @@TIMETICKS;
  69. select @@IDLE;
  70. select @@TOTAL_ERRORS;
  71. select @@IO_BUSY;
  72. select @@TOTAL_READ;--读取磁盘次数
  73. select @@PACKET_ERRORS;--发生的网络数据包错误数
  74. select @@TOTAL_WRITE;--sqlserver执行的磁盘写入次数


  75. Ø 输出语句

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

  77.      基本语法

  78. print 变量或表达式
  79. select 变量或表达式
  80.    

  81.       示例

  82. select 1 + 2;
  83. select @@language;
  84. select user_name();

  85. print 1 + 2;
  86. print @@language;
  87. print user_name();
  88.      print在输出值不少字符串的情况下,需要用convert转换成字符串才能正常输出,而且字符串的长度在超过8000的字符以后,后面的将不会显示。



  89. Ø 逻辑控制语句

  90.      1、 if-else判断语句

  91.           语法

  92. if <表达式>
  93.     <命令行或程序块>
  94. else if <表达式>
  95.     <命令行或程序块>
  96. else
  97.     <命令行或程序块>
  98.           示例

  99. if简单示例
  100. if 2 > 3
  101.     print '2 > 3';
  102. else
  103.     print '2 < 3';

  104. if (2 > 3)
  105.     print '2 > 3';
  106. else if (3 > 2)
  107.     print '3 > 2';
  108. else
  109.     print 'other';

  110. 简单查询判断
  111. declare @id char(10),
  112.         @pid char(20),
  113.         @name varchar(20);
  114. set @name = '广州';
  115. select @id = id from ab_area where areaName = @name;
  116. select @pid = pid from ab_area where id = @id;
  117. print @id + '#' + @pid;

  118. if @pid > @id
  119.     begin
  120.         print @id + '%';
  121.         select * from ab_area where pid like @id + '%';
  122.     end
  123. else
  124.     begin
  125.         print @id + '%';
  126.         print @id + '#' + @pid;
  127.         select * from ab_area where pid = @pid;
  128.     end
  129. go
  130.    

  131.        2、 while…continue…break循环语句

  132.           基本语法

  133. while <表达式>
  134. begin
  135.    <命令行或程序块>
  136.    [break]
  137.    [continue]
  138.    <命令行或程序块>
  139. end
  140.           示例

  141. --while循环输出到
  142. declare @i int;
  143.     set @i = 1;
  144. while (@i < 11)
  145.     begin
  146.         print @i;
  147.         set @i = @i + 1;
  148.     end
  149. go

  150. --while continue 输出到
  151. declare @i int;
  152.     set @i = 1;
  153. while (@i < 11)
  154.     begin               
  155.         if (@i < 5)
  156.             begin
  157.                 set @i = @i + 1;
  158.                 continue;        
  159.             end
  160.         print @i;
  161.         set @i = @i + 1;               
  162.     end
  163. go

  164. --while break 输出到
  165. declare @i int;
  166.     set @i = 1;
  167. while (1 = 1)
  168.     begin        
  169.         print @i;        
  170.         if (@i >= 5)
  171.             begin
  172.                 set @i = @i + 1;
  173.                 break;        
  174.             end        
  175.         set @i = @i + 1;               
  176.     end
  177. go
  178.    

  179.      3、 case

  180.           基本语法

  181. case
  182.    when <条件表达式> then <运算式>
  183.    when <条件表达式> then <运算式>
  184.    when <条件表达式> then <运算式>
  185.    [else <运算式>]
  186. end
  187.           示例

  188. select *,
  189.     case sex
  190.         when 1 then '男'
  191.         when 0 then '女'   
  192.         else '火星人'
  193.     end as '性别'
  194. from student;

  195. select areaName, '区域类型' = case
  196.         when areaType = '省' then areaName + areaType
  197.         when areaType = '市' then 'city'
  198.         when areaType = '区' then 'area'
  199.         else 'other'
  200.     end
  201. from ab_area;
  202.    

  203.        4、 其他语句

  204. 批处理语句go
  205. Use master
  206. Go

  207. 延时执行,类似于定时器、休眠等
  208. waitfor delay '00:00:03';--定时三秒后执行
  209. print '定时三秒后执行';
复制代码


回复

使用道具 举报

本版积分规则

关闭

站长推荐上一条 /1 下一条

小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

GMT+8, 2024-3-29 18:14 , Processed in 0.066870 second(s), 24 queries .

Powered by Discuz! X3.2

© 2001-2024 Comsenz Inc.

快速回复 返回顶部 返回列表