title: How to dynamically DELETE in SQL Server?
body: <p>I am trying to do something like that, I need to dynamically delete rows from a table. I dont know anything about the structure of that table beforehand so I am trying to solve everything dynamically.</p>
  
  <p>The following code snippet shows what I am trying to do but I am aware that the except doesn't work with an asterisk. I dont have any Ids or so to delete the item i just want the item from a deletetable to be deleted in the main table. Is there a way to do that?</p>
  
  <p>Thank you very much</p>
  
  <pre><code>CREATE PROCEDURE sp_myprocedure 
      (@tablename NVARCHAR(256),
       @changeId BIGINT)
  AS
  BEGIN
      DECLARE @sqlstatement AS NVARCHAR(1000)
  
      SELECT @sqlstatement = CONCAT('SELECT * INTO ', @tablename,
                                    '_delete FROM ', @tablename,
                                    ' WHERE changeID = @changeId 
                                      SELECT * INTO ',@tablename,'_result_temp 
                                      FROM
                                          (SELECT * FROM ', @tablename,
                                    '_result 
                                     EXCEPT
                                     SELECT * FROM ', @tablename, '_delete)
                                    DROP TABLE ', @tablename, '_result
  
  EXEC sp_rename ''',@tablename,'_result_temp'', ''',@tablename,'_result''  
  
       ')
  
      PRINT @sqlstatement
      EXEC sp_executesql @sqlstatement
  END
  GO
  
  
  </code></pre>