Home All Groups Group Topic Archive Search About
Author
8 Apr 2007 7:34 PM
John
Hi

Is there an easy way to drop all object of a db so it is as new? Just so I
can run a script to recreate the objects.

Thanks

Regards

Author
8 Apr 2007 8:26 PM
Hari Prasad
Hello,

In the Generate SQL Script, Script all objects with drop objects. While
executing the script on the database will drop and recreate all objects.
FYI, This will clear off all
the data, so if you need data please backup the database.

Thanks
Hari


Show quote
"John" <John@nospam.infovis.co.uk> wrote in message
news:%23vTMcTheHHA.1312@TK2MSFTNGP03.phx.gbl...
> Hi
>
> Is there an easy way to drop all object of a db so it is as new? Just so I
> can run a script to recreate the objects.
>
> Thanks
>
> Regards
>
>
Author
8 Apr 2007 8:29 PM
Gail Erickson [MS]
> Is there an easy way to drop all object of a db so it is as new? Just so I
> can run a script to recreate the objects

Is there some reason why you can't add DROP DATABASE <database_name>  and
CREATE DATABASE <database_name> statements to your script?

--
Gail Erickson [MS]
SQL Server Documentation Team
This posting is provided "AS IS" with no warranties, and confers no rights
Download the latest version of Books Online from
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx

Show quote
"John" <John@nospam.infovis.co.uk> wrote in message
news:%23vTMcTheHHA.1312@TK2MSFTNGP03.phx.gbl...
> Hi
>
> Is there an easy way to drop all object of a db so it is as new? Just so I
> can run a script to recreate the objects.
>
> Thanks
>
> Regards
>
>
Author
9 Apr 2007 3:29 AM
Dan Guzman
> Is there some reason why you can't add DROP DATABASE <database_name>  and
> CREATE DATABASE <database_name> statements to your script?

I can't speak for John but I have had large SQL 2000 databases where I found
it considerably faster to drop all objects rather than dropping and
recreating the entire database.  However, in the SQL 2005 world, the
database drop/create method is very fast thanks to instant file
initialization.

--
Hope this helps.

Dan Guzman
SQL Server MVP

Show quote
"Gail Erickson [MS]" <ga***@online.microsoft.com> wrote in message
news:%233GfsyheHHA.3960@TK2MSFTNGP02.phx.gbl...
>> Is there an easy way to drop all object of a db so it is as new? Just so
>> I can run a script to recreate the objects
>
> Is there some reason why you can't add DROP DATABASE <database_name>  and
> CREATE DATABASE <database_name> statements to your script?
>
> --
> Gail Erickson [MS]
> SQL Server Documentation Team
> This posting is provided "AS IS" with no warranties, and confers no rights
> Download the latest version of Books Online from
> http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
>
> "John" <John@nospam.infovis.co.uk> wrote in message
> news:%23vTMcTheHHA.1312@TK2MSFTNGP03.phx.gbl...
>> Hi
>>
>> Is there an easy way to drop all object of a db so it is as new? Just so
>> I can run a script to recreate the objects.
>>
>> Thanks
>>
>> Regards
>>
>>
>
>
Author
9 Apr 2007 6:41 PM
Gail Erickson [MS]
> I can't speak for John but I have had large SQL 2000 databases where I
> found it considerably faster to drop all objects rather than dropping and
> recreating the entire database.  However, in the SQL 2005 world, the
> database drop/create method is very fast thanks to instant file
> initialization.

Good point. I wasn't thinking along those lines. Thanks for including this
information.
Gail

--
Gail Erickson [MS]
SQL Server Documentation Team
This posting is provided "AS IS" with no warranties, and confers no rights
Download the latest version of Books Online from
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Show quote
"Dan Guzman" <guzmanda@nospam-online.sbcglobal.net> wrote in message
news:74A9F97B-3095-471B-A3C7-BF6EE085B1D6@microsoft.com...
>> Is there some reason why you can't add DROP DATABASE <database_name>  and
>> CREATE DATABASE <database_name> statements to your script?
>
> I can't speak for John but I have had large SQL 2000 databases where I
> found it considerably faster to drop all objects rather than dropping and
> recreating the entire database.  However, in the SQL 2005 world, the
> database drop/create method is very fast thanks to instant file
> initialization.
>
> --
> Hope this helps.
>
> Dan Guzman
> SQL Server MVP
>
> "Gail Erickson [MS]" <ga***@online.microsoft.com> wrote in message
> news:%233GfsyheHHA.3960@TK2MSFTNGP02.phx.gbl...
>>> Is there an easy way to drop all object of a db so it is as new? Just so
>>> I can run a script to recreate the objects
>>
>> Is there some reason why you can't add DROP DATABASE <database_name>  and
>> CREATE DATABASE <database_name> statements to your script?
>>
>> --
>> Gail Erickson [MS]
>> SQL Server Documentation Team
>> This posting is provided "AS IS" with no warranties, and confers no
>> rights
>> Download the latest version of Books Online from
>> http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
>>
>> "John" <John@nospam.infovis.co.uk> wrote in message
>> news:%23vTMcTheHHA.1312@TK2MSFTNGP03.phx.gbl...
>>> Hi
>>>
>>> Is there an easy way to drop all object of a db so it is as new? Just so
>>> I can run a script to recreate the objects.
>>>
>>> Thanks
>>>
>>> Regards
>>>
>>>
>>
>>
>
Author
9 Apr 2007 3:26 AM
Dan Guzman
> Is there an easy way to drop all object of a db so it is as new? Just so I
> can run a script to recreate the objects.

Below is a script that will drop all objects.  If the database is not large
or you are using SQL 2005, you might find it easy to simply drop and
recreate the database.


IF DB_NAME() IN ('master', 'msdb', 'model', 'distribution')
BEGIN
RAISERROR('Not for use on system databases', 16, 1)
GOTO Done
END

--Drop objects from current the database
SET NOCOUNT ON

DECLARE @DropStatement nvarchar(4000)
DECLARE @SequenceNumber int
DECLARE @LastError int
DECLARE @TablesDropped int

DECLARE DropStatements CURSOR
    LOCAL FAST_FORWARD READ_ONLY FOR
--views
SELECT
    1 AS SequenceNumber,
    N'DROP VIEW ' +
        QUOTENAME(TABLE_SCHEMA) +
        N'.' +
        QUOTENAME(TABLE_NAME) AS DropStatement
FROM
    INFORMATION_SCHEMA.TABLES
WHERE
    TABLE_TYPE = N'VIEW' AND
    OBJECTPROPERTY(
        OBJECT_ID(QUOTENAME(TABLE_SCHEMA) +
        N'.' +
        QUOTENAME(TABLE_NAME)),
        'IsSchemaBound') = 1 AND
    OBJECTPROPERTY(
        OBJECT_ID(QUOTENAME(TABLE_SCHEMA) +
        N'.' +
        QUOTENAME(TABLE_NAME)),
        'IsMSShipped') = 0
UNION ALL
--procedures and functions
SELECT
    2 AS SequenceNumber,
    N'DROP PROCEDURE ' +
        QUOTENAME(ROUTINE_SCHEMA) +
        N'.' +
        QUOTENAME(ROUTINE_NAME) AS DropStatement
FROM
    INFORMATION_SCHEMA.ROUTINES
WHERE
    ROUTINE_TYPE = N'FUNCTION' AND
    OBJECTPROPERTY(
        OBJECT_ID(QUOTENAME(ROUTINE_SCHEMA) +
        N'.' +
        QUOTENAME(ROUTINE_NAME)),
        'IsSchemaBound') = 1 AND
    OBJECTPROPERTY(
        OBJECT_ID(QUOTENAME(ROUTINE_SCHEMA) +
        N'.' +
       QUOTENAME(ROUTINE_NAME)),
        'IsMSShipped') = 0
UNION ALL
--foreign keys
SELECT
    3 AS SequenceNumber,
    N'ALTER TABLE ' +
        QUOTENAME(TABLE_SCHEMA) +
        N'.' +
        QUOTENAME(TABLE_NAME) +
        N' DROP CONSTRAINT ' +
        CONSTRAINT_NAME AS DropStatement
FROM
    INFORMATION_SCHEMA.TABLE_CONSTRAINTS
WHERE
    CONSTRAINT_TYPE = N'FOREIGN KEY'
UNION ALL
--tables
SELECT
    4 AS SequenceNumber,
    N'DROP TABLE ' +
        QUOTENAME(TABLE_SCHEMA) +
        N'.' +
        QUOTENAME(TABLE_NAME) AS DropStatement
FROM
    INFORMATION_SCHEMA.TABLES
WHERE
    TABLE_TYPE = N'BASE TABLE' AND
    OBJECTPROPERTY(
        OBJECT_ID(QUOTENAME(TABLE_SCHEMA) +
        N'.' +
        QUOTENAME(TABLE_NAME)),
        'IsMSShipped') = 0
ORDER BY SequenceNumber

OPEN DropStatements
WHILE 1 = 1
BEGIN
    FETCH NEXT FROM DropStatements INTO @SequenceNumber, @DropStatement
    IF @@FETCH_STATUS = -1 BREAK
    BEGIN
        RAISERROR('%s', 0, 1, @DropStatement) WITH NOWAIT
        --EXECUTE sp_ExecuteSQL @DropStatement
        SET @LastError = @@ERROR
        IF @LastError > 0
        BEGIN
        RAISERROR('Script terminated due to unexpected error', 16, 1)
        GOTO Done
    END
    END
END
CLOSE DropStatements
DEALLOCATE DropStatements

Done:

GO

--
Hope this helps.

Dan Guzman
SQL Server MVP

Show quote
"John" <John@nospam.infovis.co.uk> wrote in message
news:%23vTMcTheHHA.1312@TK2MSFTNGP03.phx.gbl...
> Hi
>
> Is there an easy way to drop all object of a db so it is as new? Just so I
> can run a script to recreate the objects.
>
> Thanks
>
> Regards
>
>
Author
10 Apr 2007 7:00 PM
Dale
Just a thought here, but another way, if you're doing this often, might be to
make a backup of the empty database and restore it as needed or to make a
backup of the database files while the database is empty and detach/attach as
needed - though that second option doesn't sound nearly as fast as just about
any of the others but it, like the first option here, would give you clean
log files and database files.

Dale
--
Dale Preston
MCAD C#
MCSE, MCDBA


Show quote
"Dan Guzman" wrote:

> > Is there an easy way to drop all object of a db so it is as new? Just so I
> > can run a script to recreate the objects.
>
> Below is a script that will drop all objects.  If the database is not large
> or you are using SQL 2005, you might find it easy to simply drop and
> recreate the database.
>
>
> IF DB_NAME() IN ('master', 'msdb', 'model', 'distribution')
> BEGIN
>  RAISERROR('Not for use on system databases', 16, 1)
>  GOTO Done
> END
>
> --Drop objects from current the database
> SET NOCOUNT ON
>
> DECLARE @DropStatement nvarchar(4000)
> DECLARE @SequenceNumber int
> DECLARE @LastError int
> DECLARE @TablesDropped int
>
> DECLARE DropStatements CURSOR
>     LOCAL FAST_FORWARD READ_ONLY FOR
> --views
> SELECT
>     1 AS SequenceNumber,
>     N'DROP VIEW ' +
>         QUOTENAME(TABLE_SCHEMA) +
>         N'.' +
>         QUOTENAME(TABLE_NAME) AS DropStatement
> FROM
>     INFORMATION_SCHEMA.TABLES
> WHERE
>     TABLE_TYPE = N'VIEW' AND
>     OBJECTPROPERTY(
>         OBJECT_ID(QUOTENAME(TABLE_SCHEMA) +
>         N'.' +
>         QUOTENAME(TABLE_NAME)),
>         'IsSchemaBound') = 1 AND
>     OBJECTPROPERTY(
>         OBJECT_ID(QUOTENAME(TABLE_SCHEMA) +
>         N'.' +
>         QUOTENAME(TABLE_NAME)),
>         'IsMSShipped') = 0
> UNION ALL
> --procedures and functions
> SELECT
>     2 AS SequenceNumber,
>     N'DROP PROCEDURE ' +
>         QUOTENAME(ROUTINE_SCHEMA) +
>         N'.' +
>         QUOTENAME(ROUTINE_NAME) AS DropStatement
> FROM
>     INFORMATION_SCHEMA.ROUTINES
> WHERE
>     ROUTINE_TYPE = N'FUNCTION' AND
>     OBJECTPROPERTY(
>         OBJECT_ID(QUOTENAME(ROUTINE_SCHEMA) +
>         N'.' +
>         QUOTENAME(ROUTINE_NAME)),
>         'IsSchemaBound') = 1 AND
>     OBJECTPROPERTY(
>         OBJECT_ID(QUOTENAME(ROUTINE_SCHEMA) +
>         N'.' +
>        QUOTENAME(ROUTINE_NAME)),
>         'IsMSShipped') = 0
> UNION ALL
> --foreign keys
> SELECT
>     3 AS SequenceNumber,
>     N'ALTER TABLE ' +
>         QUOTENAME(TABLE_SCHEMA) +
>         N'.' +
>         QUOTENAME(TABLE_NAME) +
>         N' DROP CONSTRAINT ' +
>         CONSTRAINT_NAME AS DropStatement
> FROM
>     INFORMATION_SCHEMA.TABLE_CONSTRAINTS
> WHERE
>     CONSTRAINT_TYPE = N'FOREIGN KEY'
> UNION ALL
> --tables
> SELECT
>     4 AS SequenceNumber,
>     N'DROP TABLE ' +
>         QUOTENAME(TABLE_SCHEMA) +
>         N'.' +
>         QUOTENAME(TABLE_NAME) AS DropStatement
> FROM
>     INFORMATION_SCHEMA.TABLES
> WHERE
>     TABLE_TYPE = N'BASE TABLE' AND
>     OBJECTPROPERTY(
>         OBJECT_ID(QUOTENAME(TABLE_SCHEMA) +
>         N'.' +
>         QUOTENAME(TABLE_NAME)),
>         'IsMSShipped') = 0
> ORDER BY SequenceNumber
>
> OPEN DropStatements
> WHILE 1 = 1
> BEGIN
>     FETCH NEXT FROM DropStatements INTO @SequenceNumber, @DropStatement
>     IF @@FETCH_STATUS = -1 BREAK
>     BEGIN
>         RAISERROR('%s', 0, 1, @DropStatement) WITH NOWAIT
>         --EXECUTE sp_ExecuteSQL @DropStatement
>         SET @LastError = @@ERROR
>         IF @LastError > 0
>         BEGIN
>         RAISERROR('Script terminated due to unexpected error', 16, 1)
>         GOTO Done
>     END
>     END
> END
> CLOSE DropStatements
> DEALLOCATE DropStatements
>
> Done:
>
> GO
>
> --
> Hope this helps.
>
> Dan Guzman
> SQL Server MVP
>
> "John" <John@nospam.infovis.co.uk> wrote in message
> news:%23vTMcTheHHA.1312@TK2MSFTNGP03.phx.gbl...
> > Hi
> >
> > Is there an easy way to drop all object of a db so it is as new? Just so I
> > can run a script to recreate the objects.
> >
> > Thanks
> >
> > Regards
> >
> >
>

AddThis Social Bookmark Button