title: How delete rows using a variable in SQL Server?
body: <p>Hope you can help me with the issue below.</p>
  
  <p><strong>DETAILS</strong>: I am trying to create a stored procedure for my Azure Data Factory project where I can pass a variable table name through to tell SQL Server to delete rows. </p>
  
  <p>This all is part of an ERP tool with transactional data. I would delete rows that are in the target, but changed in the source. </p>
  
  <p><strong>Example</strong>: rowid 1 - €100 in the target but changes in the source later to €80. Then I need to delete that row with €100 in the target and copy the new value from the source with the same rowid to the target</p>
  
  <hr>
  
  <p>I'm migrating from a Oracle environment to a Microsoft environment. In Oracle I've this SQL for the stored procedure:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>create or replace procedure AAAAA_delete
  as
  cursor c_del
  is
  
  
  select 'delete from ' ||table_name ||' a where not exists (select 1 from ' ||replace(table_name,'AAAAA','BBBBB') ||' b where a.rowid = b.rowid)' deletions
  from all_tables
  where table_name like 'AAAAA%'
  and table_name not like 'AAAAA_LOAD%'
  and table_name not in ('AAAAA_TIME','AAAAA_CONFIGURATION')
  order by 1;
  
  begin
  for r_del in c_del
      loop
       execute immediate r_del.deletions;
       commit;
      end loop;
  end;</code></pre>
  </div>
  </div>
  
  ** the names have been changed to AAAA and BBBB on purpose to publish the code ** </p>
  
  <p>I'm wondering how i can transform this into MS SQL.</p>
  
  <p><strong>WHY?</strong>: My whole ETL process in Azure Data Factory is based on variables. All the table names etc are stored in a metadata table. And it depends on +- 70 tables.</p>
  
  <p><strong>QUESTION</strong>: Is it possible to create a generic stored procedure to drop rows  depending on the name I pass through.</p>