title: Need to add constraint containing table name to sql table using sp_MSforeachtable
body: <p>I'm trying to use sp_MSforeachtable to add a new column to all my tables with a named constraint.</p>
  
  <p>So far, I've done this:</p>
  
  <pre><code>USE [MYTable]
  GO
  exec sp_MSforeachtable 'ALTER TABLE ? ADD ChangedBy nvarchar(100) DEFAULT (suser_name()) NOT NULL'
  </code></pre>
  
  <p>That works except that the constraint name comes out something like: DF_TableName_Change_51EF2864
  I want it to be named DF_TableName_ChangedBy</p>
  
  <p>I've played around and found that PARSENAME(''?'',1) will give me the name of the table.  Is there anyway to dynamically build the constraint name using this?</p>
  
  <pre><code>Example: ... CONSTRAINT ''DF_''+PARSENAME(''?'',1)+''_CreatedBy'' DEFAULT ...
  </code></pre>
  
  <p>(That doesn't seem to work but I included it to give a feel for what I'm hoping can be done.)</p>
  
  <p>Thanks for any help!</p>