1 / 21

15-2

第. 讲. 15-2. SQL编程 (流程控制语句、异常控制). 目标. SQL 中的流程控制语句. SQL 中异常控制语句. 问题引入.   对于 studentdb 数据库 , 需要打印出所有学生的 “ 英语 ” 成绩,如果该成绩大于等于 60 ,显示合格的消息。. How To ?. 流程控制语句.   设计程序时,常常需要利用各种流程控制语句,改变计算机的执行流程以满足程序设计的需要。 在 SQL Server 中提供了下表中的流程控制语句:. IF…ELSE语句.

Download Presentation

15-2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 讲 15-2 SQL编程 (流程控制语句、异常控制)

  2. 目标 SQL中的流程控制语句 SQL中异常控制语句

  3. 问题引入   对于studentdb数据库,需要打印出所有学生的“英语”成绩,如果该成绩大于等于60,显示合格的消息。 How To ?

  4. 流程控制语句   设计程序时,常常需要利用各种流程控制语句,改变计算机的执行流程以满足程序设计的需要。 在SQL Server中提供了下表中的流程控制语句:

  5. IF…ELSE语句 在程序中如果要对给定的条件进行判定,当条件为真或假时执行不同的T-SQL语句,可用IF…ELSE语句实现。   语法如下: 条件表达式 IF Boolean_expression { sql_statement | statement_block } [ ELSE { sql_statement | statement_block } ] 条件表达式为真的时候执行的语句 条件表达式为假的时候执行的语句

  6. declare @num int set @num = 1 if @num >=0 print '@num变量中存放的是正数' else print '@num变量中存放的是负数' IF…ELSE示例

  7. WHILE语句   如果需要重复执行程序中的一部分语句,可使用WHILE循环语句实现。   语法如下: WHILE Boolean_expression { sql_statement | statement_block } 条件表达式 条件表达式为真的时候执行的语句

  8. declare @num int set @num = 3 while @num>0 begin print @num set @num = @num - 1 end WHILE语句示例

  9. BREAK和CONTINUE语句 1. BREAK语句   一般用在循环中,用于退出本层循环。当程序中有多层循环嵌套时,使用BREAK语句只能退出其所在层的循环。 2. CONTINUE语句 CONTINUE语句用在循环中,使WHILE 循环重新开始执行,忽略 CONTINUE 关键字后的任何语句。

  10. declare @num int set @num = 10 while @num>0 begin set @num = @num - 1 if @num = 8 continue if @num = 3 break print @num end BREAK和CONTINUE语句示例

  11. RETURN语句 RETURN 命令用于结束当前程序的执行返回到上一个调用它的程序或其它程序,并可在退出时指定一个整型的返回值。   其语法如下: RETURN [ integer_expression ] 整数表达式(默认0表示成功)

  12. TRY…CATCH语句 SQL Server2005中,异常捕获以BEGIN TRY开始,以END TRY结束;异常处理以BEGIN CATCH开始,以END CATCH结束。   其语法如下: BEGIN TRY { sql_statement | statement_block } END TRY BEGIN CATCH { sql_statement | statement_block } ---异常发生后进行处理的语句 END CATCH ---可能发生异常的语句

  13. TRY…CATCH语句(续)   在catch块中,可以使用error类提供的函数来确定关于错误的信息,SQL Server2005提供的显示错误信息的函数如下表:

  14. declare @num int begin try set @num = 10 set @num = @num / 0 end try begin catch print ERROR_LINE() print ERROR_MESSAGE() end catch TRY…CATCH语句示例

  15. RAISERROR语句 在SQL Server2005中可以使用RAISERROR语句返回用户定义的错误消息。当在TRY 块中运行程序发生严重级别为 11 到19的错误时,RAISERROR它便会将控制传输至关联的 CATCH 块。   其语法如下: RAISERROR ( { msg_id }     { ,severity ,state } ] 用户定义消息 严重级别 状态号

  16. declare @num int begin try set @num = -1 if @num < 0 raiserror ('@num值必须大于零',12,1) end try begin catch print ERROR_LINE() print ERROR_MESSAGE() end catch RAISERROR语句示例

  17. 解决方案   分析:创建一个批处理,定义如下所需变量。 所需SQL语句如下: DECLARE @stuName varchar(50) DECLARE @stuNo varchar(50) DECLARE @score float DECLARE @i int DECLARE @count int SET @i = 0 SELECT @count = count(*) FROM Tb_Stu_Score WHERE subject_id = '1' SELECT ROW_NUMBER() OVER (ORDER BY stu_no) AS Row, * into #temp from Tb_Stu_Score WHERE subject_id = '1'

  18. 解决方案(续)   分析:通过循环取得所有学生的英语成绩,IF判断是否大于等于60 所需SQL语句如下: WHILE(@i <= @count) BEGIN SET @i = @i + 1 SELECT @stuNo = stu_no,@score = score FROM #temp WHERE Row = @i SELECT @stuName = Stu_Name FROM Tb_Stu_Info WHERE stu_no = @stuNo IF (@score >= 60) print @stuName + '的英语成绩合格' END

  19. 小结   本节我们学习了以下主要内容: • IF…ELSE语句。 • WHILE、BREAK和CONTINUE语句。 • RETURN语句。 • 异常处理机制。

  20. 作业   对于studentdb数据库,需要打印出所有学生的所有科目的平均成绩,如果该成绩大于等于60,显示合格的消息。

More Related