Showing posts with label object. Show all posts
Showing posts with label object. Show all posts

Monday, March 26, 2012

Resetting DB

> 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
>
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
"Dan Guzman" wrote:

> 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...
>
sql

Resetting DB

> 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
>
> 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]" <gaile@.online.microsoft.com> wrote in message
news:%233GfsyheHHA.3960@.TK2MSFTNGP02.phx.gbl...
> 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...
>
|||> 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
"Dan Guzman" <guzmanda@.nospam-online.sbcglobal.net> wrote in message
news:74A9F97B-3095-471B-A3C7-BF6EE085B1D6@.microsoft.com...
> 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]" <gaile@.online.microsoft.com> wrote in message
> news:%233GfsyheHHA.3960@.TK2MSFTNGP02.phx.gbl...
>

Resetting DB

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
RegardsHello,
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
"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
>|||> 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
>|||> 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
>|||> 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]" <gaile@.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
>>
>|||> 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
"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]" <gaile@.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
>>
>>
>|||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
"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
> >
> >
>

Resetting DB

> 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
>
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
"Dan Guzman" wrote:

> 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...
>

Resetting DB

> 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
>
> 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]" <gaile@.online.microsoft.com> wrote in message
news:%233GfsyheHHA.3960@.TK2MSFTNGP02.phx.gbl...
> 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...
>
|||> 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
"Dan Guzman" <guzmanda@.nospam-online.sbcglobal.net> wrote in message
news:74A9F97B-3095-471B-A3C7-BF6EE085B1D6@.microsoft.com...
> 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]" <gaile@.online.microsoft.com> wrote in message
> news:%233GfsyheHHA.3960@.TK2MSFTNGP02.phx.gbl...
>

Resetting DB

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
RegardsHello,
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
"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
>|||> 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/pr...oads/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
>|||> 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
>|||> 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]" <gaile@.online.microsoft.com> wrote in message
news:%233GfsyheHHA.3960@.TK2MSFTNGP02.phx.gbl...
> 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/pr...oads/books.mspx
> "John" <John@.nospam.infovis.co.uk> wrote in message
> news:%23vTMcTheHHA.1312@.TK2MSFTNGP03.phx.gbl...
>|||> 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/pr...oads/books.mspx
"Dan Guzman" <guzmanda@.nospam-online.sbcglobal.net> wrote in message
news:74A9F97B-3095-471B-A3C7-BF6EE085B1D6@.microsoft.com...
> 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]" <gaile@.online.microsoft.com> wrote in message
> news:%233GfsyheHHA.3960@.TK2MSFTNGP02.phx.gbl...
>|||Just a thought here, but another way, if you're doing this often, might be t
o
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 a
s
needed - though that second option doesn't sound nearly as fast as just abou
t
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
"Dan Guzman" wrote:

> Below is a script that will drop all objects. If the database is not larg
e
> 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...
>sql

Reset-button

I want a reset-button which clear my formular....

I have the following code in mypage.aspx.vb:

Sub Reset(ByVal sender As Object, ByVal e As System.EventArgs)

Me.Server.Transfer("Mypage.aspx") 'reloads the page without postback?

End Sub

And a button on mypage.aspx:

<asp:Button ID="Button2" OnClick="Reset" runat="server" Text="Rensa" />

But when I click the button....the validation-controls executes before...so instead for "clearing" the page (reload it without postback) the validation-messages shows that I have forgott to write in som textboxes....

What should I do?

hi
you use this statement:

If Not Page.IsPostBack Then
< validation statement >
End If

good luck

sql

Monday, March 12, 2012

Request for SqlCeEngine.Exists(), .Delete() methods

Given a connection string, I can create a database using a SqlCeEngine object and call engine.CreateDatabase().

However, there doesn't seem to be a way to determine whether a database exists, given a connection string. I would need to interpret the connection string myself and extract the file name to use File.Exists() etc.

There should be SqlCeEngine methods to test whether a database exists, test whether it's accessible, and to delete it, given a connection string.

Cheers, Oli

It is unlikely that would be added as you can easily do that with simple string manipulations and classes from System.IO – you already know how.

In compact world (where keeping size down is very important) functionality is usually added only if it’s not possible (or very hard) to do or if it’s some very common task. One example would be multiple connections support added in SQL Mobile. Your task is easy to do with existing functionality and it’s not that common – usually connection string is constructed from file name.

In any case you’re welcome to submit a request via Product feedback site: http://connect.microsoft.com/Main/content/content.aspx?ContentID=2220

|||

Hmm...I can get you a work around if you are taking connection string as input.

This should help you!

conn = new SqlCeConnection(inputConnectionString);

if (File.Exists(conn.DataSource))

File.Delete(conn.DataSource);

The important thing to note here is that, Connection Object parses the connection string the moment you assign it (need not call Open). And DataSource property will return the Database File Path.

Thanks,

Laxmi

Saturday, February 25, 2012

repost: SqlServer 2005 => SMS => Object Explorer => Programability Filter questio

SqlServer 2005 => SMS => Object Explorer => Programability Filter => Filter:
Contains _aaa_ shows all stored procs that include the string _aaa_, works
great.
SqlServer 2005 => SMS => Object Explorer => Programability Filter => Filter:
Contains _yyy_ shows all stored procs that include the string _yyy_
How does one structure the "contains" filter to show procs that contain both
_aaa_ AND _yyy_ ?
Still don't have a good feeling for Sql Server tools expressions.
Any help would be greatly appreciated !!!
Barry
in Oregon
I don't think that there is any way to combine filter criteria.
You could execute the following query:
SELECT ROUTINE_NAME
FROM INFORMATION_SCHEMA.ROUTINES
WHERE ( ROUTINE_NAME LIKE '%_aaa_%'
OR ROUTINE_NAME LIKE '%_yyy_%'
)
Arnie Rowland, Ph.D.
Westwood Consulting, Inc
Most good judgment comes from experience.
Most experience comes from bad judgment.
- Anonymous
You can't help someone get up a hill without getting a little closer to the top yourself.
- H. Norman Schwarzkopf
"frostbb" <barry.b.frost@.remove-this-spam-filter.state.or.us> wrote in message news:%23tuas6o$GHA.4024@.TK2MSFTNGP04.phx.gbl...
> SqlServer 2005 => SMS => Object Explorer => Programability Filter => Filter:
> Contains _aaa_ shows all stored procs that include the string _aaa_, works
> great.
> SqlServer 2005 => SMS => Object Explorer => Programability Filter => Filter:
> Contains _yyy_ shows all stored procs that include the string _yyy_
> How does one structure the "contains" filter to show procs that contain both
> _aaa_ AND _yyy_ ?
> Still don't have a good feeling for Sql Server tools expressions.
> Any help would be greatly appreciated !!!
> Barry
> in Oregon
>
|||I don't think that there is any way to combine filter criteria.
You could execute the following query:
SELECT ROUTINE_NAME
FROM INFORMATION_SCHEMA.ROUTINES
WHERE ( ROUTINE_NAME LIKE '%_aaa_%'
OR ROUTINE_NAME LIKE '%_yyy_%'
)
Arnie Rowland, Ph.D.
Westwood Consulting, Inc
Most good judgment comes from experience.
Most experience comes from bad judgment.
- Anonymous
You can't help someone get up a hill without getting a little closer to the top yourself.
- H. Norman Schwarzkopf
"frostbb" <barry.b.frost@.remove-this-spam-filter.state.or.us> wrote in message news:%23tuas6o$GHA.4024@.TK2MSFTNGP04.phx.gbl...
> SqlServer 2005 => SMS => Object Explorer => Programability Filter => Filter:
> Contains _aaa_ shows all stored procs that include the string _aaa_, works
> great.
> SqlServer 2005 => SMS => Object Explorer => Programability Filter => Filter:
> Contains _yyy_ shows all stored procs that include the string _yyy_
> How does one structure the "contains" filter to show procs that contain both
> _aaa_ AND _yyy_ ?
> Still don't have a good feeling for Sql Server tools expressions.
> Any help would be greatly appreciated !!!
> Barry
> in Oregon
>
|||frostbb (barry.b.frost@.remove-this-spam-filter.state.or.us) writes:
> SqlServer 2005 => SMS => Object Explorer => Programability Filter =>
> Filter: Contains _aaa_ shows all stored procs that include the string
> _aaa_, works great.
> SqlServer 2005 => SMS => Object Explorer => Programability Filter =>
> Filter: Contains _yyy_ shows all stored procs that include the string
> _yyy_
> How does one structure the "contains" filter to show procs that contain
> both _aaa_ AND _yyy_ ?
I thought that maybe something like [xy]{xy][xy] would take you half-way
there, but it was too smart for me and escaped the brackets.
If you feel that this would be a valueable feature, pay a visit to
https://connect.microsoft.com/SQLServer/feedback/.
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
|||Erland,
Thanks for the feedback. Its very much appreciated! Also, apologies for
the late acknowledgement.
Good suggestion. I'll ping MS about the issue
https://connect.microsoft.com/SQLServer/feedback/.
I've got a couple hundred stored procs (so far) and the filter is becoming
more and more handy. (we've been migrating our two 'largest' enterprise db's
from UNIX to Sql Server). I name our table management procs to sort by
sub-system / table_name. I also have a number of 'generic tool box' procs
that are accessed by multiple subsystems ... thus the need to display both
_aaa_ and _yyy_ subsystem procs at the same time.
Best Wishes!
Barry
in Oregon
"Erland Sommarskog" <esquel@.sommarskog.se> wrote in message
news:Xns986FEE338BC11Yazorman@.127.0.0.1...
> frostbb (barry.b.frost@.remove-this-spam-filter.state.or.us) writes:
> I thought that maybe something like [xy]{xy][xy] would take you half-way
> there, but it was too smart for me and escaped the brackets.
> If you feel that this would be a valueable feature, pay a visit to
> https://connect.microsoft.com/SQLServer/feedback/.
>
> --
> Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
> Books Online for SQL Server 2005 at
> http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
> Books Online for SQL Server 2000 at
> http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx