title: Bind a column default value to a function in SQL 2005
body: <p>I have a column containing items that can be sorted by the user:</p>
  
  <pre><code>DOC_ID  DOC_Order DOC_Name
     1       1        aaa
     2       3        bbb
     3       2        ccc
  </code></pre>
  
  <p>I'm trying to figure out a way to properly initialize DOC_Order when the entry is created. A good value would either be the corresponding DO-CID (since it is autoassigned), or MAX(DOC-ORDER) + 1</p>
  
  <p>After a bit of googling I saw it was possible to assign a scalar function's return to the default column. </p>
  
  <pre><code>CREATE FUNCTION [dbo].[NEWDOC_Order] 
  (
  )
  RETURNS int
  AS
  BEGIN
  
  RETURN (SELECT MAX(DOC_ORDER) + 1 FROM DOC_Documents)
  
  END
  </code></pre>
  
  <p>But each of my tries using MS SQL Management studio ended in a "Error validating the default for column 'DOC_Order'" message.</p>
  
  <p>Any idea of what the exact SQL syntax to assign a function to DEFAULT is?</p>