动态生成SQL Server视图作业

原创
2012/11/30 13:49
阅读数 238
DECLARE @SQL VARCHAR(8000), @SALETABEL VARCHAR(100)
SET @SQL='ALTER VIEW DBO.SCOST AS '

DECLARE SALELIST CURSOR SCROLL FOR SELECT 'SELECT * FROM SALECOST UNION ALL ' TABLENAME UNION ALL SELECT 'SELECT * FROM '+LTRIM(RTRIM(NAME))+' UNION ALL ' FROM SYSOBJECTS WHERE NAME LIKE 'SALECOST201%' AND XTYPE='U'
OPEN SALELIST
FETCH NEXT FROM SALELIST INTO @SALETABEL
WHILE @@FETCH_STATUS=0
BEGIN
        SET @SQL=@SQL+@SALETABEL
        FETCH NEXT FROM SALELIST INTO @SALETABEL
END
CLOSE SALELIST
DEALLOCATE SALELIST

SET @SQL=SUBSTRING(@SQL,1,LEN(@SQL)-10)
EXEC(@SQL)

展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部