1、if语句使用示例

1

2

3

4

5

6

7

8

9

10

declare @a int

set @a=12

if @a>100

begin

  print @a

end

else

begin

  print 'no'

end

2、while语句使用示例

1

2

3

4

5

6

7

8

declare @i int

set @i=1

while @i<30

begin

  insert into test (userid) values(@i)

set @i=@i+1

end

-- 设置重复执行 SQL 语句或语句块的条件。只要指定的条件为真,就重复执行语句。可以使用 BREAK 和 CONTINUE 关键字在循环内部控制 WHILE 循环中语句的执行。 

3、临时表和try

1

2

3

4

5

6

7

8

-- 增加临时表

select into #csj_temp from csj

-- 删除临时表 用到try

begin try -- 检测代码开始

  drop table #csj_temp

end try

begin catch -- 错误开始

end catch

  

4、正常循环语句

1

2

3

4

5

6

7

8

9

10

11

12

13

14

declare @orderNum varchar(255)

create table #ttableName(id int identity(1,1),Orders varchar(255))

declare @n int,@rows int

insert #ttableName(orders) select orderNum from pe_Orders where orderId<50

--select @rows=count(1) from pe_Orders

select @rows =@@rowcount

set @n=1

while @n<=@rows

begin

  select @orderNum=OrderNum from PE_Orders where OrderNum=(select Orders from #ttableName where id=@n)

  print (@OrderNum)

  select @n=@n+1

end

drop table #ttableName

5、不带事务的游标循环

1

2

3

4

5

6

7

8

9

10

11

12

13

declare @orderN varchar(50)  --临时变量,用来保存游标值

declare y_curr cursor for   --申明游标 为orderNum

select orderNum from pe_Orders where orderId<50

open y_curr   --打开游标

fetch next from Y_curr into @orderN   ----开始循环游标变量

while(@@fetch_status=0)  ---返回被 FETCH 语句执行的最后游标的状态,而不是任何当前被连接打开的游标的状态。

begin

  print (@orderN)

  update pe_Orders set Functionary+@orderN where orderNum=@orderN   --操作数据库

  fetch next from y_curr into @orderN   --开始循环游标变量

end

close y_curr  --关闭游标

deallocate y_curr   --释放游标

6、带事务的游标循环

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

select orderNum,userName,MoneyTotal into #t from pe_Orders po

DECLARE @n int,@error int

--set @n=1

set @error=0

BEGIN TRAN   --申明 开始事务

declare @orderN varchar(50),@userN varchar(50)   --临时变量,用来保存游标值

declare y_curr cursor for    --申明游标 为orderNum,userName

select orderNum,userName from PE_Orders where Orderid<50

open y_curr

fetch next from y_curr into @orderN,@userN

while @@fetch_status = 0

BEGIN

  select isnull(sum(MoneyTotal),0),orderNum from #t where username=@userN

  -- set @n=@n+1

  set @error=@error+@@error  --记录每次运行sql后 是否正确 0正确

  fetch next from y_curr into @orderN,@userN

END

IF @error=0

BEGIN

  commit tran   ---事务提交

END

ELSE

BEGIN

  ROLLBACK TRAN   ---事务回滚

END

close y_curr

deallocate y_curr

DROP TABLE #t

   

7、游标循环读记录

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

declare @temp_temp int

--declare @Cur_Name

--@Cur_Name="aaa"

--------------------------------- 创建游标 --Local(本地游标)

DECLARE aaa CURSOR for select House_Id from House_House where Deleted=0 or deleted is null

----------------------------------- 打开游标

Open aaa

----------------------------------- 遍历和获取游标

fetch next from aaa into @temp_temp

--print @temp_temp

while @@fetch_status=0

begin

  --做你要做的事

  select from House_monthEnd where House_Id=@temp_temp

  fetch next from aaa into @temp_temp -- 取值赋给变量

--

end

----------------------------------- 关闭游标

Close aaa

----------------------------------- 删除游标

Deallocate aaa

SQL 循环语句几种写法相关推荐

  1. for循环的几种写法

    参考链接: for循环的三种写法 - 美好的明天 - 博客园 今天在写程序的时候,在百度上看到了一个语句是这样写的 for(auto& text: textlist) 不知甚解,所以来百度记录 ...

  2. Java中List for循环的6种写法总结(推荐)(亲测)

    如下所示: List<String> list = new ArrayList<String>(); ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ...

  3. java for循环 写法_java中for循环的6种写法

    packageForLoop;importjava.util.ArrayList;importjava.util.Iterator;importjava.util.List;/*** java中for ...

  4. sql分页的几种写法

    一直想整理下关于sql分页的几种方法,今天终于有时间整理下了.闲话少说直接上sql,先创建一个测试库,测试表以及测试数据,sql语句如下: CREATE DATABASE DBTEST GO USE ...

  5. Java中List for循环的几种写法种写法总结(推荐)

    List<String> list = new ArrayList<String>(); /** * 方法一:最普通的不加思考的写法 * <p> * 优点:较常见, ...

  6. 一元函数黄金分割求最优解中循环的两种写法

    我用C语言编写黄金分割法的过程中发现它可以用两种方法来编写,主要区别是循环结构的表达方式不同,一种是常规的while循环语句,另一种是采用了函数自我调用的方法进行循环. 因为本人是个菜鸟,在设计函数自 ...

  7. mysql数据库insert语句怎么写_MySQL数据库Insert语句7种写法

    简介 很多开发人员工作了几年之后,都会自嘲,自己啥技术都没学到,就会CRUD,可是我要说的是,CRUD你真的都会吗,你在MySQL数据库中,会几种insert语句写法呢.在这里我会7种写法,下面我就来 ...

  8. mysql动态sql循环语句_mysql存储过程循环遍历sql结果集,并执行动态sql

    /* 将其他几张表数据复制到一张总表中 */ DROP PROCEDURE IF EXISTS sp_customer; CREATE PROCEDURE sp_customer() BEGIN -- ...

  9. java的for循环的几种写法

    J2SE 1.5提供了另一种形式的for循环.借助这种形式的for循环,可以用更简单地方式来遍历数组和Collection等类型的对象.本文介绍使用这种循环的具体方式,说明如何自行定义能被这样遍历的类 ...

最新文章

  1. 【python教程入门学习】Pandas库下载和安装
  2. 机械妖姬上门要源码后续结果来了!
  3. 只有mdf文件的恢复技术
  4. image caption优秀链接
  5. absolute元素水平居中
  6. Ubuntu中的回车与换行
  7. 【安全牛学习笔记】COCAT
  8. jquery 初步(四)内容过滤器
  9. LINUX下system和execl有什么差异?
  10. 网页数据抓取工具 (谷歌插件 web Scraper)
  11. Kafka消费异常报Failing OffsetCommit request since the consumer
  12. java实现每天定时执行任务,Spring Task定时任务每天零点执行一次的操作
  13. Java微信公众号开发之初步认识微信公众平台
  14. 中国传媒大学计算机考研调剂,2018年中国传媒大学考研调剂信息
  15. python大数据课程_Python课程(大数据系列)ElasticSearch从基础与实战视频课程
  16. TCP/IP协议数据链路层
  17. 一本超越期待的 C++ 书——简评《Boost程序库完全开发指南:深入C++“准”标准库》...
  18. 介绍自己过去现在和未来_过去,现在和未来
  19. JZOJ2018.07.12【2018提高组】模拟B组 魔道研究
  20. MySQL数据库性能优化(享学课堂听课笔记)

热门文章

  1. 拆机观察自己电脑中的各个硬件模块
  2. 特殊用途光纤连接器和光缆组件
  3. 写一份java开发周报
  4. FFmpeg3.4.2编译配置说明文档
  5. 英语阅读的逻辑——唐迟
  6. 移动端app开发,框架的选择。
  7. 莫烦老师,DQN代码学习笔记(图片版)
  8. SQL server数据库的备份与还原遇到的问题
  9. Ansys-超弹性分析-缓冲垫(非线性)分析学习收获
  10. 性能测试接口请求超时问题_如何获得更好的性能:超时的情况