码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-12-13 23:46:37      阅读:227      评论:0      收藏:0      [点我收藏+]

标签:select   char   har   varchar   div   rom   拼接   weight   font   

--按变量赋值方式
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循环)

标签:select   char   har   varchar   div   rom   拼接   weight   font   

原文地址:http://www.cnblogs.com/win32pro/p/8034266.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!