I have several tables with a foreign key constraint that has the option ON DELETE CASCADE
. Every table belongs to the same schema called datasets
.
I'm able to retrieve the complete list of tables using :
SELECT *
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA ='datasets'
For each table I would like to remove the ON DELETE CASCADE
option on the foreign key constraint named FK_[TABLENAME]_SerieID
where [TABLENAME]
corresponds to the name of the table (and SerieId
is the same foreign key across tables).
I am able to perform the operation for a particular table, for instance the table called Table1
using :
ALTER TABLE datasets.Table1
DROP CONSTRAINT FK_Table1_SerieID
ALTER TABLE datasets.Table1
ADD CONSTRAINT FK_Table1_SerieID
FOREIGN KEY (Serie_Id) REFERENCES[dbo].[Serie](SerieID)
ON DELETE NO ACTION
GO
I would like to perform the above operation for each table that belong to the schema datasets
. I'm new to T-SQL and I don't know how to do it.
Should I use a cursor? Can you help me with this?
I'm using SQL Server 2016.