What is SQL Server TEXT_LEX

JediSQL 76 Reputation points
2020-10-26T17:52:10.547+00:00

I was writing a T-SQL query with FOR XML RAW(...). I have always use a string literal for the argument for "RAW()". I wanted to see if it would take a variable. When I put in a variable, I got a red squiggly underline (as I pretty much expected). When I hovered it, the pop-up message was "Incorrect syntax near '@VarName'. Expecting STRING, or TEXT_LEX." What is a TEXT_LEX? How do I create a TEXT_LEX?

I know there are many places in T-SQL where a string literal is required and a variable will not work. I am NOT asking why I got the message. I am asking what TEXT_LEX in the message is.

Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,625 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 116.6K Reputation points
    2020-10-27T15:05:30.367+00:00

    Local variables cannot be used in RAW, but it is possible to use scripting variables:

    SELECT … FOR XML RAW($(x))

    These variables are specified in command line of SQLCMD utility:

    SQLCMD -v x="'abc'" …,

    which results in RAW('abc').

    IntelliSense accepts such constructs if you enable the “SQLCMD Mode” in Query menu.

    Maybe TEXT_LEX means such kind of indirect text lexemes.

    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. David Browne - msft 3,846 Reputation points
    2020-10-26T18:33:48.937+00:00

    That's an implementation detail of the TSQL parser used in SSMS for syntax highlighting. The source code is not on GitHub, so I don't know what the parser is looking for exactly there.

    But per the docs, there's nothing other than a literal string that can appear inside the RAW(). https://learn.microsoft.com/en-us/sql/t-sql/queries/select-for-clause-transact-sql?view=sql-server-ver15

    You can see how that parser tokenizes the batch like this:

            static void Main(string[] args)  
            {  
      
                var sql = @"  
    declare @f varchar(20)  
      
    SELECT SalesOrderID  
    FROM Sales.SalesOrderDetail  
    WHERE SalesOrderID = 43892  
      AND ProductID = 758  
    for xml raw(@f)  
    ";  
                              
                var result = Microsoft.SqlServer.Management.SqlParser.Parser.Parser.Parse(sql);  
                foreach (var e in result.Errors)  
                {  
                    Console.WriteLine(e.Message);  
                }  
                foreach (var t in result.Script.Batches.First().Tokens)  
                {  
                    Console.WriteLine($"Token {t.Type}: \"{t.Text}\"");  
                }  
            }  
    
    0 comments No comments

  2. MelissaMa-MSFT 24,196 Reputation points
    2020-10-27T07:28:54.093+00:00

    Hi @JediSQL ,

    Thank you so much for posting here.

    I took a lot of efforts to do some research about TEXT_LEX but failed to find any offcial document about its declaration and creation method.

    Per my understanding,TEXT_LEX is something like TEXT or STRING and you could focus on the solution to this error.

    You could post your complete code if possbile and we could check further.

    Best regards
    Melissa


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  3. JediSQL 76 Reputation points
    2020-10-27T15:42:02.543+00:00

    Thank you, everyone. Thank you anonymous usere-msft and @MelissaMa-MSFT for the time spent on research. Thank you @Viorel for staying away from "why the error" and introducing me to the word lexeme.

    0 comments No comments

Your answer

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