Share via

how to correect error message

Michael Ashby 40 Reputation points
2026-03-04T15:20:19.3633333+00:00

I am using the following to create a database to import data from a csv file.

DECLARE @sql NVARCHAR(MAX)

DECLARE @filePath NVARCHAR(MAX) = "C:\Users\aussi\OneDrive\Desktop! Complete Family - BMDs_people_EN.csv"

DECLARE @tableName NVARCHAR(MAX) = 'dbo.Personal_Details'

DECLARE @colString NVARCHAR(MAX)

SET @sql = 'SELECT @res = LEFT(BulkColumn, CHARINDEX(CHAR(10),BulkColumn)) FROM OPENROWSET(BULK ''' + @filePath + ''', SINGLE_CLOB) AS x'

exec sp_executesql @sql, N'@res NVARCHAR(MAX) output', @colString output;

SELECT @sql = 'DROP TABLE IF EXISTS ' + @tableName + '; CREATE TABLE [dbo].' + @tableName + ' '

FROM (

SELECT ' [' + value + '] nvarchar(max) ' as name

FROM STRING_SPLIT(@colString, ',')

) t

EXECUTE(@sql)

When I excute the code i get the following error message: Msg 137, Level 15, State 2, Line 15

Must declare the scalar variable "@sql".

Can anyone tell what I am doing wrong,

Many thanks

SQL Server Database Engine
{count} votes

Answer accepted by question author
  1. Q&A Assist
    2026-03-04T15:20:31.0333333+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    The error occurs because @sql goes out of scope or is referenced in a batch where it has not been declared. In the posted script there are also other issues that will prevent it from working as intended.

    Key problems and fixes:

    1. Use single quotes for string literals in T‑SQL
         DECLARE @filePath NVARCHAR(MAX) = 'C:\Users\aussi\OneDrive\Desktop\! Complete Family - BMDs_people_EN.csv';
      
    2. Ensure everything is in a single batch and @sql is declared before every use
         DECLARE @sql       NVARCHAR(MAX);
         DECLARE @filePath  NVARCHAR(MAX) = 'C:\Users\aussi\OneDrive\Desktop\! Complete Family - BMDs_people_EN.csv';
         DECLARE @tableName NVARCHAR(256) = 'Personal_Details';
         DECLARE @colString NVARCHAR(MAX);
         
         -- Read header line from file
         SET @sql = N'SELECT @res = LEFT(BulkColumn, CHARINDEX(CHAR(10), BulkColumn))
                      FROM OPENROWSET(BULK ''' + @filePath + ''', SINGLE_CLOB) AS x;';
         
         EXEC sp_executesql
             @sql,
             N'@res NVARCHAR(MAX) OUTPUT',
             @res = @colString OUTPUT;
         
         -- Build CREATE TABLE statement
         SELECT @sql = N'DROP TABLE IF EXISTS dbo.' + QUOTENAME(@tableName) + N';
                         CREATE TABLE dbo.' + QUOTENAME(@tableName) + N' (' +
                         STRING_AGG(N'[' + value + N'] NVARCHAR(MAX)', N',') + N');'
         FROM STRING_SPLIT(@colString, ',');
         
         EXEC(@sql);
      
      Important points:
      • DECLARE @sql appears once at the top and is reused; there is no GO between declarations and uses.
      • @colString is passed as an OUTPUT parameter (@res = @colString OUTPUT).
      • @tableName is used without dbo. prefix duplication and is quoted with QUOTENAME to avoid invalid identifiers.
    3. Avoid GO between declarations and uses If there is a GO between DECLARE @sql and EXECUTE(@sql), the variable is not visible in the later batch and SQL Server raises “Must declare the scalar variable @sql.” Remove any GO separators inside this script.

    Once these changes are applied and the script is kept in a single batch, the @sql variable will be recognized and the error Msg 137 will be resolved.


    References:

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.