当前位置:网站首页>SQL server for circular usage
SQL server for circular usage
2022-06-27 21:16:00 【Ink Sky Wheel】

A project needs to be modified recently , involves SqlServer Database knowledge , Sort it out and share it with you , Old rules , Post the example first , Finally, grammar :
preparation , simulation 2012-2018 Annual order data :
First from tb_Detail In the order detail table, find out which one belongs to 2012 Years of data ( Remarks yes , Deliberately simulated ), Find out OrderID:
select OrderID from tb_Detail where Remark = "2012 For annual data ";

Re pass OrderID modify tb_OrderInfo Each year in the commodity order table :
update tb_OrderInfo set OrderDate="2012-05-31 11:27:37.123" where OrderID in (select OrderID from tb_Detail where Remark = "2012 For annual data ");

Checked the whole 2012 The annual data is OK (select OrderID from tb_OrderInfo where YEAR(OrderDate) =2012;) Consider using for Circulated :

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))+" For annual data ")
set @[email protected]+1
end
Be careful for In the loop cast(@min as varchar(10)) Field , This is because we declare that @min A variable is int Type of , And then there's “ For annual data ” And what we are looking for Remark The fields are vachar Type of , So we need to int Type to varchar, Otherwise, the following mistakes will occur :

in addition , If you type it from the command line , Pay attention to copy and paste SQL Line breaks in statements .
Execute as shown in the figure for loop :

Finally, test the data , adopt , Oh yeah \(^o^)/!!

for Introduction to loop syntax :
-- ╔════════╗
-- =============================== ║ if Examples of statement usage ║
-- ╚════════╝
declare @a int
set @a=12
if @a>100
begin
print @a
end
else
begin
print 'no'
end
-- ╔══════════╗
-- =============================== ║ while Examples of statement usage ║
-- ╚══════════╝
declare @i int
set @i=1
while @i<30
begin
insert into test (userid) values(@i)
set @[email protected]+1
end
-- Set repeat execution SQL The condition of a statement or statement block . As long as the specified condition is true , Just repeat the statement . have access to BREAK and CONTINUE Keywords are controlled inside the loop WHILE The execution of a statement in a loop . This article was obtained from the Internet before !
-- ╔════════╗
-- ================================ ║ Temporary tables and try ║
-- ╚════════╝
-- Add temporary table
select * into #csj_temp from csj
-- Delete temporary table be used try
begin try -- Detection code starts
drop table #csj_temp
end try
begin catch -- The mistake started
end catch
-- ╔═════════╗
-- =============================== ║ The cursor loops through the record ║
-- ╚═════════╝
declare @temp_temp int
--declare @Cur_Name
[email protected]_Name="aaa"
--------------------------------- Create cursors --Local( Local Cursor )
DECLARE aaa CURSOR for select House_Id from House_House where Deleted=0 or deleted is null
----------------------------------- Open cursor
Open aaa
----------------------------------- Traversing and getting cursors
fetch next from aaa into @temp_temp
--print @temp_temp
while @@fetch_status=0
begin
-- Do what you want to do
select * from House_monthEnd where [email protected]_temp
fetch next from aaa into @temp_temp -- Values are assigned to variables
--
end
----------------------------------- Close cursor
Close aaa
----------------------------------- Delete cursor
Deallocate aaa
--
Reference article :
for The realization of the cycle :
https://zhidao.baidu.com/question/72824226.html
https://zhidao.baidu.com/question/82332450.html
https://www.cnblogs.com/xielong/p/5941595.html
SqlServer Type splicing problem :
https://zhidao.baidu.com/question/2073884979869176588.html
Finally, I feel :
It is also used in recent projects SqlServer Knowledge , I thought that the database teacher talked about this in college , I got the exam for Circulation? , Ha ha ha , But now it's all forgotten , Turn over the previous courseware , You can still pick up the information , Thanks to all the teachers in the University !

边栏推荐
- 原创翻译 | 机器学习模型服务工具对比:KServe,Seldon Core和BentoML
- 一场分销裂变活动,不止是发发朋友圈这么简单
- Installation and configuration of grayog new generation log collection early warning system
- 爱数课实验 | 第九期-利用机器学习方法进行健康智能诊断
- Openharmony hisysevent dotting and calling practice of # Summer Challenge (L2)
- Flexible IP network test tool -- x-launch
- 释放开源数据库创新力量 | 【甘肃】openGauss Meetup圆满结束
- 事件相关电位ERP的皮层溯源分析
- GFS distributed file system
- Open a new ecological posture | use the wrodpress remote attachment to store it in COS
猜你喜欢

MYSQL 性能优化 index 函数,隐藏,前缀,hash 索引 使用方法(2)

Oracle architecture summary

#夏日挑战赛# OpenHarmony HiSysEvent打点调用实践(L2)

Oracle的CTAS能不能将约束等属性带到新表?

A distribution fission activity is more than just a circle of friends

Wechat applet based service management system for college party members' Home System applet graduation design, Party members, activists, learning, punch in, forum

基于微信小程序的高校党员之家服务管理系统系统小程序#毕业设计,党员,积极分子,学习,打卡,论坛

动物养殖生产虚拟仿真教学系统|华锐互动

Industry case | see the operation of bank digital transformation from the king of retail

花了6个月时间完成本科优秀毕业设计,我做了什么?
随机推荐
实际工作中用到的shell命令 - sed
教程|fNIRS数据处理工具包Homer2下载与安装
Practice of combining rook CEPH and rainbow, a cloud native storage solution
GFS分布式文件系统
Leetcode 989. Integer addition in array form (simple)
1029 Median
Experience Navicat premium 16, unlimited reset, 14 day trial method (with source code)
At 19:00 on Tuesday evening, the 8th live broadcast of battle code Pioneer - how to participate in openharmony's open source contribution in multiple directions
#夏日挑战赛# OpenHarmony HiSysEvent打点调用实践(L2)
GFS distributed file system
Cerebral cortex: predicting children's mathematical skills from task state and resting state brain function connections
OpenSSL client programming: SSL session failure caused by an obscure function
How to reduce the weight transfer of unnecessary pages that users pay attention to?
爱数课实验 | 第八期-新加坡房价预测模型构建
SQL Server for循环用法
MySQL Express - day 1 - basic introduction
众昂矿业:新能源或成萤石最大应用领域
shell脚本控制服务的启动和关闭 - 具备详细案例
Model reasoning acceleration based on tensorrt
分享|智慧环保-生态文明信息化解决方案(附PDF)