title: How to execute a dynamically created sql statement
body: <p>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:</p>
  
  <pre><code>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_%''
  </code></pre>
  
  <p>This done, I can't find how to parse it and execute that in a loop.</p>
  
  <p>My attempt is to create a cursor like this, but I have no clue then to make it executing each line:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>Thank you in advance to whom would have a solution for that!</p>
  
  <p>cheers</p>
  
  <p>J.</p>