Pages

Tuesday, May 7, 2013

Cursor, drop all tables,views, procedures



USE LRRV6Portal
GO



--SELECT name
--FROM sys.Tables
--GO




  DECLARE @name nvarchar(50); 
   DECLARE tableCursor CURSOR FOR
       SELECT name
FROM sys.Tables
 
  OPEN tableCursor
 
  FETCH NEXT FROM tableCursor into @name
 
  WHILE @@FETCH_STATUS = 0
  BEGIN
  
  
 
         PRINT @name
        
        exec( 'drop table "'+@name+'"');

    FETCH NEXT FROM tableCursor into @name

  end   
  CLOSE tableCursor;
  DEALLOCATE tableCursor;
  go

------------

 DECLARE @name nvarchar(50);
   DECLARE viewCursor CURSOR FOR
       SELECT name
FROM sys.views

  OPEN viewCursor

  FETCH NEXT FROM viewCursor into @name

  WHILE @@FETCH_STATUS = 0
  BEGIN
 
 

         PRINT @name
       
        exec( 'drop view "'+@name+'"');

    FETCH NEXT FROM viewCursor into @name

  end  
  CLOSE viewCursor;
  DEALLOCATE viewCursor;
  go

----------

 DECLARE @name nvarchar(50);
   DECLARE procedCursor CURSOR FOR
       SELECT name
FROM sys.procedures

  OPEN procedCursor

  FETCH NEXT FROM procedCursor into @name

  WHILE @@FETCH_STATUS = 0
  BEGIN
 
 

         PRINT @name
       
        exec( 'drop procedure "'+@name+'"');

    FETCH NEXT FROM procedCursor into @name

  end  
  CLOSE procedCursor;
  DEALLOCATE procedCursor;
  go

No comments:

Post a Comment