简体   繁体   中英

SQL Server: error Msg 156, Level 15, State 1, Line 12 Incorrect syntax near the keyword 'AS'

Getting an error, I'm sure it's syntax related.

DECLARE @Rank AS INT = 0;

SELECT
    asn_key,
    asn_code,
    asn_name, 
    asn_eweb_description_ext,
    @Rank = CASE 
               WHEN asn_name = 'AONE' THEN 1
               WHEN asn_name = 'ACHI' THEN 1
               WHEN asn_name = 'ATLARGE' THEN 1
               WHEN asn_name = 'IFD' THEN 1
               ELSE 0 
            END AS ASN_Rank
FROM
    mb_association
JOIN
    mb_association_ext (nolock) ON asn_key = asn_key_ext
WHERE
    asn_eweb_description_ext IS NOT NULL
ORDER BY 
    ASN_Rank ASC

I need to "group" my associations. If its not one of those associations listed then it needs to be listed first in ASC order else I need it to be listed lastly in ASC order. It looks like its telling me I have a syntax error but I'm not sure why its giving me the error. I don't see any issues with the code and I've reviewed similar code.

Edit: I tried to approach it a different direction but got another error. I'm not very good with group by clause.

New Code:

select asn_key, asn_code, asn_name,  asn_eweb_description_ext
from mb_association join
     mb_association_ext (nolock)
     on asn_key = asn_key_ext
where asn_eweb_description_ext is not null
--order by (CASE WHEN asn_name in ('AONE', 'ACHI', 'ATLARGE', 'IFD') then 1 else 0 end);
GROUP BY (CASE WHEN asn_name in ('AONE', 'ACHI', 'ATLARGE', 'IFD') then 1 else 0 end);

Error: Msg 8120, Level 16, State 1, Line 1 Column 'mb_association.asn_key' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

You can't assign value to variable twice with alias :

SELECT asn_key, asn_code, asn_name, asn_eweb_description_ext,
       (CASE WHEN asn_name IN ('AONE', 'ACHI', 'ATLARGE', 'IFD') 
             THEN 1 ELSE 0 
        END) AS ASN_Rank
FROM mb_association JOIN 
     mb_association_ext 
     ON asn_key = asn_key_ext
WHERE asn_eweb_description_ext IS NOT NULL
ORDER BY ASN_Rank ASC;

Also, i have just simplified the multiple WHEN with IN clause.

EDIT : Your syntax error fixes with first version of query, if you want this as in particular order then i would do :

SELECT asn_key, asn_code, asn_name, asn_eweb_description_ext, ASN_Rank 
FROM mb_association JOIN 
     mb_association_ext 
     ON asn_key = asn_key_ext CROSS APPLY
     ( VALUES (CASE WHEN asn_name IN ('AONE', 'ACHI', 'ATLARGE', 'IFD') 
                    THEN 1 ELSE 0 
               END) 
     ) t(ASN_Rank)
WHERE asn_eweb_description_ext IS NOT NULL
ORDER BY ASN_Rank ASC;

You seem to want something like this:

select asn_key, asn_code, asn_name,  asn_eweb_description_ext
from mb_association join
     mb_association_ext (nolock)
     on asn_key = asn_key_ext
where asn_eweb_description_ext is not null
order by (case when asn_name in ('AONE', 'ACHI', 'ATLARGE', 'IFD') then 1 else 0 end);

I'm not sure why you would think of using variables for this. Variables aren't appropriate. And, SQL Server does not allow you to assign a value to a variable in a query that returns a result set -- return results or assign a variable, but not both.

Thanks to my co worker getting me on the right track I was able to resolve the issue and figure it out.

SQL:

    SELECT 
    asn_key, 
    asn_code, 
    asn_name, 
    asn_eweb_description_ext,
    CASE WHEN asn_code NOT IN ('AHE', 'SHSMD', 'AHRMM', 'ASHRM', 'ASHHRA', 'AHVRP') THEN asn_code END AS Other_Individual_Membership_Organizations,
    CASE WHEN asn_code IN ('AHE', 'SHSMD', 'AHRMM', 'ASHRM', 'ASHHRA', 'AHVRP') THEN asn_code END AS Professional_Membership_Groups
FROM 
    mb_association 
    JOIN mb_association_ext (nolock) ON asn_key=asn_key_ext
WHERE 
    asn_eweb_description_ext IS NOT NULL
ORDER BY 
    Other_Individual_Membership_Organizations, Professional_Membership_Groups ASC

Its pretty stright forward now that I see the working code. Using the CASE statement to filter out the first set of data and use the second CASE statement to filter the data I want for second column data.

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