sql 循环语句几种方式
来源:互联网 发布:ip网络广播系统图 编辑:程序博客网 时间:2024/05/17 02:57
--第一
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
--第二
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 --释放游标
--第三
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
- -- ╔════════╗
- -- =============================== ║ if语句使用示例 ║
- -- ╚════════╝
- declare @a int
- set @a=12
- if @a>100
- begin
- print @a
- end
- else
- begin
- print 'no'
- end
- -- ╔══════════╗
- -- =============================== ║ 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 循环中语句的执行。 本条为以前从网上查找获取!
- -- ╔════════╗
- -- ================================ ║ 临时表和try ║
- -- ╚════════╝
- -- 增加临时表
- select * into #csj_temp from csj
- -- 删除临时表 用到try
- begin try -- 检测代码开始
- drop table #csj_temp
- end try
- begin catch -- 错误开始
- end catch
- -- ╔═════════╗
- -- =============================== ║ 游标循环读记录 ║
- -- ╚═════════╝
- 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
- --
阅读全文
0 0
- sql 循环语句几种方式
- 【转载】sql 循环语句几种方式
- SQL循环语句的几种方式
- sql 循环语句几种方式
- 优化sql 语句的几种方式
- C#中产生SQL语句的几种方式
- C#中产生SQL语句的几种方式
- MySQL笔记-执行SQL语句的几种方式
- mybatis 构建SqlSessionFactory的几种方式 以及调用sql映射语句的几种方式
- oracle循环语句的几种使用方法
- Oracle的几种循环语句
- 跳出循环的几种方式比较
- Map的几种循环方式
- Android中循环的几种方式
- Oracle几种循环的方式
- [Oracle] 循环的几种方式
- java for的几种循环方式
- sql语句删除数据库表的几种方式及区别
- 算法模板——倍增求LCA
- 学习笔记_002
- R数据整合(aggregate)和重塑(reshape)
- python 的日志logging模块学习
- 别问我昨天为什么没跟新
- sql 循环语句几种方式
- Django-restframework05 用户认证和权限管理
- 使用对象 url 显示图像(window.URL.createObjectURL的使用)
- 优先考虑静态成员类。
- 2017.11.8NOIP模拟赛
- 如何通过API调用来分析恶意软件
- BigDecimal 类型的用法
- 诗与远方
- 程序员常去的14个顶级开发社区