前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >SQL Server for循环用法

SQL Server for循环用法

作者头像
浩Coding
发布2019-07-03 11:03:20
16.6K0
发布2019-07-03 11:03:20
举报
文章被收录于专栏:浩Coding浩Coding

最近需要修改一个项目,涉及到SqlServer数据库知识,整理出来和大家分享一下,老规矩,先贴示例,最后是语法:

准备工作,模拟2012-2018年订单数据:

先从tb_Detail订单详细表中找出属于2012年的数据(备注有,故意模拟的),查出OrderID:

select OrderID from tb_Detail where Remark = "2012年数据用";

再通过OrderID修改tb_OrderInfo商品订单表中的各个年度:

update tb_OrderInfo set OrderDate="2012-05-31 11:27:37.123" where OrderID in (select OrderID from tb_Detail where Remark = "2012年数据用");

查看了下整个2012年数据没问题(select OrderID from tb_OrderInfo where YEAR(OrderDate) =2012;)可以考虑使用for循环了:

declare @min int

declare @max int

set @min=2013

set @max=2019

while @min<@max

begin

update tb_OrderInfo set OrderDate=cast(@min as varchar(10))+"-05-31 11:27:37.123" where OrderID in (select OrderID from tb_Detail where Remark = cast(@min as varchar(10))+"年数据用")

set @min=@min+1

end

注意for循环中的cast(@min as varchar(10))字段,这是因为我们声明的@min变量是int类型的,而后面的“年数据用”和我们要查询的Remark字段都是vachar类型的,所以需要把int类型转为varchar,不然会出现如下错误:

另外,如果用命令行输入的话,注意复制粘贴SQL语句时候的换行符问题。

如图执行for循环:

最后测试数据,通过,噢耶\(^o^)/!!

for循环语法介绍:

-- ╔════════╗

-- =============================== ║ 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

--

参考文章:

for循环的实现:

https://zhidao.baidu.com/question/72824226.html

https://zhidao.baidu.com/question/82332450.html

https://www.cnblogs.com/xielong/p/5941595.html

SqlServer类型拼接问题:

https://zhidao.baidu.com/question/2073884979869176588.html

最后感想:

也是最近做项目用得上了SqlServer的知识,想到大学时候数据库课老师讲过这个,考试还考了for循环呢,哈哈哈,但是现在已经全忘了,翻翻以前的课件,查查资料还是能拿起来的,感谢大学所有老师!

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2018-12-25,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 浩Coding 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档