• There's a bug in this script at approximately line 82 - details below.

    At line 56, you go to the trouble of figuring out the OBJECT_ID for the table using the schema name, etc:

    SELECT

    @TABLE_ID = [object_id]

    FROM sys.objects

    WHERE [type] IN ('S','U','V')

    AND [name] <> 'dtproperties'

    AND [name] = @TBLNAME

    AND [schema_id] = schema_id(@SCHEMANAME) ;

    And then a bit farther down, you cancel out all that good work, and re-set the TABLE_ID. If your table is in a schema other than the dbo schema, the @TABLE_ID gets set to NULL, and the stored procedure returns no results.

    -- Valid Table, Continue Processing

    SELECT @FINALSQL = 'CREATE TABLE [' + @SCHEMANAME + '].[' + UPPER(@TBLNAME) + '] ( '

    SELECT @TABLE_ID = OBJECT_ID(@TBLNAME)

    David