SQL SERVER 游标删除无数据试图

Declare @SQLToken varchar( max)
set @SQLToken   = ''
declare hCForEachTable cursor global for 
select '[' + REPLACE( schema_name(syso.schema_id), N ']', N ']]') + ']' + '.'  + '[' + REPLACE(object_name(o.id), N ']', N ']]') + ']' 
from dbo.sysobjects o
join sys.all_objects syso 
    on o.id = syso.object_id
where OBJECTPROPERTY(o.id, N 'IsVIEW') = 1 and o.category & 2 = 0 

OPEN hCForEachTable;
Declare @tablename varchar( max)
Declare @ Temp varchar( max)
FETCH NEXT FROM hCForEachTable;
WHILE @@FETCH_STATUS = 0
BEGIN
     FETCH  NEXT FROM hCForEachTable into @tablename;
     set @ Temp   = 'if not exists(select 1 from ' +@tablename + ') drop view ' +@tablename + ';'
     set @SQLToken = @SQLToken + @ Temp
     set @ Temp = ''
     set @tablename = ''
END;
SELECT @SQLToken
CLOSE hCForEachTable;
DEALLOCATE hCForEachTable;





你可能感兴趣的:(sql,server)