I have a database in sql server 2008 R2 that have many table (over 200) and have many relation betweens tables. Delete rule in most of relations is No Action I need to Update all relation delete rule to Cascade at once Because of too many relation in my database I dont want do this one by one Is there any way ?
-
1In any moderately complex database, there could be multiple cascade paths which aren't allowed by SQL Server's cascade options - so you'll need to carefully plan which ones actually get turned on (then either leave the rest, or you'll have to implement via a trigger)– Damien_The_UnbelieverCommented May 20, 2013 at 11:50
-
@Damien_The_Unbeliever: Also, in any moderately complex database, there will almost certainly be at least a few foreign keys that shouldn't cascade either updates or deletes.– Mike Sherrill 'Cat Recall'Commented May 20, 2013 at 13:20
Add a comment
|
1 Answer
Typically, when you ALTER a Foreign key constraint, using the SSMS GUI, SQL Server on the background actually drops and recreates the same.
Here is a script, that will generate the SQL for dropping all FKeys and recreating them with ON UPDATE CASCADE ON DELETE CASCADE
options
Assumption is, all your FKeys are name "FK....."
SET NOCOUNT ON;
DECLARE @Objects TABLE
(
ID int identity(1,1),
TableName sysname,
SchemaName sysname
)
INSERT INTO @Objects (TableName, SchemaName)
SELECT
TABLE_NAME,
CONSTRAINT_SCHEMA
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE CONSTRAINT_NAME LIKE 'FK%'
DECLARE @min int, @max int,@table sysname,@schema sysname
SELECT @min = 1, @max = MAX(ID) FROM @Objects
WHILE @min <=@max
BEGIN
SELECT
@table = TableName,@schema = SchemaName FROM @Objects WHERE ID = @min
print '/*Drop Foreign Key Statements for [' + @schema + '].[' + @table + ']*/'
SELECT
'ALTER TABLE [' + SCHEMA_NAME(o.schema_id) + '].[' + o.name + ']
DROP CONSTRAINT [' + fk.name + ']'
FROM sys.foreign_keys fk
INNER JOIN sys.objects o
ON fk.parent_object_id = o.object_id
WHERE o.name = @table AND
SCHEMA_NAME(o.schema_id) = @schema
print '/*Create Foreign Key Statements for ['
+ @schema + '].[' + @table + ']*/'
SELECT
'ALTER TABLE [' + SCHEMA_NAME(o.schema_id) + '].[' + o.name + ']
ADD CONSTRAINT [' + fk.name + '] FOREIGN KEY ([' + c.name + '])
REFERENCES [' + SCHEMA_NAME(refob.schema_id) + '].[' + refob.name + ']
([' + refcol.name + '])ON UPDATE CASCADE ON DELETE CASCADE'
FROM sys.foreign_key_columns fkc
INNER JOIN sys.foreign_keys fk
ON fkc.constraint_object_id = fk.object_id
INNER JOIN sys.objects o
ON fk.parent_object_id = o.object_id
INNER JOIN sys.columns c
ON fkc.parent_column_id = c.column_id AND
o.object_id = c.object_id
INNER JOIN sys.objects refob
ON fkc.referenced_object_id = refob.object_id
INNER JOIN sys.columns refcol
ON fkc.referenced_column_id = refcol.column_id AND
fkc.referenced_object_id = refcol.object_id
WHERE o.name = @table AND
SCHEMA_NAME(o.schema_id) = @schema
SET @min = @min+1
END
Hope this helps.
Raj
PS: Setting query output to text helps. Also please read the comment posted on your question. Arbitrarily setting CASCADE
may not be the right thing to do
-
your Query just show command to do.Can you change It to execute ? Commented May 21, 2013 at 5:14
-
DDL queries need to be reviewed before execution.Can you copy the output and execute in a new query window?– RajCommented May 21, 2013 at 5:21