【MSSQLServer】 T-SQL完整整理

  1. /*
  2. 第一学期SQL Server应用开发
  3. 第三章 SQL Server数据管理
  4.  3.3 使用T-SQL插入数据
  5.   3.3.1 使用Insert插入数据行
  6.   3.3.2 一次插入多行数据
  7.  3.4 使用T-SQL更新数据
  8.  3.5 使用T-SQL删除数据
  9.   3.5.1 使用delete删除数据
  10.   3.5.2 使用truncate table 删除数据
  11. 第4章 数据查询(1)
  12.  4.2 T-SQL查询基础
  13.   4.2.2 使用select语句进行查询
  14.  4.3 查询排序
  15.  4.4 在查询中使用函数
  16.   4.4.1 字符串函数
  17.   4.4.2 日期函数
  18.   4.4.3 数学函数
  19.   4.4.4 系统函数
  20. 第五章 数据查询(2)
  21.  5.1 模糊查询
  22.   5.1.1 使用like进行模糊查询
  23.   5.1.2 使用between在某个范围内进行查询
  24.  5.2 SQL Server中的聚合函数
  25.   5.2.1 SUM(求和)
  26.   5.2.2 AVG(平均值)
  27.   5.2.3 MAX/MIN(最大值/最小值)
  28.   5.2.4 count(计数)
  29.  5.3 分组查询
  30.   5.3.1 使用Group by进行分组查询
  31.   5.3.2 使用having子句进行分组
  32.  5.4 多表连接查询d
  33.   5.4.1 多表连接查询的分类
  34.   5.4.2 内联接查询
  35.   5.4.3 外联接查询
  36. */
  37. /*
  38. 第三章 SQL Server数据管理
  39. */
  40. --3.3 使用T-SQL插入数据
  41. ---3.3.1 使用insert插入数据行
  42. insert [into] <表名>[列名] values <值列表>
  43. insert into jobs(job_id,job_desc,min_lvl,max_lvl) values(1,'aa',12,12)
  44. ---3.3.2 一次插入多行数据
  45. ----1.通过Insert select 语句将现有表中的数据添加到物理表a中
  46. insert into a(a.job_id) select job_id from jobs
  47. ----2.通过select into 语句将现有表中的数据添加到虚拟表aaa中
  48. select jobs.job_id,jobs.job_desc,jobs.min_lvl,jobs.max_lvl into aaa from jobs 
  49. select indentity(int,1,1) as 列名 into 新表 from 原始表
  50. ----3.通过union关键字并数据进行插入与insert into.....select的效果一样
  51. insert a(job_id)
  52. select 111 union
  53. select 211 union
  54. select 311
  55. --3.4 使用T-SQL更新数据
  56. update <表名> set <列名 = 更新值> [where <更新条件>]
  57. update jobs set jobs_desc = 'aa'
  58. update jobs set job_desc = job_desc+'aa' where job_id = 1
  59. --3.5 使用T-SQL删除数据
  60. ---3.5.1 使用delete删除数据
  61. --delete from <表名> [where <删除条件>]
  62. delete from jobs where job_id = 1
  63. ---3.5.2 使用truncate table 删除数据
  64. truncate table a
  65. /*
  66. 第4章 数据查询(1)
  67. */
  68. --4.2 T-SQL查询基础
  69. ---4.2.2 使用select语句进行查询
  70. select <列名> from <表名> [where <查询条件表达式>] [order by <排序的列名> [asc or desc]]
  71. ----1. 查询所有的数据行和列
  72. select * from jobs
  73. ----2. 查询部分行列 ----条件查询
  74. select job_id,job_desc,min_lvl,max_lvl from jobs where job_id = 1
  75. ----3. 在查询中使用列名
  76. select job_id as 编号,job_desc as 描述 from jobs where job_id <> 1
  77. ----4. 查询空行
  78. select job_id from jobs where job_desc is null
  79. ----5. 在查询中使用常量列
  80. select 编号=job_id,描述=job_desc,'无列名' as 其他 from jobs
  81. ----6. 查询返回限制的行数
  82. select top 5 job_id,job_desc from jobs where job_id <> 1
  83. -----6.1使用percent关键字限制%
  84. select top 50 percent job_id from jobs where job_id <> 2
  85. --4.3 查询排序
  86. select job_id as 编号,job_desc as 描述,min_lvl as 最小值,max_lvl as 最大值 from jobs where min_lvl > 100 order by max_lvl
  87. ---合并查询 job_desc1 +'1'+ job_desc2 表内容连接查询(字符串 整型和字符串 不能匹配)
  88. ---查询非重复项
  89. select max_lvl as 描述 from jobs union
  90. select max_lvl as 描述 from aaa
  91. --4.4 在查询中使用函数
  92. ---4.4.1 字符串函数
  93. /*
  94. charindex   用来寻找一个制定的字符串在另一个字符串中的起始位置
  95.         select charindex('accp','my accp course',1)
  96.         返回:4
  97. len         返回传递给它的字符串长度
  98.         select len('sql server 课程')
  99.         返回:13
  100. lower       把传递给它的字符串转换成小写
  101.         select lower('SQL Server 课程')
  102.         返回:sql server 课程
  103. upper       把传递给它的字符串转换成大写
  104.         select upper('sql server 课程')
  105.         返回:SQL SERVER 课程
  106.     
  107. ltrim       消除字符左边的空格
  108.         select ltrim(' sql server 课程 ')
  109.         返回:'sql server 课程 '(后面的空格保留)
  110. rtrim       消除字符右边的空格
  111.         select rtrim(' sql server 课程 ')
  112.         返回:' sql server 课程'(前面的空格保留)
  113. right       从字符串右边返回指定数目的字符
  114.         select right('买买提.吐尔松',3)
  115.         返回:吐尔松
  116. replace     替换一个字符串中的字符
  117.         select replace('莫勒可且.杨兰','兰','蓝')
  118.         返回:莫勒可且.杨蓝
  119. stuff       在一个字符串中,删除指定长度的字符,并在该位置插入一个新的字符串
  120.         select stuff('ABCDEFG',3,3,'我的音乐我的世界')
  121.         返回:A我的音乐我的世界EFG
  122. */
  123.         select charindex('accp','my accp course',1)
  124.         select len('sql server课程')
  125.         select lower('SQL Server 课程')
  126.         select upper('sql server 课程')
  127.         select ltrim(' sql server 课程 ')
  128.         select rtrim(' sql server 课程 ')
  129.         select right('买买提.吐尔松',3)
  130.         select replace('莫勒可且.杨兰','兰','蓝')
  131.         select stuff('ABCDEFG',2,3,'我的音乐我的世界')
  132. ---4.4.2 日期函数
  133. /*
  134. getdate     取得当前的系统日期
  135.         select getdate()
  136.         返回:今天的日期
  137. dateadd     将制定的数值添加到指定的日期部分后的日期
  138.         select dateadd(mm,4,'01/01/09')
  139.         返回:以当前的日期格式返回01/05/09
  140. datediff    两个日期之间的制定日期部分的区别
  141.         select datediff(mm,'01/01/09','01/03/10')
  142.         返回:2
  143. datename    日期中指定日期部分的字符串形式
  144.         select datename(dw,'01/02/2000')
  145.         返回:Saturday(星期6)
  146. datepart    日期中指定日期部分的整数形式
  147.         select datepart(day,'01/15/2000')
  148.         返回:15
  149. */
  150.         select getdate()
  151.         select dateadd(mm,4,'01/01/09')
  152.         select datediff(mm,'01/01/09','01/03/10')
  153.         select datediff(yyyy,'07/01/09','01/02/10')
  154.         select datename(dw,getdate())
  155.         select datepart(day,'01/16/2000')
  156. ---4.4.3 数学函数
  157. /*
  158. abs     取数值表达式的绝对值
  159.         select abs(-43) 
  160.         返回:43
  161. ceiling     取大于或等于指定数值、表达式的最小整数
  162.         select ceiling(43.5)
  163.         返回:44
  164. floor       取小于或等于制定表达式的最大整数
  165.         select floor(43.5)
  166.         返回:43
  167. power       取数值表达式的幂值(平方)
  168.         select power(5,2)
  169.         返回:25
  170. round       将数值表达式的四舍五入为指定精度
  171.         select round(43.543,1)
  172.         返回:43.500
  173. sign        对于正整数返回+1,对于负数返回-1,对于0则返回0
  174.         select sign(-43)
  175.         返回:-1
  176. sqrt        取浮点表达式的平方根
  177.         select sqrt(9)
  178.         返回:3.0
  179. */
  180.         select abs(-43) 
  181.         select ceiling(43.5)
  182.         select floor(43.5)
  183.         select power(5,3)
  184.         select round(43.543,1)
  185.         select sign(-43)
  186.         select sqrt(9)
  187. ---4.4.4 系统函数
  188. /*
  189. convert     用来转变数据类型
  190.         select convert(varchar(5),12345)
  191.         返回:字符串12345
  192. current_user    返回当前用户的名字
  193.         select current_user
  194.         返回:你登录的用户名
  195. datalength  返回用于指定表达式的字节数
  196.         select datalength('中国A联盟')
  197.         返回:5
  198. host_name   返回当前用户所登录的计算机名字
  199.         select host_name()
  200.         返回:你登录的计算机的名字
  201. system_user 返回当前所登录的用户名称
  202.         select system_user
  203.         返回:你当前所登录的用户名
  204. user_name   从给定的用户ID返回用户名
  205.         select user_name(1)
  206.         返回:从任意数据库中返回"dbo"
  207. */
  208.         select convert(varchar(5),12345)
  209.         select current_user
  210.         select datalength('中国A联盟')
  211.         select host_name()
  212.         select system_user
  213.         select user_name(1)
  214. /*
  215. 第五章 数据查询(2)
  216. */
  217. --5.1 模糊查询
  218. ---5.1.1 使用like进行模糊查询
  219. select * from jobs where job_desc like 'a%'
  220. ---查询id不是1的
  221. select * from jobs where job_id like '[^1]%'
  222. ---5.1.2 使用between在某个范围内进行查询
  223. select * from jobs where job_id between 1 and 5
  224. ---5.1.3 使用In在列举值内进行查询
  225. select job_id as 编号,job_desc as 描述 from jobs where job_id in (1,2,3,4) order by job_id
  226. --5.2 SQL Server中的聚合函数
  227. ---5.2.1 SUM(求和)
  228. select sum(job_id) from jobs where job_id in (1,2,3,4)
  229. ---5.2.2 AVG(平均值)
  230. select avg(min_lvl) from jobs where job_id in (1,2,3,4,5,6)
  231. ---5.2.3 MAX/MIN(最大值/最小值)
  232. select max(max_lvl) as 最大值, min(min_lvl) as 最小值 from jobs where min_lvl>10
  233. ---5.2.4 count(计数)
  234. select count(*) as 总数 from jobs where job_id <10
  235. --5.3 分组查询
  236. ---5.3.1 使用Group by进行分组查询
  237. select avg(min_lvl) as 平均数 from jobs group by min_lvl
  238. ---5.3.2 使用having子句进行分组
  239. /*
  240. where: 子句从数据源中去掉不符合其搜索条件的数据;
  241. group by:子句搜索数据行到各个组中,统计函数为各个组计算统计值;
  242. having: 子句去掉不符合其组搜索条件的各组的搜索行
  243. */
  244. select min_lvl from jobs where min_lvl > 20 group by min_lvl having count(min_lvl)>2
  245. --5.4 多表连接查询
  246. ---5.4.1 多表连接查询的分类
  247. ----1.内联接 inner join
  248. ----2.外联接 
  249. -----1).左外联接:left join 或 left outer join
  250. -----2).右外联接:right join 或 right outer join
  251. -----3).完整外联接: full join 或 full outer join
  252. ----3.交叉联接
  253. ---5.4.2 内联接查询
  254. ----1.在where子句中指定联接条件
  255. select Students.SName,Score.CourseID,Score.Score from Students,Score where Students.SCode = Score.StudentID
  256. ----2.在From子句中使用join..on
  257. select S.SName,C.CourseID,C.Score from Students as S inner join Score as C on (S.Scode = C.StudentID)
  258. ---5.4.3 外联接查询
  259. ----1. 左外联接查询(Students主表,Score从表)Students主表所有的数据都会被列出 如果从表中没有与主表对应的数据则为NULL
  260. select S.SName,C.CourseID,C.Score from Students as S left outer join Score as C on S.Scode = C.StudentID
  261. ----2. 右外联接查询(Titles从表,Publishers从表)Publishers主表所有的数据都会被列出 如果从表中没有与主表对应的数据则为NULL
  262. select Titles.Title_id,Titles.Title,Publishers.Pub_name from Titles right outer join Publishers on Titles.Pub_id = Publishers.Pub_id
  263. /*
  264. 第二学期 SQL Server数据库设计和实现
  265. 第二章 数据库的实现
  266.  2.1 T-SQL语句回顾
  267.   添加数据
  268.   修改数据
  269.   查询数据
  270.   删除数据
  271.  2.2 使用SQL语句创建和删除数据库
  272.   2.2.1 创建数据库
  273.   2.2.2 删除数据库
  274.  2.3 使用SQL语句创建和删除表
  275.   2.3.1 创建表
  276.   2.3.2 删除表
  277.  2.4 使用SQL语句创建和删除约束
  278.   2.4.1 添加约束
  279.   2.4.2 删除约束
  280.  2.5 使用SQL语句创建登录
  281.   2.5.1 创建登录账户
  282.   2.5.2 创建数据库用户
  283.   2.5.3 向数据库用户授权
  284. 第三章 T-SQL编程
  285.  3.1 使用变量
  286.   3.1.1 局部变量
  287.   3.1.2 全局变量
  288.  3.2 输出语句
  289.  3.3 逻辑控制语句
  290.   3.3.1 IF-ELSE 条件语句
  291.   3.3.2 WHILE 循环语句
  292.   3.3.3 CASE多分支语句
  293.  3.4 批处理语句
  294. 第四章 高级查询
  295.  4.1 简单子查询
  296.  4.2 IN和NOT IN子查询
  297.  4.3 EXISTS 和 NOT EXISTS 子查询
  298. 第五掌 事务、索引和视图
  299.  5.1 事务
  300.  5.2 索引
  301.   5.2.1 什么是索引
  302.   5.2.2 如何创建索引
  303.  5.3 视图
  304.   5.3.2 如何创建视图
  305. 第六章 存储过程
  306.  6.2 常用的系统存储过程
  307.  6.3 用户自定义存储过程
  308.   6.3.1 创建不带参数的存储过程
  309.   6.3.2 创建带输出参数的存储过程
  310.   6.3.3 创建带输出参数的存储过程
  311.   6.3.4 处理错误信息
  312. */
  313. --第二章 数据库的实现
  314. --2.1 T-SQL语句回顾
  315. ---添加数据
  316. insert into jobs(job_id,job_desc,min_lvl,max_lvl) values(1,'bbb',12,12)
  317. ---修改数据
  318. update jobs set job_desc = 'bbbbc' where job_id = 1
  319. ---查询数据
  320. select job_id,job_desc from jobs where job_id > 4 order by job_desc 
  321. ---删除数据
  322. delete from jobs where job_id = 2
  323. --2.2 使用SQL语句创建和删除数据库
  324. ---2.2.1 创建数据库
  325. /*
  326. create database 数据库名
  327.   on [primary]
  328.   (
  329.     <数据文件参数>,[……n]
  330.     [name = 逻辑文件名,]
  331.     filename = 物理文件名
  332.     [,size = 大小]
  333.     [,maxsize = {最大容量|unlimited}]
  334.     [,filegrowth = 增长量]
  335.     [,……n]
  336.     [<文件组参数>]
  337.     filegroup 文件组名 <文件参数>[,……n]
  338.   )
  339.   [log on]
  340.   (
  341.     {<日志文件参数>,[……n]}
  342.   )
  343. */
  344. create database stuDB
  345. on primary
  346. (
  347.   name = 'stuDB_data',
  348.   filename = 'D:/project/stuDB_data.mdf',
  349.   size = 5mb,
  350.   maxsize = 100mb,
  351.   filegrowth = 15%
  352. ),
  353. (
  354.   /*多个数据文件*/
  355. )
  356. log on
  357. (
  358.   name = 'stuDB_log',
  359.   filename = 'D:/project/stuDB_log.ldf',
  360.   size = 2mb,
  361.   filegrowth = 1mb
  362. ),
  363. (
  364.   /*多个日志文件*/
  365. )
  366. go
  367. ---2.2.2 删除数据库
  368. --drop database 数据库名
  369. drop database stuDB
  370. --完整T-SQL
  371. use pubs
  372. go
  373. if exists (select * from sysdatabases where name = 'stuDB')
  374.     drop database stuDB
  375. create database stuDB 
  376. on 
  377. (
  378. ...
  379. )
  380. log on 
  381. (
  382. ...
  383. )
  384. go
  385. --2.3 使用SQL语句创建和删除表
  386. ---2.3.1 创建表
  387. use stuDB
  388. go
  389. create table stuInfo
  390. (
  391.   stuName varchar(20) not null,
  392.   stuNo char(6) not null,
  393.   stuAge int not null,
  394.   stuID numeric(18,0) --身份证号,numeric(18,0)代表18位数字,小数位数为0
  395.   stuSeat smallint identity(1,1), 
  396.   stuAddress text
  397. )
  398. go
  399. create table stuMarks
  400. (
  401.   ExamNo char(7) not null,
  402.   stuNo char(6) not null,
  403.   writtenExam int not null,
  404.   LabExam int not null
  405. )
  406. go
  407. ---2.3.2 删除表
  408. drop table stuInfo
  409. --完整T-SQL
  410. use stuDB
  411. go
  412. if exists(select * from sysobjects where name = 'stuInfo')
  413.     drop table stuInfo
  414. create table stuInfo
  415. (
  416. ...
  417. )
  418. go
  419. --2.4 使用SQL语句创建和删除约束
  420. ---2.4.1 添加约束
  421. /*
  422. alter table 表名
  423. add constraint 约束名 约束类型 具体的约束说明
  424. */
  425. ----添加主键约束(stuNo作为主键)
  426. alter table stuInfo
  427. add constraint PK_stuNo primary key (stuNo)
  428. ----添加唯一约束(身份证号唯一)
  429. alter table stuInfo
  430. add constraint UQ_stuID unique (stuID)
  431. ----添加默认约束,(如果地址不详,默认为"地址不祥")
  432. alter table stuInfo
  433. add constraint DF_stuAddress default ('地址不祥'for stuAddress
  434. ----添加检查约束,要求年龄只能在15-40岁之间
  435. alter table stuInfo
  436. add constraint CK_stuAge check(stuAge between 15 and 40)
  437. ----添加外键约束(主表stuInfo和从表stuMarks建立关系,关联字段为stuNo)
  438. alter table stuMarks
  439.   add constraint FK_stuNo
  440.     foreign key (stuNo) references stuInfo(stuNo)
  441. go
  442. ---2.4.2 删除约束
  443. /*
  444. alter table 表名
  445.   drop constraint 约束名
  446. */
  447. alter table stuInfo
  448.   drop constraint DF_stuAddress
  449. --2.5 使用SQL语句创建登录
  450. ---2.5.1 创建登录账户
  451. --exec sp_grantlogin 'windos 域名/域账户'
  452. --exec sp_addlogin '账户名','密码'
  453. exec sp_grantlogin 'G/cjf'
  454. exec sp_addlogin 'congcong','2664'
  455. go
  456. ---2.5.2 创建数据库用户
  457. --exec sp_grantdbaccess '登录账户','数据库用户'
  458. /*--在stuDB数据库中添加两个用户--*/
  459. use stuDB
  460. go
  461. exec sp_grantdbaccess 'G/cjf','stuDB'
  462. exec sp_grantdbaccess 'zhangsan','zhangsanDBUser'
  463. --2.5.3 向数据库用户授权
  464. 授权的语法为:
  465. grant 权限 [on 表名] to 数据库用户
  466. use stuDB
  467. /*--为zhangsanDBUser分配对表stuInfo的select,insert,update权限--*/
  468. grant select,insert,update on stuInfo to zhangsanDBUser
  469. /*--为S26301DBUser分配建表的权限--*/
  470. grant create table to S26301DBUser
  471. --第三章 T-SQL编程
  472. --3.1 使用变量
  473. ---3.1.1 局部变量
  474. --语法:declare @variable_name DataType
  475. declare @name varchar(20)
  476. declare @seat int
  477. --局部变量赋值两种方法 set语句或select语句
  478. --语法:set @variable_name = value或 select @variable = value
  479. ---3.1.2 全局变量
  480. print @@error           --最后一个T-SQL错误的错误号
  481. print @@identity        --最后一次插入的标识值
  482. print @@language        --当前使用的语言的名称
  483. print @@max_connections     --可以创建的同时连接的最大数据
  484. print @@rowcount        --受上一次SQL语句影响的行数
  485. print @@servername      --本地服务器的名称
  486. print @@servicename     --该计算机上的SQL服务的名称
  487. print @@timeticks       --当前计算机上每刻度的微秒数
  488. print @@trancount       --当前连接打开的事务数
  489. print @@version         --SQL Server的版本信息
  490. --3.2 输出语句
  491. --print 局部变量或字符串
  492. --select 局部变量 AS 自定义列名
  493. --3.3 逻辑控制语句
  494. ---3.3.1 IF-ELSE 条件语句
  495. /*语法:
  496.   if(条件)
  497.     语句或语句块
  498.   else
  499.     语句或语句块
  500. */
  501. ---3.3.2 WHILE 循环语句
  502. /*语法:
  503.   while (条件)
  504.     语句或语句块
  505.     [break]
  506. */
  507. ---3.3.3 CASE多分支语句
  508. /*语法:
  509.   case
  510.     when 条件1 then 结果1
  511.     when 条件2 then 结果2
  512.   end
  513. */
  514. --3.4 批处理语句
  515. go
  516. --第四章 高级查询
  517. ---4.1 简单子查询
  518. --实现1:采用T-SQL变量实现,SQL语句如下
  519. declare @age int
  520. select @age=stuAge from stuInfo where stuName='cjf'
  521. select * from stuInfo where stuAge>@age
  522. go
  523. --实现2:采用子查询实现,SQL语句如下
  524. select * from stuInfo
  525.   where stuAge>@age
  526. ---示例2:
  527. select * from stuInfo
  528.   where stuAge>(select stuAge from stuInfo where stuName='cjf')
  529. ---示例3:采用表连接
  530. select stuName from stuInfo inner join stuMarks
  531.   on stuInfo.stuNo = stuMarks.stuNo where writtenExam=60
  532. go
  533. ---示例4:采用子查询
  534. select stuName from stuInfo
  535.   where stuNo=(select stuNo from stuMarks where writtenExam=60)
  536. go
  537. --4.2 IN和NOT IN子查询
  538. ---示例5: 采用IN子查询
  539. select stuName from stuInfo
  540.   where stuNo in (select stuNo from stuMarks where writtenExam=60)
  541. go
  542. --4.3 EXISTS 和 NOT EXISTS 子查询
  543. if exists(select * from sysdatabases where name='stuDB')
  544.   drop database stuDB
  545. create database stuDB
  546. ...
  547. if exists(子查询)
  548.   语句
  549. --第五掌 事务、索引和视图
  550. --5.1 事务
  551. /*Transact-SQL使用下列语句来管理事务
  552.   开始事务:begin transaction
  553.   提交事务:commit transaction
  554.   回滚事务: roolback transaction
  555. */
  556. --5.2 索引
  557. ---5.2.1 什么是索引
  558. --索引:是SQL Server编排数据的内部方法,它为SQL Server提供一中方法来编排查询数据的路由。
  559. /*索引三大类
  560.   唯一索引、主键索引、聚集索引
  561. */
  562. ---5.2.2 如何创建索引
  563. /* 语法
  564. create [unique][clustered][nonclustered] index index_name
  565.   on table_name (column_name[,column_name].....)
  566.     [with
  567.        fillfactor=x
  568.     ]
  569. */
  570. --5.3 视图
  571. ---5.3.2 如何创建视图
  572. /*语法:
  573. create view view_name
  574.   as 
  575.     <select 语句>
  576. */
  577. --第六章 存储过程
  578. --6.2 常用的系统存储过程
  579. /*   系统存储过程名              说明
  580.     sp_databases        列出服务器上的所有数据库
  581.     sp_helpdb       报告有关指定数据库或所有数据库的信息
  582.     sp_renamedb     更改数据库的名称
  583.     sp_tables       返回当前环境下可查询的对象的列表
  584.     sp_columns      返回某个表列的信息
  585.     sp_help         返回某个表的所有信息
  586.     sp_helpconstraint   查看某个表的约束
  587.     sp_helpindex        查看某个表的索引
  588.     sp_stored_procedures    列出当前环境中的所有存储过程
  589.     sp_password     添加或修改登录账户的密码
  590.     sp_helptext     显示默认值、未加密的存储过程、用户自定义的存储过程、触发器或视图的实际文本
  591. */
  592. exec sp_databases
  593. exec sp_helpdb
  594. exec sp_renamedb
  595. exec sp_tables
  596. exec sp_columns
  597. exec sp_help
  598. exec sp_helpconstraint
  599. exec sp_helpindex
  600. exec sp_stored_procedures
  601. exec sp_password
  602. exec sp_helptext
  603. --扩展
  604. exec xp_cmdshell 'mkdir d:/bank',no_output--无输出
  605. --6.3 用户自定义存储过程
  606. ---6.3.1 创建不带参数的存储过程
  607. /*语法:
  608. create procedure 存储过程名
  609.     [{@参数1 数据类型}[= 默认值] [output],
  610.     ....,
  611.      {@参数n 数据类型}[= 默认值] [output]
  612.     ]
  613.   as
  614.     SQL 语句
  615. */
  616. if exists(select * from sysobjects where name = 'proc_stu')
  617.   drop procedure proc_stu
  618. go
  619. create procedure proc_stu
  620.  as
  621. ...
  622. go
  623. exec proc_stu
  624. ---6.3.2 创建带输入参数的存储过程
  625. if exists(select * from sysobjects where name='proc_stu')
  626.   drop procedure proc_stu
  627. go
  628. create procedure proc_stu
  629.   @writtenPass int=60,
  630.   @labPass int=60
  631. as
  632. ...
  633. go
  634. exec proc_stu 60,55
  635. --exec proc_stu @labPass=55,@writtenPass=60
  636. --exec proc_stu --都采用默认值:笔试和即使及格线都为60分
  637. --exec proc_stu 64 --机试采用默认值:笔试及格线64分,机试及格线60分
  638. --exec proc_stu 60,55 --都不采用默认值
  639. --错误的调用方式: exec proc_stu ,55 --希望笔试采用默认值,机试及格线55分
  640. --正确的调用方式: exec proc_stu @labPass=55 --笔试采用默认值,机试及格线55分
  641. ---6.3.3 创建带输出参数的存储过程
  642. if exists(select * from sysobjects where name='proc_stu')
  643.   drop procedure proc_stu
  644. go
  645. create procedure proc_stu
  646.   @notpassSum int output,--output关键字,否则视为输入参数
  647.   @writtenPass int=60, --默认参数放后
  648.   @labPass int=60 --默认参数放后
  649.   as
  650.   ....
  651. go
  652. declare @sum int
  653. exec proc_stu @sum output,64
  654. ---6.3.4 处理错误信息
  655. if(not @writtenPass between 0 and 100) or (not @labPass between 0 and 100)
  656.   begin
  657.     raiserror('错误!',16,1)
  658.     return 
  659.   end
  660. --第七章 触发器
  661. --7.3 如何创建触发器
  662. ---7.3.1 创建INSERT、DELETE、UPDATE触发器
  663. /*语法:
  664. create trigger Trigger_name
  665.   on table_name
  666.     [with encryption]--加密 可以防止触发器作为SQL Server复制的一部分发布
  667.     for {[delete,insert,update]}
  668.      as
  669.     SQL 语句
  670. */
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值