Showing posts with label constraints. Show all posts
Showing posts with label constraints. Show all posts

Thursday, March 29, 2012

Dropping FK Constraints in a Stored Procedure

I just looked at a coworker's stored procedure and this person is
dropping 4 Foreign key constraints and then re-adding them after
processing the required logic (updating rows in the 4 tables in
question).

Is there *ANY* good reason to do this? What are the performance
implications of doing this - negative or otherwise?

I was furious when I found this because every once in a while I would
notice that the constraints would be in flux...some days they were
there, othere days there were not. I mean, this is a good enough reason
to NOT do this, but I need some additional feedback. Maybe there *is* a
good reason, and that the logic just needs tweaking.

Thanks.The only justification for this technique is developer productivity. It's a
lot easier to write code without those nasty foreign key constraints getting
in the way :-)

Seriously, I can't think of a good reason to do this. There may be some
situations where temporarily removing constraints can improve batch
performance but this kind of thing should only be done during maintenance
windows. Adding and removing constraints during normal operation can lead
to blocking and data integrity issues.

--
Hope this helps.

Dan Guzman
SQL Server MVP

"Dmitri" <nienna.gaia@.gmail.com> wrote in message
news:1111719489.616490.262060@.f14g2000cwb.googlegr oups.com...
>I just looked at a coworker's stored procedure and this person is
> dropping 4 Foreign key constraints and then re-adding them after
> processing the required logic (updating rows in the 4 tables in
> question).
> Is there *ANY* good reason to do this? What are the performance
> implications of doing this - negative or otherwise?
> I was furious when I found this because every once in a while I would
> notice that the constraints would be in flux...some days they were
> there, othere days there were not. I mean, this is a good enough reason
> to NOT do this, but I need some additional feedback. Maybe there *is* a
> good reason, and that the logic just needs tweaking.
> Thanks.|||Dmitri (nienna.gaia@.gmail.com) writes:
> I just looked at a coworker's stored procedure and this person is
> dropping 4 Foreign key constraints and then re-adding them after
> processing the required logic (updating rows in the 4 tables in
> question).
> Is there *ANY* good reason to do this? What are the performance
> implications of doing this - negative or otherwise?

Good reason...

There is a problem with constraints and triggers in SQL Server, as they
always fire at statemet time. Other products have commit-time constraints
and/or triggers. This is good, because that permits you to violate
constraints temporarily in a transaction where it does not matter. One
example is that you have Orders and OrderDetails, and for some reason
need to reallocating order numbers and the constraints are not defined
as cascading. (Maybe because of the many restrictions with cascading
DRI in SQL Server.)

That said, I would say that it vert bad practice to do this in application
code. For a maintenance procedure or a fix proecedure that is run once in
a blue moon it could be acceptable. If there is some really difficult
situation where you must to this, you *must* do it within the realm of a
transaction, so that the final result of the operation in case of an error
or a power failure is that the constraints are gone.

Performance implications? You bet. If, as you say, he drops and re-adds
the constraints, there is a cost for checking the constraints. An
alternative would be to disable the constraints and enabling them
again without checking. In this case, the constraints would not be
trusted by the optimizer, and this could affect query plans. (And of
course, if his logic violated the constraints, no one would know.)

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp|||Standard SQL has a DEFERABLE option on constraints. They can be turned
on or off by default or by action. But all constraints have to be true
at COMMIT time. I would guess that he is used to DB2 or another SQL
that hs this feature.

SQL Server does not work this way. So this is very dangerous and he
should not be doing it. He can lock up or trash the whole system with
his stored procedures. My guess would be that his procedure ought to
update the table in the proper order, or that the schema has something
really ugly in it, like a cyclic reference.

If this has been goign on for awhile, you better do a data audit, too.|||Thanks for the input all!

dropping default constraints

Hello

We've got a product which uses merge replication with anonymous pullsubscriptions.
At most custome sites it's running on SQL Server 2000, a few with SQL 2005, which is running wich replication compatibility level 80 due to .
As it happens, db schema changes. So I have to drop a column with a default constraint. First the constraint, then the column.
This works excellently on the publisher - but not on the subscriber Sad

The schema script 'exec sp_repldropcolumn '[dbo].[role_modul_rmd]', 'rmd_modul_enabled', 1' could not be propagated to the subscriber. (Quelle: MSSQL_REPL, Fehlernummer: MSSQL_REPL-2147201001)
Hilfe abrufen: http://help/MSSQL_REPL-2147201001
The object 'DF__role_modu__rmd_m__3119DB2C' is dependent on column 'rmd_modul_enabled'. (Quelle: MSSQLServer, Fehlernummer: 5074)
Hilfe abrufen: http://help/5074
ALTER TABLE DROP COLUMN rmd_modul_enabled failed because one or more objects access this column. (Quelle: MSSQLServer, Fehlernummer: 4922)
Hilfe abrufen: http://help/4922

What can bi done?

Thanks for your advice
Aline

We had this exact same problem.

First to recover your replication you can go in and drop the column manually on the subscribers. Then replication should recover.

This happens because replication does not deliver the commands in the correct order, it tries to drop the column first before droping the constraint which it cannot do. The only solution is to drop the constraint first then wait for everyone to replicate then try dropping the column.

Martin

Tuesday, March 27, 2012

dropping default constraints

Hello

We've got a product which uses merge replication with anonymous pullsubscriptions.
At most custome sites it's running on SQL Server 2000, a few with SQL 2005, which is running wich replication compatibility level 80 due to .
As it happens, db schema changes. So I have to drop a column with a default constraint. First the constraint, then the column.
This works excellently on the publisher - but not on the subscriber Sad

The schema script 'exec sp_repldropcolumn '[dbo].[role_modul_rmd]', 'rmd_modul_enabled', 1' could not be propagated to the subscriber. (Quelle: MSSQL_REPL, Fehlernummer: MSSQL_REPL-2147201001)
Hilfe abrufen: http://help/MSSQL_REPL-2147201001
The object 'DF__role_modu__rmd_m__3119DB2C' is dependent on column 'rmd_modul_enabled'. (Quelle: MSSQLServer, Fehlernummer: 5074)
Hilfe abrufen: http://help/5074
ALTER TABLE DROP COLUMN rmd_modul_enabled failed because one or more objects access this column. (Quelle: MSSQLServer, Fehlernummer: 4922)
Hilfe abrufen: http://help/4922

What can bi done?

Thanks for your advice
Aline

We had this exact same problem.

First to recover your replication you can go in and drop the column manually on the subscribers. Then replication should recover.

This happens because replication does not deliver the commands in the correct order, it tries to drop the column first before droping the constraint which it cannot do. The only solution is to drop the constraint first then wait for everyone to replicate then try dropping the column.

Martin

Dropping all indexes with a single query?

Would that it were so simple! I tried that method, no go because
there are constraints that have to be deleted first. So, I figure OK,
I'll just kill the constraints first, then the indexes will go away
with ease. NOT! Since there are two ways to do things, using
standard SQL and using the SQLDMO object, I tried both. Here
is where I am with the code right now:
'******************************
Dim server
Dim tbcnt
Dim idcnt
Dim kecnt
Dim i, j, k
Dim tblname
Dim keyname
Dim sql
Set server = CreateObject("SQLDMO.SQLServer")
server.Connect "Server", "sa", ""
i = "Northwind"
tbcnt = server.Databases(i).Tables.Count
For j = 1 to tbcnt
If server.Databases(i).Tables(j).TypeOf = 8 Then
tblname = server.Databases(i).Tables(j).Name
kecnt = server.Databases(i).Tables(j).Keys.Count
idcnt = server.Databases(i).Tables(j).Indexes.Count
For k = 1 to kecnt
keyname = server.Databases(i).Tables(j).Keys(k).Name
' was: server.Databases(i).Tables(j).Keys(k).Remove
sql = "USE " & i & vbCrLf & _
"ALTER TABLE " & tblname & vbCrLf & _
"DROP CONSTRAINT " & keyname
server.ExecuteImmediate(sql)
Next
For k = 1 to idcnt
server.Databases(i).Tables(j).Indexes(k).Remove
Next
End If
Next
'******************************
Now, Keys here are really constraints, so I tried this, using a
mixture of SQL and VB code, and I also tried it with all VB,
and all SQL as well. No matter what I try, I keep getting
'Constrain xxxx is being referenced by Foreign Key uuuu...etc.
and
'Could not drop constraint xxxx'
Does anyone know if this can be automated like this? If so,
How? I'm already past tearing my hair out, I hate to think
what happens next...
"Aaron Bertrand - MVP" <aaron@.TRASHaspfaq.com> wrote in message
news:%23d0XGCbQEHA.2132@.TK2MSFTNGP11.phx.gbl...
> So, run the first query, bring back the "DROP INDEX ...; GO; DROP INDEX;
> GO;" statements into a variable, and then execute that just like you
> executed the initial query.
> A
>
How about drop the database and re-create it?
Aaron Bertrand
SQL Server MVP
http://www.aspfaq.com/
"Gary Morris" <gwmorris1@.hotpop.com> wrote in message
news:O8YoOieQEHA.3748@.TK2MSFTNGP09.phx.gbl...
> Would that it were so simple! I tried that method, no go because
> there are constraints that have to be deleted first. So, I figure OK,
> I'll just kill the constraints first, then the indexes will go away
> with ease. NOT! Since there are two ways to do things, using
> standard SQL and using the SQLDMO object, I tried both. Here
> is where I am with the code right now:
> '******************************
> Dim server
> Dim tbcnt
> Dim idcnt
> Dim kecnt
> Dim i, j, k
> Dim tblname
> Dim keyname
> Dim sql
> Set server = CreateObject("SQLDMO.SQLServer")
> server.Connect "Server", "sa", ""
> i = "Northwind"
> tbcnt = server.Databases(i).Tables.Count
> For j = 1 to tbcnt
> If server.Databases(i).Tables(j).TypeOf = 8 Then
> tblname = server.Databases(i).Tables(j).Name
> kecnt = server.Databases(i).Tables(j).Keys.Count
> idcnt = server.Databases(i).Tables(j).Indexes.Count
> For k = 1 to kecnt
> keyname = server.Databases(i).Tables(j).Keys(k).Name
> ' was: server.Databases(i).Tables(j).Keys(k).Remove
> sql = "USE " & i & vbCrLf & _
> "ALTER TABLE " & tblname & vbCrLf & _
> "DROP CONSTRAINT " & keyname
> server.ExecuteImmediate(sql)
> Next
> For k = 1 to idcnt
> server.Databases(i).Tables(j).Indexes(k).Remove
> Next
> End If
> Next
> '******************************
> Now, Keys here are really constraints, so I tried this, using a
> mixture of SQL and VB code, and I also tried it with all VB,
> and all SQL as well. No matter what I try, I keep getting
> 'Constrain xxxx is being referenced by Foreign Key uuuu...etc.
> and
> 'Could not drop constraint xxxx'
> Does anyone know if this can be automated like this? If so,
> How? I'm already past tearing my hair out, I hate to think
> what happens next...
> "Aaron Bertrand - MVP" <aaron@.TRASHaspfaq.com> wrote in message
> news:%23d0XGCbQEHA.2132@.TK2MSFTNGP11.phx.gbl...
>
|||How did I know you would say that
Actually I had thought of that, but was dearly hoping
that I wouldn't have to resort to it. I just cannot
believe that SQL Server will not allow this in an easier
way. It's no problem manually, but programmatically
it just won't work. Eight hours working on this one
problem today..not very productive. Why can't it just
work like the Sybase version?
Well, I'll hunt around some just to satisfy myself that
some nerdy guru hasn't found a way around this
already, then I'll look into the alternative. What I'm
trying to accomplish is a script that will drop just the
indexes of ALL the user tables, OR just the indexes
on specified tables. I won't go into the reasons, that's
just the way it has to work. Now, I have considered
just writing the sql out for each table, parsing it to
remove the constraint/index parts, and then reimport
the table(s) back into the db, which would work, I
just consider it to be a dirty hack, and I should have
a nice clean, acceptable way to do it. This sucks...
"Aaron Bertrand [MVP]" <aaron@.TRASHaspfaq.com> wrote in message
news:e%23lGhGgQEHA.252@.TK2MSFTNGP10.phx.gbl...[vbcol=seagreen]
> How about drop the database and re-create it?
> --
> Aaron Bertrand
> SQL Server MVP
> http://www.aspfaq.com/
>
> "Gary Morris" <gwmorris1@.hotpop.com> wrote in message
> news:O8YoOieQEHA.3748@.TK2MSFTNGP09.phx.gbl...
INDEX;
>
sql

Dropping all Data but keeping constraints and Structure Intact!

Hi All -
I have a SQL 2000 database of 60 some tables, with constraints, indexes, PK-
FK, and other relationships on them. I would like to drop all data in all
tables, knowing that a lot of the tables have full PK-FK-PK-FK ...
relationships cascading, and indexes and such. I want to keep intact all
that is not data. So basically truncate or delete all data and leave all
table structures with all their constraints etc ..
I wonder if such a script exists? that would be a great help.
Thank You In Advance.
Wise -
--
Message posted via SQLMonster.com
http://www.sqlmonster.com/Uwe/Forums.aspx/sql-server/200704/1On Apr 11, 7:20 pm, "wiseteufel via SQLMonster.com" <u4067@.uwe> wrote:
> Hi All -
> I have a SQL 2000 database of 60 some tables, with constraints, indexes, PK-
> FK, and other relationships on them. I would like to drop all data in all
> tables, knowing that a lot of the tables have full PK-FK-PK-FK ...
> relationships cascading, and indexes and such. I want to keep intact all
> that is not data. So basically truncate or delete all data and leave all
> table structures with all their constraints etc ..
> I wonder if such a script exists? that would be a great help.
> Thank You In Advance.
> Wise -
> --
> Message posted via SQLMonster.comhttp://www.sqlmonster.com/Uwe/Forums.aspx/sql-server/200704/1
You can script out the entire database ( with PK,FK, INDEX ,
Constraint options) and create a new database from the script .

Dropping all Data but keeping constraints and Structure Intact!

Hi All -
I have a SQL 2000 database of 60 some tables, with constraints, indexes, PK-
FK, and other relationships on them. I would like to drop all data in all
tables, knowing that a lot of the tables have full PK-FK-PK-FK ...
relationships cascading, and indexes and such. I want to keep intact all
that is not data. So basically truncate or delete all data and leave all
table structures with all their constraints etc ..
I wonder if such a script exists? that would be a great help.
Thank You In Advance.
Wise -
Message posted via droptable.com
http://www.droptable.com/Uwe/Forum...server/200704/1On Apr 11, 7:20 pm, "wiseteufel via droptable.com" <u4067@.uwe> wrote:
> Hi All -
> I have a SQL 2000 database of 60 some tables, with constraints, indexes, P
K-
> FK, and other relationships on them. I would like to drop all data in all
> tables, knowing that a lot of the tables have full PK-FK-PK-FK ...
> relationships cascading, and indexes and such. I want to keep intact all
> that is not data. So basically truncate or delete all data and leave all
> table structures with all their constraints etc ..
> I wonder if such a script exists? that would be a great help.
> Thank You In Advance.
> Wise -
> --
> Message posted via droptable.comhttp://www.droptable.com/Uwe/Forums.aspx/sql-ser
ver/200704/1
You can script out the entire database ( with PK,FK, INDEX ,
Constraint options) and create a new database from the script .

Dropping all Data but keeping constraints and Structure Intact!

Hi All -
I have a SQL 2000 database of 60 some tables, with constraints, indexes, PK-
FK, and other relationships on them. I would like to drop all data in all
tables, knowing that a lot of the tables have full PK-FK-PK-FK ...
relationships cascading, and indexes and such. I want to keep intact all
that is not data. So basically truncate or delete all data and leave all
table structures with all their constraints etc ..
I wonder if such a script exists? that would be a great help.
Thank You In Advance.
Wise -
Message posted via droptable.com
http://www.droptable.com/Uwe/Forums.aspx/sql-server/200704/1
On Apr 11, 7:20 pm, "wiseteufel via droptable.com" <u4067@.uwe> wrote:
> Hi All -
> I have a SQL 2000 database of 60 some tables, with constraints, indexes, PK-
> FK, and other relationships on them. I would like to drop all data in all
> tables, knowing that a lot of the tables have full PK-FK-PK-FK ...
> relationships cascading, and indexes and such. I want to keep intact all
> that is not data. So basically truncate or delete all data and leave all
> table structures with all their constraints etc ..
> I wonder if such a script exists? that would be a great help.
> Thank You In Advance.
> Wise -
> --
> Message posted via droptable.comhttp://www.droptable.com/Uwe/Forums.aspx/sql-server/200704/1
You can script out the entire database ( with PK,FK, INDEX ,
Constraint options) and create a new database from the script .

Sunday, March 11, 2012

Drop indexes

I have a database with many PK and FK constraints. Is there any script I can use to drop all indexes and rebuild them taking care of PK and dependencies? I am unable to drop them by tablename order. This is on MSSQLServer-2000 SP3. Any help is appreciated.why drop and recreate when you can defrag or reindex? have you looked at DBCC SHOWCONTIG to make you sure need to do this.

btw, you only have to do the clustered ones. the nonclustered get rebuilt when you do this.

here is some code...

SELECT 'DBCC DBREINDEX(' + CAST(o.[name] as varchar(200)) + ',' + CAST(i.[name] as varchar) + ')
GO'
FROM sysindexes i
JOIN sysobjects o
ON i.id = o.id
WHERE o.xtype = 'U'
AND i.indid = 1|||is I have primary clustered indexes on 812 tables and do not know a lot of dependencies if I go by tablename order to drop and recreate indexes. Does DBCC REINDEX drops and recreates PK indexes too? We have run DBCC showcontig and saw fragmentation quite a bit.|||DBCC DBREINDEX does not drop and recreate to knowledge but I would confirm in BOL. It does however lock up tables.

DBCC INDEXDEFRAG is slower but does not lock up the tables so much and the users can work more easily while this goes on.|||DBCC DBREINDEX does not drop and recreate to knowledge but I would confirm in BOL.
It doesn't - in fact, when last I read about all this stuff it was presented as DBREINDEX's USP :D

Friday, March 9, 2012

drop constraints

Hi,

I'm trying to update some tables, but there are constraints on them that need to be removed first. As I didn't create the DB and tables myself, I used the 'Generate SQL script' to get all constraints and their name.

I then had a look at the 'sysobject' table, and found some constraints (FK__ ...) listed in the script, but not all of them. Is there another way to to get all constraints on the DB ??

can you suggest the best way to drop the constrains? I was going to use something like:

declare @.mytest char(50)
set @.mytest=(select name from sysobjects where name like "FK__Item__attrib%")
EXEC( 'alter table item drop constraint '+@.mytest)

vincentI use this code to remove PRIMARY and FORIEGN KEY constraints plus drop all of my indexes, however I removed the DROP INDEX part
SET NOCOUNT ON
DECLARE @.SQLCmd varchar(255)

DECLARE DelCur CURSOR
FOR
SELECT CASE
WHEN OBJECTPROPERTY(OBJECT_ID(i.name), 'IsPrimaryKey') = 1
THEN 'ALTER TABLE ' + + o.name + ' DROP CONSTRAINT ' + i.name
WHEN OBJECTPROPERTY(OBJECT_ID(i.name), 'IsForiegnKey') = 1
THEN 'ALTER TABLE ' + + o.name + ' DROP CONSTRAINT ' + i.name
END
FROM sysindexes i,
sysobjects o
WHERE o.id = i.id
AND OBJECTPROPERTY(o.id, 'IsUserTable') = 1
AND OBJECTPROPERTY(o.id, 'IsMSShipped') = 0
AND i.indid BETWEEN 1 AND 254
AND INDEXPROPERTY(o.id, i.name, 'IsStatistics') = 0
AND (
OBJECTPROPERTY(OBJECT_ID(i.name), 'IsPrimaryKey') = 1 OR
OBJECTPROPERTY(OBJECT_ID(i.name), 'IsForiegnKey') = 1
)

OPEN DelCur

FETCH DelCur INTO @.SQLCmd

WHILE (@.@.fetch_status = 0)
BEGIN
PRINT @.SQLCmd
EXEC (@.SQLCmd)
FETCH DelCur INTO @.SQLCmd
END

CLOSE DelCur
DEALLOCATE DelCur
GO

DROP COLUMN

If I execute ALTER TABLE DROP COLUMN XX but the column XX has Constraints
there will be an error.
I should first delete the related Constraints.
Is there any way to delete all of the Constraints related to this column?
Should I navigate before for all of the constraints of the table and delete
the related ones? In this case, how could I do this in VB?
Regards.
Jose Nuez
Montevideo
Jose,
The INFORMATION_SCHEMA view contain what you are after. Here is a sample
query that may be helpful to you:
select y.TABLE_NAME AS ConstrainingTable, x.CONSTRAINT_NAME AS
ConstraintToDisable,
z.TABLE_NAME AS ConstrainedTable, zz.COLUMN_NAME as ConstrainedColumn,
z.CONSTRAINT_NAME AS ConstrainedConstraint
from INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS x
join INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE y
on x.CONSTRAINT_NAME = y.CONSTRAINT_NAME
join INFORMATION_SCHEMA.CONSTRAINT_TABLE_USAGE z
join INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE zz
on z.CONSTRAINT_NAME = zz.CONSTRAINT_NAME
on x.UNIQUE_CONSTRAINT_NAME = z.CONSTRAINT_NAME
where y.COLUMN_NAME = 'MyKeyColumn' and z.TABLE_NAME = 'MyConstrainedTable'
RLF
"Jose Nunez" <josenunez70@.hotmail.com> wrote in message
news:uBIE0B7LIHA.5208@.TK2MSFTNGP04.phx.gbl...
> If I execute ALTER TABLE DROP COLUMN XX but the column XX has Constraints
> there will be an error.
> I should first delete the related Constraints.
> Is there any way to delete all of the Constraints related to this column?
> Should I navigate before for all of the constraints of the table and
> delete the related ones? In this case, how could I do this in VB?
> Regards.
> Jose Nuez
> Montevideo
>

Wednesday, March 7, 2012

Drop All tables using one query

Hi all,

How do i drop all tables (with constraints) in a database using one queries?

one easy way is using the ms_foreachtable.

exec sp_msforeachtable 'Drop table ?'

Note: This sp is undocumented sp. You can use it for one time activity, but don't stick on this sp. It may be get removed with out any notification.

|||

Could not drop object 'dbo.TRN' because it is referenced by a FOREIGN KEY constraint.

Msg 3726, Level 16, State 1, Line 1

This is what i get..

|||Execute the same query multiple times|||

Would not it be easier if you drop the database?

AMB

|||

Another possible approach:

if object_id('[dbo].[sp_drop_all_tables]') is not null drop proc [dbo].[sp_drop_all_tables]

go

create proc [dbo].[sp_drop_all_tables]

@.pSchemaName sysname = null

as

set xact_abort on

set nocount on

declare

@.ParentTableSchemaName sysname,

@.ParentTableName sysname,

@.ChildTableSchemaName sysname,

@.ChildTableName sysname,

@.FKName sysname,

@.SQLCommandText nvarchar(max),

@.ErrorMessageText nvarchar(max)

if db_name() = 'master'

begin

raiserror('Cannot be run in the master database.', 16, 1)

return

end

if @.pSchemaName = 'sys'

begin

raiserror('Cannot be run for tables in the [sys] schema.', 16, 1)

return

end

select

@.ParentTableSchemaName = @.pSchemaName

begin tran

select

@.SQLCommandText = '

declare curObject cursor global static for

select

fk_po_s.name as ParentTableSchemaName,

fk_po.name as ParentTableName,

fk_co_s.name as ChildTableSchemaName,

fk_co.name as ChildTableName,

fk.name as FKName

from sys.foreign_keys as fk

join sys.objects as fk_po

on fk_po.object_id = fk.referenced_object_id

join sys.schemas as fk_po_s

on fk_po_s.schema_id = fk_po.schema_id

join sys.objects as fk_co

on fk_co.object_id = fk.parent_object_id

join sys.schemas as fk_co_s

on fk_co_s.schema_id = fk_co.schema_id

where fk_po.type = ''u''

and fk_po_s.name <> ''sys''' +

case when @.ParentTableSchemaName is null then '' else '

and fk_po_s.name = ''' + @.ParentTableSchemaName + '''' end + '

order by

fk_po_s.name,

fk_po.name,

fk_co_s.name,

fk_co.name,

fk.name'

--print @.SQLCommandText

exec(@.SQLCommandText)

open curObject

fetch next from curObject into

@.ParentTableSchemaName,

@.ParentTableName,

@.ChildTableSchemaName,

@.ChildTableName,

@.FKName

while @.@.fetch_status = 0

begin

select

@.SQLCommandText = 'alter table [' + @.ChildTableSchemaName + '].[' + @.ChildTableName +

'] drop constraint [' + @.FKName + ']'

-- print @.SQLCommandText

exec(@.SQLCommandText)

fetch next from curObject into

@.ParentTableSchemaName,

@.ParentTableName,

@.ChildTableSchemaName,

@.ChildTableName,

@.FKName

end

close curObject

deallocate curObject

select

@.ParentTableSchemaName = @.pSchemaName

select

@.SQLCommandText = '

declare curObject cursor for

select

s.name as ParentTableSchemaName,

o.name as ParentTableName

from [' + db_name() + '].sys.objects as o

join [' + db_name() + '].sys.schemas as s

on s.schema_id = o.schema_id

where o.type = ''u''

and left(o.name, 1) <> ''#''

and s.name <> ''sys''' +

case when @.ParentTableSchemaName is null then '' else '

and s.name = ''' + @.ParentTableSchemaName + '''' end + '

order by

o.name'

--print @.SQLCommandText

exec(@.SQLCommandText)

open curObject

fetch next from curObject into

@.ParentTableSchemaName,

@.ParentTableName

if @.@.fetch_status <> 0

begin

print 'No user tables exist.'

end

while @.@.fetch_status = 0

begin

select

@.SQLCommandText = 'drop table [' + @.ParentTableSchemaName + '].[' + @.ParentTableName + ']'

print @.SQLCommandText

exec(@.SQLCommandText)

fetch next from curObject into

@.ParentTableSchemaName,

@.ParentTableName

end

close curObject

deallocate curObject

commit

NormalReturn:

return 0

ErrorReturn:

if @.@.trancount > 0

begin

rollback

end

if cursor_status('global','curObject') = 1

begin

close curObject

end

if cursor_status('global','curObject') = -1

begin

deallocate curObject

end

select

@.ErrorMessageText = coalesce(@.ErrorMessageText, 'The stored procedure sp_drop_all_tables failed.')

raiserror(@.ErrorMessageText, 16, 1)

return 1

go

Ron Rice