当前位置:网站首页>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 !

边栏推荐
- Serveur mandataire SQUID
- Ble Bluetooth module nrf518/nrf281/nrf528/nrf284 chip scheme comparison
- CocosCreator播放音频并同步进度
- Love math experiment | phase VI - Financial anti fraud case study
- Character interception triplets of data warehouse: substrb, substr, substring
- BTC and eth recapture the lost land! Leading the market recovery? Encryption will enter the "ice age"!
- 基于 TensorRT 的模型推理加速
- [STL programming] [common competition] [Part 2]
- 关于企业数字化的展望(38/100)
- 动物养殖生产虚拟仿真教学系统|华锐互动
猜你喜欢

基于微信小程序的高校毕业论文管理系统#毕业设计

Serveur mandataire SQUID

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

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

GFS分布式文件系统

KDD 2022 | 图“预训练、提示、微调”范式下的图神经网络泛化框架

On the drawing skills of my writing career

【STL编程】【竞赛常用】【part 2】

How dbeaver restores and backs up databases

SQL Server for循环用法
随机推荐
Flutter隐藏AppBar的返回按钮
CocosCreator播放音频并同步进度
MySQL速成——第一天--基础入门
This is the same as data collection. Can you define a parameter as last month or the previous day, and then use this parameter in SQL?
本周二晚19:00战码先锋第8期直播丨如何多方位参与OpenHarmony开源贡献
Share an experience of self positioning + problem solving
[STL programming] [common competition] [Part 2]
爱数课实验 | 第七期-基于随机森林的金融危机分析
1029 Median
开启生态新姿势 | 使用 WrodPress 远程附件存储到 COS
Love math experiment | phase 9 - intelligent health diagnosis using machine learning method
VMware vSphere ESXi 7.0安装教程
基于微信小程序的高校党员之家服务管理系统系统小程序#毕业设计,党员,积极分子,学习,打卡,论坛
JPA踩坑系列之save方法
Record a failure caused by a custom redis distributed lock
Cerebral Cortex:从任务态和静息态脑功能连接预测儿童数学技能
【STL编程】【竞赛常用】【part 2】
爱数课实验 | 第五期-基于机器学习方法的商品评论情感判定
Mongodb introduction and typical application scenarios
Love math experiment | phase VI - Financial anti fraud case study