Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Removing constraints
Message
 
 
To
25/03/2011 01:11:26
General information
Forum:
Microsoft SQL Server
Category:
Scripting
Environment versions
SQL Server:
SQL Server 2008
Application:
Web
Miscellaneous
Thread ID:
01504936
Message ID:
01504947
Views:
47
This message has been marked as the solution to the initial question of the thread.
Your script is not protected against invalid table names. You should use QUOTENAME() function
Select 'ALTER TABLE '+ QUOTENAME(tab.name) + ' DROP CONSTRAINT '+sysobjects.name
>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?
--sb--
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform