请问:Sql中有循环语句吗?

brownhwy 2014-03-19 10:42:33
类似 Do While 或者 For 什么的,如果有的话,格式该怎么写?
...全文
368 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
brownhwy 2014-03-19
  • 打赏
  • 举报
回复
多谢各位大侠的回复,是我写错了,我在while前面多加了个Do,怎么都不对。
發糞塗牆 2014-03-19
  • 打赏
  • 举报
回复
IF/ELSE是流程控制,不能算入循环
發糞塗牆 2014-03-19
  • 打赏
  • 举报
回复
循环用while,还有一种类似循环的是游标。sqlsever的话联机丛书都有案例
  • 打赏
  • 举报
回复
while,还有if else



declare @temp table(schematable varchar(100))

declare @tablename varchar(100)

insert into @temp
(schematable)
select s.name + '.' + t.name
from sys.tables t
inner join sys.schemas s
        on t.schema_id = s.schema_id
where t.type_desc = 'User_table'

--while循环结构
while (select COUNT(*) from @temp ) > 0
begin
	
	select top (1) @tablename = schematable
	from @temp
	
	exec sp_spaceused @tablename;  --输出当前数据库中每个表的空间使用情况
	
	delete from @temp
	where schematable = @tablename
	
	--if-else分支结构
	if( (select COUNT(*) from @temp)=0 )
	begin
	    
	    --延迟10秒
	    waitfor delay '00:00:10'
	    
		exec sp_spaceused;         --输出当前数据库的空间使用情况
        break;
    end
    else
    begin
        continue;
    end
end
  • 打赏
  • 举报
回复
sql server用t-sql,比如while: while @i <=20 begin 你的语句 end
--小F-- 2014-03-19
  • 打赏
  • 举报
回复
--while,continue,break用法

declare @i int
set @i=1
while @i<20
begin
 set @i=@i+1
 if @i<=19
 continue
 print @i
end

declare @i int
set @i=1
while @i<20
begin
 if @i=19
 break
 print @i
 set @i=@i+1
end

34,575

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧