title: Must declare the scalar variable of cursor in SQL Server
body: <p>I have this procedure in SQL Server:</p>
  <pre><code>CREATE PROCEDURE [dbo].[QC_data_integrity]  
  AS
      DECLARE @SQLScript VarChar(MAX)
      DECLARE @TableName3 VarChar(MAX)
      DECLARE @Starttime as datetime
      
      DECLARE @column_name VarChar(MAX)
      DECLARE @data_type VarChar(MAX)
      
      DECLARE @sqldatabase VarChar(MAX)
      DECLARE @tbllookup VarChar(MAX)
      DECLARE @sqlserver VarChar(MAX)
      
      SET @Starttime = GETDATE()
              
      DECLARE @Tblname VarChar(MAX)
      DECLARE @sql nvarchar(max)
      
  BEGIN
      
      DECLARE cur_lookup CURSOR FOR
          SELECT * 
          FROM Abc.dbo.Abc_lookup_tables 
          WHERE tablename = 'APFH104'
      
      OPEN cur_lookup;
      FETCH NEXT FROM cur_lookup INTO @sqldatabase, @tbllookup, @sqlserver;
  
      WHILE @@Fetch_status = 0
      BEGIN
          SET @SQL=' use '+@sqldatabase+'
      
          SET @Tblname = ''['+ @sqldatabase +']' + '.dbo.' +'['+ @tbllookup+']''
  
          DECLARE cur_tracking CURSOR FOR
              SELECT column_name, data_type 
              FROM INFORMATION_SCHEMA.COLUMNS 
              WHERE TABLE_NAME = ' + @tbllookup +
              ' ORDER BY ordinal_position
      
          OPEN cur_tracking;
  
          FETCH NEXT FROM cur_tracking INTO @column_name, @data_type;
  
          WHILE @@Fetch_status = 0
          BEGIN
              SELECT @column_name 
              FROM @Tblname
      
              FETCH NEXT FROM cur_tracking INTO @column_name, @data_type;
          END;
  
          CLOSE cur_tracking;
          DEALLOCATE cur_tracking;'
  
          EXEC sp_executesql @sql
  
          FETCH NEXT FROM cur_lookup INTO @sqldatabase, @tbllookup, @sqlserver;
      END;
  
      CLOSE cur_lookup;
      DEALLOCATE cur_lookup;
  END 
  </code></pre>
  <p>I basically need <code>&quot;USE database&quot;</code> statement because this <code>&quot;INFORMATION_SCHEMA.COLUMNS&quot;</code> only works while using <code>&quot;USE database&quot;</code></p>
  <p>So,<code>&quot;use database&quot;</code> is not allowed without keeping inside the quote in procedure since it throws error so I kept in quote <code>set @SQL=''</code>and tried to execute using <code>Exec sp_executesql @sql</code> but
  I am getting errors:</p>
  <blockquote>
  <p>Msg 137, Level 15, State 1, Line 68<br />
  Must declare the scalar variable &quot;@Tblname&quot;.</p>
  <p>Msg 137, Level 15, State 2, Line 73<br />
  Must declare the scalar variable &quot;@column_name&quot;.</p>
  <p>Msg 137, Level 15, State 2, Line 77<br />
  Must declare the scalar variable &quot;@column_name&quot;.</p>
  <p>Msg 137, Level 15, State 2, Line 79<br />
  Must declare the scalar variable &quot;@column_name&quot;.</p>
  </blockquote>
  <p>I needed all the <code>columns name</code> from each table so I needed to use <code>INFORMATION_SCHEMA.COLUMNS</code> and need to print all column names of each table. So, I tried using this way.</p>