0

I have the following code snippet from a SQL Server Stored Procedure.

    DECLARE @sql nvarchar(Max)

    SET @sql = N'

    WITH CTE AS
    (
      SELECT * FROM #TEMP WHERE ...
    ),

    Track AS
    (
      SELECT *,
              CASE WHEN 1B1 = "Track" AND (QTRK_1B1 = "2D QT" OR QTRK_1B1 = "3D QT") THEN "Yes" ELSE "No" END AS Tracking
      FROM CTE
    ),
    ... '

Exec sp_executesql @sql, N'@AirTarget nvarchar(25), @GroundTracker nvarchar(25)', @AirTarget, @GroundTracker

I am not sure what I am doing wrong here. When I execute the stored procedure, I am getting errors like:

Invalid Column Name 'Track'

Invalid Column Name '2D QT'

Invalid Column Name '3D QT'

Invalid Column Name 'Yes'

Invalid Column Name 'No'

But those are not column names from the table. I have Googled and cannot figure out what I am doing wrong.

I have tried surrounding those values with single quotes, and I have tried removing the quotes from around those values and nothing works. Any help would be appreciated

0

2 Answers 2

4

SQL use single quote ' for string

CASE 
    WHEN 1B1 = ''Track'' AND (QTRK_1B1 = ''2D QT'' OR QTRK_1B1 = ''3D QT'') THEN ''Yes'' 
    ELSE ''No'' 
END AS Tracking
Sign up to request clarification or add additional context in comments.

Comments

1

They all definitely should be single quotes - all places where you have double-quotes. If you do that it should at least give you a different message.

You'll also need to wrap 1B1 in square brackets like [1B1] as it won't like a column name starting with a number in a CASE statement.

1 Comment

Thanks for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.