title: Merge statement inside if condition
body: <p>I have this script:</p>
  
  <pre><code>DECLARE @TenantId INT = 1
  IF EXISTS(SELECT 1 
              FROM [DB1].[Client1].information_schema.tables 
             WHERE table_type='BASE TABLE' 
               AND [TABLE_SCHEMA] = 'dbo'
               AND table_name='MyTable')
  BEGIN
  
      MERGE info.[MyTable] AS t
      USING (
              SELECT [Id],
                     [Name]                  
                FROM [DB1].[Client1].dbo.MyTable
            ) s   
         ON (t.[Id] = s.[Id] AND t.[TenantId] = @TenantId)
  
      WHEN MATCHED THEN 
          UPDATE SET      
              t.[Name] = s.[Name]
  
      WHEN NOT MATCHED THEN 
          INSERT ([Id], [TenantId], [Name])
          VALUES (s.[Id], @TenantId, s.[Name]);
  
  END  
  </code></pre>
  
  <p>Why is giving me an error on the select inside the USING, if the table doesn't exists?, I cannot run the script when the table doesn't exists. If table exists is fine.</p>