do whileSQL Server 2008中是否有实现循环的方法?
我有现有的记录
ID Hospital ID Email Description
1 15 abc@e.com Sample Description
2 15 def@dd.com Random Text
Run Code Online (Sandbox Code Playgroud)
我需要使用WHILE循环来插入医院ID更改为特定值的行或在这种情况下为32,而其他(不是ID,因为它是自动生成的)保持不变.
它应该看起来像
ID Hospital ID Email Description
1 15 abc@e.com Sample Description
2 15 def@dd.com Random Text
3 32 abc@e.com Sample Description
4 32 def@dd.com Random Text
Run Code Online (Sandbox Code Playgroud)
请注意,上面现在有两个ID与医院ID不同的新行.ID是自动生成的.
我有几个表,我需要进行相同的更新.如果我可以使用while循环执行此操作,我不想使用游标.
编辑 在接受的答案中提供了循环作为更简单的解决方案.
我需要在sql中做这样的事情:
declare @StartDate varchar(10)
declare @EndDate varchar(10)
set @StartDate='12/31/2008'
set @EndDate='1/11/2009'
Declare @date varchar = @StartDate
while (@date <= @EndDate)
begin
-- some statements
set @date += 1 -- basically increment by 1 day
end
Run Code Online (Sandbox Code Playgroud)
如何在SQL中正确执行上述操作?基本上,我的startdate和enddate是字符串而不是日期时间,因为我的业务逻辑在另一个表中引用字符串列,日期作为列的名称 - 但我需要遍历一堆列,每列的名称都是第二天的约会.
如果日期是11/07/2009,那么列的名称将是'11/7/2009'(7中没有0),所以我也要注意这一点.
任何帮助表示赞赏!
谢谢.