title: Cannot find data type - error occurs only inside larger script
body: <p>I am creating a UDF.<br>
  The function is successfully created <strong>ONLY</strong> if I run the <code>CREATE FUNCTION</code> block by itself.</p>
  
  <p>If I include the <code>CREATE FUNCTION</code> block inside a much larger "complete" DB creation script, I get a weird error message stating an invalid data type was used.<br>
  (See code block and error screenshot below.)</p>
  
  <p><code>USE MyDB</code>, <code>SET ANSI_NULLS</code>, and <code>SET QUOTED_IDENTIFIER</code> are identical in both cases, and have no effect.</p>
  
  <p>This is very weird, because the UDTT is created much earlier in the DB creation script, with no errors. I can see the UDTT in SSMS, so it clearly exists ...</p>
  
  <p>If I re-run the complete DB creation script, I obviously get a bunch of messages about recreating objects that already exist - I ignore these messages, because that's expected.<br>
  But then I get this error, which causes this particular function creation to fail, which blocks many other object creations after it.</p>
  
  <p>If I drop the database, and re-run the complete database creation script, I receive no errors before this line, and I still receive the same error message about this datatype being invalid.</p>
  
  <p>What could be causing this?<br>
  If I wanted to "repair" the complete DB creation script, where should I start?</p>
  
  <pre><code>USE MyDB
  GO
  
  SET ANSI_NULLS ON
  GO
  
  SET QUOTED_IDENTIFIER ON
  GO
  
  CREATE FUNCTION [dbo].[Test_FloatToNString] ( )
  
  RETURNS @TestResults TABLE (
                                 [Category]      NVARCHAR ( MAX ) NOT NULL ,
                                 [Example]       NVARCHAR ( MAX ) NOT NULL ,
                                 [Expected]      NVARCHAR ( MAX ) NOT NULL ,
                                 [Actual]        NVARCHAR ( MAX ) NOT NULL ,
                                 [CaseSensitive] BIT              NOT NULL ,
                                 [Tolerance]     NVARCHAR ( MAX ) NOT NULL ,
                                 [Difference]    NVARCHAR ( MAX ) NOT NULL ,
                                 [Passed]        BIT              NOT NULL ,
                                 [Description]   NVARCHAR ( MAX ) NOT NULL
                             )
  AS BEGIN
  
      DECLARE @testData [NStringTestData] ;   /* Error here, see screenshot below */
  
      INSERT INTO @testData   /* etc. */
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/evEyl.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/evEyl.png" alt="enter image description here"></a></p>
  
  <p>Here's what the UDTT looks like, in the complete DB creation script:</p>
  
  <pre><code>USE MyDB
  GO
  
  /* ... */
  
  CREATE TYPE [dbo].[NStringTestData] AS TABLE(
      [Category] [nvarchar](max) NOT NULL,
      [Example] [nvarchar](max) NOT NULL,
      [Expected] [nvarchar](max) NOT NULL,
      [Actual] [nvarchar](max) NOT NULL,
      [CaseSensitive] [bit] NOT NULL,
      [Tolerance] [int] NOT NULL,
      [Description] [nvarchar](max) NOT NULL
  )
  GO
  </code></pre>