Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Removing constraints
Message
 
À
25/03/2011 01:11:26
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Scripting
Versions des environnements
SQL Server:
SQL Server 2008
Application:
Web
Divers
Thread ID:
01504936
Message ID:
01504943
Vues:
38
This message has been marked as a message which has helped to the initial question of the thread.
>I have this nice script which removes constraints on the database:
>
>
>DECLARE @ConstraintName nvarchar(128)
>DECLARE @Command nvarchar(4000)
>
>DECLARE TableTemporary CURSOR LOCAL FOR
>
>Select 'ALTER TABLE '+tab.name+' DROP CONSTRAINT '+sysobjects.name
> From SysObjects
> INNER JOIN (Select [Name],[ID] From SysObjects Where XType = 'U') As Tab
>On Tab.[ID] = Sysobjects.[Parent_Obj] 
>Inner Join sysconstraints On sysconstraints.Constid = Sysobjects.[ID] 
>Inner Join SysColumns Col On Col.[ColID] = sysconstraints.[ColID] And Col.[ID] = Tab.[ID]
>
>OPEN TableTemporary
>FETCH NEXT FROM TableTemporary INTO @ConstraintName
>WHILE @@fetch_status = 0
>BEGIN
>   SET @Command = @ConstraintName
>   EXEC sp_executesql @Command
>   FETCH NEXT FROM TableTemporary into @ConstraintName
>END
>
>
>This has been tested well on two servers. Both are SQL Server 2005. When we moved that one into production, also a SQL Server 2005, we got this message:
>
>"Incorrect syntax near the keyword 'Table'."
>
>I have to say I am quite surprised as the three servers are identical. Anyone would know what could be in cause here?

I would use this:
DECLARE @ConstraintName nvarchar(128)
DECLARE @Command nvarchar(max)
SET @Command = ''
Select @Command = @Command + 
       'ALTER TABLE '+tab.name+' DROP CONSTRAINT '+sysobjects.name+CHAR(13)+CHAR(10)
FROM SysObjects
INNER JOIN (Select [Name],[ID] From SysObjects Where XType = 'U')  Tab
       ON Tab.[ID] = Sysobjects.[Parent_Obj] 
Inner Join sysconstraints On sysconstraints.Constid = Sysobjects.[ID] 
Inner Join SysColumns Col On Col.[ColID] = sysconstraints.[ColID] And Col.[ID] = Tab.[ID]

EXEC sp_executesql @Command
but that is a matter of preferences :-)

In your case you could add a print @Command before executing each line to see where you get the error.
Against Stupidity the Gods themselves Contend in Vain - Johann Christoph Friedrich von Schiller
The only thing normal about database guys is their tables.
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform