简体   繁体   中英

SQL Server Invalid Column Name and String Value

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM