首页 > 数据库 >SQL 循环语句几种写法

SQL 循环语句几种写法

时间:2022-09-30 08:45:08浏览次数:50  
标签:语句 temp -- 游标 int SQL 写法 declare select

摘自:https://www.cnblogs.com/guorongtao/p/11939751.html

1、正常循环语句

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

2、不带事务的游标循环

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   --释放游标

3、带事务的游标循环

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

4、if语句使用示例

declare @a int
set @a=12
if @a>100
begin
  print @a
end
else
begin
  print 'no'
end

5、while语句使用示例

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 循环中语句的执行。 本条为以前从网上查找获取!

6、临时表和try

-- 增加临时表
select * into #csj_temp from csj
-- 删除临时表 用到try
begin try -- 检测代码开始
  drop table #csj_temp
end try
begin catch -- 错误开始
end catch

7、游标循环读记录

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

标签:语句,temp,--,游标,int,SQL,写法,declare,select
From: https://www.cnblogs.com/gzb1/p/16743697.html

相关文章

  • mysql安全基础(1)常见语句
    上一篇: docker安装和登陆mysql发现用docker安装mysql非常简洁方便快速,继续学习。1.查看当前数据库排行榜可以通过当前数据库使用排行榜:db-engines.comThemostpopula......
  • python 连接 sqlite3 文件
     #0.pipinstallpymysql#1.导入pymysqlimportpymsql#2.创建一个数据库连接对象#3.创建游标cursor=conn.cursor()#4.SQL执行cursor.execute(sql)......
  • 学习 MySQL 需要知道的 28 个小技巧
    如何快速掌握MySQL?培养兴趣兴趣是最好的老师,不论学习什么知识,兴趣都可以极大地提高学习效率。不管学习 MySQL5.7 还是 MySQL8.0 都不例外!夯实SQL基础计算机领......
  • MySQL——SQL加锁机制简要分析
    前提:针对于InnoDB引擎行锁讨论 锁机制MySQL的锁机制可以分为:锁模型(lock mode)和锁类型(locktype) 锁模型(lock mode)共享锁&排他锁InnoDB实现了两种类型的......
  • sql 行列转换
    行列转换实例表ttt有三个字段seq--序列jcxm--检查项目zhi--值数据分别如下:seqjcxmzhi-----------------------111......
  • 随说秋色园从Access升迁到MSSQL过程
    ​ ​​秋色园​​的运行环境概况:目前运行在国外godaddy的虚拟主机的一个子目录中,数据库为Access。 随说Access分页:​ 1:topmax(id)在​​CYQ.Data​​数据框架支持上Ac......
  • mysql
                    ......
  • props写法
    在eslint中写props,直接简写会有语法错误提示,要想去掉,可以加忽略检查的注释,但最好还是改为eslint认可的规范写法加注释:在上一行加上//eslint-disable-next-line,或者在......
  • less写法示例
    less写法示例<divclass="parentDiv"><divclass="childDiv">childDiv1</div><divclass="childDiv">childDiv2</div><divclass="childDiv">childDiv3</div>......
  • NoSQL数据库与关系数据库的比较
    SQL,即结构化查询语言,是传统的关系型数据库的查询语言。SQL数据库能够通过简化CRUD操作,处理数据库中的结构化数据。此处的CRUD代表了创建(create)、检索(或读取,retrieve、r......