title: create several views in same batch in SQL
body: <p>I try to run a script who create a view for each table of a specific database. 
  I found this link : 
  <a href="https://stackoverflow.com/questions/23166423/how-to-create-view-for-all-tables-in-database">How to create view for all tables in database?</a></p>
  
  <p>and apparently, in order to work I need to add the 'GO' statement and a carriage return. So here is what I did </p>
  
  <pre><code>DECLARE @SQL NVARCHAR(MAX)
  SET @SQL = ''
  
  SELECT
      @SQL = @SQL+
      N'CREATE VIEW [v2_' + t.name + N'] AS SELECT ' + 
          STUFF(
              (SELECT N',' + c.name
                 FROM
                    sys.columns AS c
                 WHERE 
                   c.OBJECT_ID = t.OBJECT_ID
                 ORDER BY
                   column_id
                 FOR XML PATH(''), TYPE).value('.',N'nvarchar(max)')
          ,1,1,N'')
          + N' FROM [' + t.name + N'] [\r\n]go[\r\n]'
  
  FROM
      sys.tables AS t
  WHERE
      t.schema_id ='1'
  
  SET @SQL = REPLACE(@SQL,'[\r\n]',CHAR(13)+CHAR(10))
  PRINT @SQL
  
  EXECUTE (@SQL)
  </code></pre>
  
  <p>So the print result is what I expect : </p>
  
  <pre><code>CREATE VIEW ***** AS SELECT Parent_Path,id,a2ea,userLabel,DATE,HEURE,REF_EQT,COMMANDE_REF,LOG FROM ***** 
  go
  CREATE VIEW ***** AS SELECT AlarmIdentifier,Class,Category,Time,ObjectOfReference,AlarmText,DATE,HEURE,REF_EQT,SS_EQT,Carte_EQT,COMMANDE_REF,LOG FROM ***** 
  go
  CREATE VIEW ***** AS SELECT CELL,MCC,MNC,LAC,CID,BSC,CO,EA,RO,NCS,DATE,HEURE,REF_EQT,SS_EQT,Carte_EQT,COMMANDE_REF,LOG FROM ***** 
  go
  CREATE VIEW ***** AS SELECT RNC,RNCID,R1,R2,GLCNID,RNCCODEC,TB,DATE,HEURE,REF_EQT,SS_EQT,Carte_EQT,COMMANDE_REF,LOG FROM ***** 
  go
  CREATE VIEW ***** AS SELECT MGG,MG,RESTRICTED,DEFAULT,MISC,MGP,WF,DATE,HEURE,REF_EQT,SS_EQT,Carte_EQT,COMMANDE_REF,LOG FROM [NRGGP] 
  go
  CREATE VIEW ***** AS SELECT SNT,SNTV,SNTP,DIP,DEV,DEVP,SNTINL,EQLEV,PROT,SDIP,SUBSNT,DEFPST,EXTP,MG,DATE,HEURE,REF_EQT,COMMANDE_REF,LOG FROM *****
  go
  CREATE VIEW ***** AS SELECT RefreshLog_Id,Date,Etat,DateTransfert,Transfert FROM ***** 
  go
  </code></pre>
  
  <p>but the exec line returns: </p>
  
  <blockquote>
    <p>Msg 156, Level 15, State 1, Procedure *****, Line 3 [Batch Start Line 0]<br>
    Incorrect syntax near the keyword 'CREATE'.</p>
    
    <p>Msg 111, Level 15, State 1, Procedure *****, Line 3 [Batch Start Line 0]<br>
    'CREATE VIEW' must be the first statement in a query batch.</p>
    
    <p>Msg 111, Level 15, State 1, Procedure *****, Line 5 [Batch Start Line 0]<br>
    'CREATE VIEW' must be the first statement in a query batch.</p>
    
    <p>Msg 111, Level 15, State 1, Procedure *****, Line 7 [Batch Start Line 0]</p>
  </blockquote>
  
  <p>Should I create a schema ? Any solution to my problem ? </p>
  
  <p>Thank you very much </p>