循环打印视图(学习WHILE循环)

Posted Li Essay

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了循环打印视图(学习WHILE循环)相关的知识,希望对你有一定的参考价值。

--按变量赋值方式
DECLARE
@ViewName VARCHAR(50) --视图名 DECLARE @RowCount INT = 0 --总视图数 DECLARE @i INT = 0 --循环次数 SELECT @RowCount = COUNT(NAME) FROM sysobjects WHERE xtype = v WHILE @i < @RowCount BEGIN SELECT @ViewName = t1.NAME FROM(SELECT ROW_NUMBER() OVER(ORDER BY NAME) AS RowNum, NAME FROM sysobjects WHERE xtype = v) AS t1 WHERE t1.RowNum = @i PRINT @ViewName SET @i = @i + 1 END

 

--按拼接Sql语句方式
DECLARE @ViewName VARCHAR(50)  --视图名
DECLARE @RowCount INT = 0      --总视图数
DECLARE @i INT = 0               --循环次数
DECLARE @sql VARCHAR(MAX)=‘‘

SELECT @RowCount = COUNT(NAME) FROM sysobjects WHERE  xtype = v

WHILE @i < 10  --@RowCount
BEGIN
    SET @sql = select t1.NAME FROM( SELECT ROW_NUMBER() OVER(ORDER BY NAME) AS RowNum, NAME FROM sysobjects WHERE  xtype = + ‘‘‘v+‘‘‘)t1 WHERE  t1.RowNum = + cast(@i AS VARCHAR)
    EXEC (@sql)
    SET @i = @i + 1
END

 

以上是关于循环打印视图(学习WHILE循环)的主要内容,如果未能解决你的问题,请参考以下文章

for循环

Python学习笔记-流程控制(while循环)

Python学习之[while]循环

“if”和“while”循环中的“continue”关键字

如何在while循环中只打印一次帖子?

python学习之循环