How to execute a dynamically created sql statement
I wish to drop all the constraints of tables I have.
Since this is not possible in SQL Server, I've created a script (based on something found online) that is creating dynamically the drop statements as a result of a Select:

    SELECT 'ALTER TABLE ' + TABLE_SCHEMA + '.[' + TABLE_NAME + '] DROP CONSTRAINT [' + CONSTRAINT_NAME + ']'
        FROM information_schema.table_constraints
        WHERE CONSTRAINT_TYPE = 'FOREIGN KEY'
        AND TABLE_NAME LIKE 'Old_%''

This done, I can't find how to parse it and execute that in a loop.

My attempt is to create a cursor like this, but I have no clue then to make it executing each line:

    DECLARE @AlterTables nvarchar(2000)
    SET @AlterTables = 'DECLARE Dyn_cursor CURSOR
    FOR SELECT ''ALTER TABLE '' + TABLE_SCHEMA + ''.['' + TABLE_NAME + ''] DROP CONSTRAINT ['' + CONSTRAINT_NAME + '']''
    FROM information_schema.table_constraints
    WHERE CONSTRAINT_TYPE = ''FOREIGN KEY''
    AND TABLE_NAME LIKE ''Old_%'''
    Exec(@AlterTables)
    
    Open Dyn_Cursor 
    	FETCH NEXT FROM Dyn_Cursor INTO @name   
    	WHILE @@FETCH_STATUS = 0   
    BEGIN 
    END
    
    Close Dyn_cursor
    Deallocate Dyn_cursor

Thank you in advance to whom would have a solution for that!

cheers

J.

How to execute a dynamically created sql statement
===

I wish to drop all the constraints of tables I have.
Since this is not possible in SQL Server, I've created a script (based on something found online) that is creating dynamically the drop statements as a result of a Select:

    SELECT 'ALTER TABLE ' + TABLE_SCHEMA + '.[' + TABLE_NAME + '] DROP CONSTRAINT [' + CONSTRAINT_NAME + ']'
        FROM information_schema.table_constraints
        WHERE CONSTRAINT_TYPE = 'FOREIGN KEY'
        AND TABLE_NAME LIKE 'Old_%''

This done, I can't find how to parse it and execute that in a loop.

My attempt is to create a cursor like this, but I have no clue then to make it executing each line:

    DECLARE @AlterTables nvarchar(2000)
    SET @AlterTables = 'DECLARE Dyn_cursor CURSOR
    FOR SELECT ''ALTER TABLE '' + TABLE_SCHEMA + ''.['' + TABLE_NAME + ''] DROP CONSTRAINT ['' + CONSTRAINT_NAME + '']''
    FROM information_schema.table_constraints
    WHERE CONSTRAINT_TYPE = ''FOREIGN KEY''
    AND TABLE_NAME LIKE ''Old_%'''
    Exec(@AlterTables)
    
    Open Dyn_Cursor 
    	FETCH NEXT FROM Dyn_Cursor INTO @name   
    	WHILE @@FETCH_STATUS = 0   
    BEGIN 
    END
    
    Close Dyn_cursor
    Deallocate Dyn_cursor

Thank you in advance to whom would have a solution for that!

cheers

J.
