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

边栏推荐
- Best practice: optimizing Postgres query performance (Part 2)
- GoLand永久激活
- 如何降低用户关注的非必要页面的权重传递?
- 爱数课实验 | 第六期-金融反欺诈案例研究
- 原创翻译 | 机器学习模型服务工具对比:KServe,Seldon Core和BentoML
- JPA踩坑系列之save方法
- Love math experiment | phase 9 - intelligent health diagnosis using machine learning method
- Squid proxy server
- Cortical traceability analysis of ERP
- Necessary software tools in embedded software development
猜你喜欢

实现字符串MyString

CSDN 技能树使用体验与产品分析(1)

GFS分布式文件系统

释放开源数据库创新力量 | 【甘肃】openGauss Meetup圆满结束

Squid proxy server

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

众昂矿业:新能源或成萤石最大应用领域

Flood fighting and disaster relief, overcoming difficulties, and City United premium products rushed to the aid of Yingde to donate loving materials

squid代理服務器

Very comprehensive dolphin scheduler installation and use documents
随机推荐
NVIDIA three piece environment configuration
Squid proxy server
OpenSSL client programming: SSL session failure caused by an obscure function
众昂矿业:新能源或成萤石最大应用领域
Oracle architecture summary
Data platform scheduling upgrade and transformation | operation practice from Azkaban smooth transition to Apache dolphin scheduler
NVIDIA三件套环境配置
Dictionary tree (review)
MySQL客户端工具推荐,一定想不到最好用巨然是它
Industry case | see the operation of bank digital transformation from the king of retail
元宇宙虚拟数字人离我们更近了|华锐互动
通过CE修改器修改大型网络游戏
It took me 6 months to complete the excellent graduation project of undergraduate course. What have I done?
Cerebral cortex: predicting children's mathematical skills from task state and resting state brain function connections
Recommended practice sharing of Zhilian recruitment based on Nebula graph
非常全面的DolphinScheduler(海豚调度)安装使用文档
CocosCreator播放音频并同步进度
Pycharm common functions - breakpoint debugging
Goldfish rhca memoirs: do447 managing projects and carrying out operations -- creating job templates and starting jobs
How to do a good job of gateway high availability protection in the big promotion scenario