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

边栏推荐
- How dbeaver restores and backs up databases
- 原创翻译 | 机器学习模型服务工具对比:KServe,Seldon Core和BentoML
- 1027 Colors in Mars
- MySQL Express - day 1 - basic introduction
- Yyds dry goods counting SQL sub query
- 灵活的IP网络测试工具——— X-Launch
- Open a new ecological posture | use the wrodpress remote attachment to store it in COS
- Unleash the innovative power of open source database | [Gansu] opengauss meetup has come to a successful conclusion
- 事件相关电位ERP的皮层溯源分析
- Love math experiment | phase VI - Financial anti fraud case study
猜你喜欢

Unity3d button adapts the size according to the text content

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

2021全球独角兽榜发布:中国301家独角兽企业全名单来了!
I haven't thought about the source for some time. After upgrading to the latest version 24, the data encryption problem is repeatedly displayed

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

BTC and eth recapture the lost land! Leading the market recovery? Encryption will enter the "ice age"!
![[STL programming] [common competition] [Part 1]](/img/ce/4d489e62d6c8d16134262b65d4b0d9.png)
[STL programming] [common competition] [Part 1]

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

squid代理服务器

Practice of combining rook CEPH and rainbow, a cloud native storage solution
随机推荐
KDD 2022 | 图“预训练、提示、微调”范式下的图神经网络泛化框架
mime. Type file content
爱数课实验 | 第九期-利用机器学习方法进行健康智能诊断
College graduation thesis management system based on wechat applet graduation design
Shell script controls the startup and shutdown of services - with detailed cases
基于微信小程序的高校党员之家服务管理系统系统小程序#毕业设计,党员,积极分子,学习,打卡,论坛
1030 Travel Plan
Cloud native Security Guide: learn kubernetes attack and defense from scratch
实现字符串MyString
白嫖红队goby&POC,叫你如何白嫖?
OpenSSL client programming: SSL session failure caused by an obscure function
Necessary software tools in embedded software development
How to do a good job of gateway high availability protection in the big promotion scenario
SQL audit platform permission module introduction and account creation tutorial
基于微信小程序的高校毕业论文管理系统#毕业设计
关于企业数字化的展望(38/100)
使用storcli工具配置RAID,收藏这一篇就够了
划重点!国产电脑上安装字体小技巧
Runmaide medical opened the offering: without the participation of cornerstone investors, the amount of loss doubled
抗洪救灾,共克时艰,城联优品驰援英德捐赠爱心物资