SQL中类似For循环处理的实例

数据库 SQL Server
对SQL数据库的循环操作可以有多种方法,下面就将为您介绍SQL中类似For循环处理的实例,供您参考,希望对您有所启迪。

 原本CreatDate的yyyy-MM-dd hh:mm:ss:fff格式被变成了yyyy-MM-dd格式,下面就将对SQL进行类似For循环处理,该方法供您参考,希望对您学习SQL中的For循环能有所帮助。

 

  1. declare @itemnumber int --定义需要循环的次数  
  2.  declare @tagint int --定义标志字段,用于结束循环  
  3.  set @tagint=1 
  4.  select @itemnumber = count(distinct Creater) from Demo_TestTable where isnull(Creater,'')<>'' And   
  5.    DATEDIFF(DAY,CreatDate,GETDATE())<1 
  6.    if(@itemnumber>0)  
  7.    begin  
  8.      while @tagint<=@itemnumber  
  9.          begin  
  10.               waitfor delay '00:00:01' --每隔一秒再执行 可用参数变量替换  
  11.              Update Demo_TestTable set CreatDate=GETDATE() where Creater =(  
  12.              Select Creater from (  
  13.                  select Creater,ROW_NUMBER() over(order by Creater) as RowID from Demo_TestTable where   
  14.             isnull(Creater,'')<>'' And DATEDIFF(DAY,CreatDate,GETDATE())<1 group by Creater  
  15.              ) TableA  
  16.               where  TableA.RowID=@tagint  
  17.               )  
  18.               set @tagint=@tagint+1  
  19.         end  
  20.    end  

 

 

 

 

【编辑推荐】

对存储过程代替SQL语句的讨论

SQL聚合函数之Avg 函数

SQL中MAX()和MIN()函数的使用

SQL中求和函数SUM()的应用实例

TOP字句加SQL变量的相关问题

责任编辑:段燃 来源: 互联网
相关推荐

2010-09-10 14:33:32

SQL循环语句

2010-09-09 16:34:19

SQL循环while

2010-09-09 16:10:57

sql server2循环

2010-09-08 17:15:45

SQL循环结构

2010-09-10 09:38:36

SQL系统变量

2010-07-12 15:07:05

SQL Server实

2010-09-10 13:24:45

SQL求和函数

2022-01-16 08:00:28

PythonFor循环

2022-05-12 23:38:19

SQL数据库字符串

2010-09-24 18:03:38

SQL中EXISTS

2010-03-09 14:10:13

Python循环语句

2010-10-08 09:52:18

JavaScript匿

2010-06-28 14:13:18

SQL Server实

2020-11-19 15:26:36

SQLPandas代码

2010-09-08 16:26:26

SQL循环语句

2013-01-04 16:17:33

Android开发图像特效图像处理

2010-09-10 15:37:44

SQL函数

2010-09-26 16:51:03

SQL Server查

2009-08-28 16:37:32

C# for循环

2023-12-13 12:41:59

原生CSS元素
点赞
收藏

51CTO技术栈公众号