Click here to Skip to main content
15,891,423 members
Home / Discussions / Database
   

Database

 
GeneralTimeout Exception [modified] Pin
Vimalsoft(Pty) Ltd23-Apr-08 23:25
professionalVimalsoft(Pty) Ltd23-Apr-08 23:25 
GeneralRe: Timeout Exception Pin
Mike Dimmick24-Apr-08 4:15
Mike Dimmick24-Apr-08 4:15 
GeneralRe: Timeout Exception Pin
Vimalsoft(Pty) Ltd24-Apr-08 20:09
professionalVimalsoft(Pty) Ltd24-Apr-08 20:09 
GeneralRe: Timeout Exception Pin
GuyThiebaut24-Apr-08 5:52
professionalGuyThiebaut24-Apr-08 5:52 
GeneralRe: Timeout Exception Pin
Vimalsoft(Pty) Ltd24-Apr-08 20:03
professionalVimalsoft(Pty) Ltd24-Apr-08 20:03 
GeneralDropping all Tables Pin
Broken Bokken23-Apr-08 16:10
Broken Bokken23-Apr-08 16:10 
GeneralRe: Dropping all Tables Pin
Harini N K23-Apr-08 20:44
Harini N K23-Apr-08 20:44 
GeneralRe: Dropping all Tables Pin
Broken Bokken24-Apr-08 2:25
Broken Bokken24-Apr-08 2:25 
I used your solution, however, you must drop the parent tables first. The Child tables are the ones that error when you try to drop them. Here is the code in case anyone else wants to know how to do this. Just change {DatabaseName} to the name of the database you want to drop all the tables in.

SQL
USE {DatabaseName}
SET NOCOUNT ON

DECLARE @Message	varchar (1000)
DECLARE @TableName	varchar (100)
DECLARE @Count int

--############################################################################################
--		Drop Entities
--############################################################################################


---############### drop related entities first ###############---
SELECT @Count = Count(c.parent_object_id) 
	FROM sys.foreign_key_columns c
	INNER JOIN sys.columns a ON a.object_id = c.referenced_object_id 
	AND a.column_id = c.referenced_column_id	

---loop for tertiary or greater relationships
WHILE @Count > 0
BEGIN
	
	DECLARE tableCursor cursor FOR 
		SELECT OBJECT_NAME(c.parent_object_id) 
		FROM sys.foreign_key_columns c
		INNER JOIN sys.columns a ON a.object_id = c.referenced_object_id 
		AND a.column_id = c.referenced_column_id

	OPEN tableCursor

	FETCH NEXT FROM tableCursor INTO @TableName

	WHILE @@FETCH_STATUS = 0
	BEGIN
		BEGIN TRY
			EXEC ('DROP TABLE ' + @TableName)
			PRINT '--- Dropped Table ' + @TableName + ' ---'
		END TRY
		BEGIN CATCH
		--eat any errors
		END CATCH

		FETCH NEXT FROM tableCursor INTO @TableName
	END

	CLOSE tableCursor
	DEALLOCATE tableCursor

SELECT @Count = Count(c.parent_object_id) 
	FROM sys.foreign_key_columns c
	INNER JOIN sys.columns a ON a.object_id = c.referenced_object_id 
	AND a.column_id = c.referenced_column_id
END

---############### drop the rest of the tables once the relationships have been dropped ###############---
SELECT @Count = COUNT(*) FROM sys.Tables WHERE name NOT LIKE 'sys%'

WHILE @Count > 0
BEGIN
		--only parents left, so drop using msforeachtable
		EXEC sp_MSforeachtable "DROP TABLE ? PRINT '--- Dropped Table ? ---' ";	

		SELECT @Count = COUNT(*) FROM sys.Tables WHERE name NOT LIKE 'sys%'
END

IF @Count = 0
BEGIN
	SET @Message = '--- Tables dropped successfully ---' 
END
ELSE
BEGIN
	SET @Message = '--- Error dropping tables ---'
END

PRINT @Message
GO


Broken Bokken

You can't carry out a ninja-style assasination dressed as an astronaut. It's the luminous fabric; too visible. - Tripod

http://www.brokenbokken.com

GeneralMissing table in SQL Server Pin
blakey40422-Apr-08 22:59
blakey40422-Apr-08 22:59 
GeneralRe: Missing table in SQL Server Pin
Marek Grzenkowicz22-Apr-08 23:36
Marek Grzenkowicz22-Apr-08 23:36 
GeneralRe: Missing table in SQL Server Pin
blakey40422-Apr-08 23:49
blakey40422-Apr-08 23:49 
GeneralRe: Missing table in SQL Server Pin
Marek Grzenkowicz23-Apr-08 1:27
Marek Grzenkowicz23-Apr-08 1:27 
AnswerRe: Missing table in SQL Server Pin
blakey40423-Apr-08 1:55
blakey40423-Apr-08 1:55 
GeneralConnectivity with SQl database Pin
Sankalp Verma22-Apr-08 22:56
Sankalp Verma22-Apr-08 22:56 
GeneralRe: Connectivity with SQl database Pin
Marek Grzenkowicz22-Apr-08 23:37
Marek Grzenkowicz22-Apr-08 23:37 
GeneralRe: Connectivity with SQl database Pin
Broken Bokken24-Apr-08 3:01
Broken Bokken24-Apr-08 3:01 
Generaltable-value function from ADO Pin
Green Fuze22-Apr-08 22:39
Green Fuze22-Apr-08 22:39 
AnswerRe: table-value function from ADO [modified] Pin
andyharman23-Apr-08 23:19
professionalandyharman23-Apr-08 23:19 
GeneralRe: table-value function from ADO Pin
Mark J. Miller24-Apr-08 10:17
Mark J. Miller24-Apr-08 10:17 
GeneralSlow running Insert/Update query for a specific db Pin
Maira K22-Apr-08 19:32
Maira K22-Apr-08 19:32 
GeneralRe: Slow running Insert/Update query for a specific db Pin
Ashfield22-Apr-08 21:21
Ashfield22-Apr-08 21:21 
GeneralRe: Slow running Insert/Update query for a specific db Pin
Maira K22-Apr-08 22:38
Maira K22-Apr-08 22:38 
GeneralRe: Slow running Insert/Update query for a specific db Pin
Ashfield23-Apr-08 2:21
Ashfield23-Apr-08 2:21 
GeneralInput string was not in a correct format Pin
chaitanya_dec2422-Apr-08 18:57
chaitanya_dec2422-Apr-08 18:57 
GeneralRe: Input string was not in a correct format Pin
Colin Angus Mackay22-Apr-08 23:42
Colin Angus Mackay22-Apr-08 23:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.