Why does SQL Server consider a value to be a column name??

  • Hi,

    This should be a really simple, straight forward query, but apparently it's not. The error I get is "Invalid column name 'A'. I've put it in quotes and not in quotes and I left spaces around the equal sign and I've eliminated them. Can anyone tell me what's wrong with the SELECT statement below?

    SELECT * from firms WHERE firm_id= 100002

    AND active_firm = 1 AND firm_id != 100092

    AND trust_id = A;

    Thanks,

    Rob

  • Because it needs single quotes to identify strings. Most (or all) languages need quotes to identify strings, so there shouldn't be a big surprise. Without quotes, there would be no way to know if it's a column name or a string.

    SELECT * from firms WHERE firm_id= 100002

    AND active_firm = 1 AND firm_id != 100092

    AND trust_id = 'A';

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • That was it. Thanks!!!!

  • Are you seriously asking? Or, is it a comment on your own answer? No, I don't take any code off the internet without testing it. It's been a slightly over a year and yes, I forgot that I need to single quote strings.

    ~ Rob

  • That's his signature... it's put on all of the posts automatically.

    Wayne
    Microsoft Certified Master: SQL Server 2008
    Author - SQL Server T-SQL Recipes


    If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
    Links:
    For better assistance in answering your questions
    Performance Problems
    Common date/time routines
    Understanding and Using APPLY Part 1 & Part 2

  • WayneS (7/22/2015)


    That's his signature... it's put on all of the posts automatically.

    It's not the first time an OP has shared his opinion about the signature. 🙂



    Alvin Ramard
    Memphis PASS Chapter[/url]

    All my SSC forum answers come with a money back guarantee. If you didn't like the answer then I'll gladly refund what you paid for it.

    For best practices on asking questions, please read the following article: Forum Etiquette: How to post data/code on a forum to get the best help[/url]

  • Alvin Ramard (7/22/2015)


    WayneS (7/22/2015)


    That's his signature... it's put on all of the posts automatically.

    It's not the first time an OP has shared his opinion about the signature. 🙂

    I just edited my signature to reduce confusions. I'll hope that it works.

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2

Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic. Login to reply