Apostophes in SQL statement

  • Hi,

    I am trying to execute this statement and I suppose there is a problem in apostrophes, but I don't know how to make it work.

    Exec ('SELECT DISTINCT ' + @DefaultSelect + ' FROM ' + @FromClause + ' WHERE FirstName LIKE 'Peter' ');

    Any suggestions?

    Thanks

  • dtopicdragovic (10/28/2010)


    Hi,

    I am trying to execute this statement and I suppose there is a problem in apostrophes, but I don't know how to make it work.

    Exec ('SELECT DISTINCT ' + @DefaultSelect + ' FROM ' + @FromClause + ' WHERE FirstName LIKE 'Peter' ');

    Any suggestions?

    Thanks

    When you are trying to use quotes with a quoted string, you need to double-up on the embedded quotes. Specifically around the constant in the where clause:

    Exec ('SELECT DISTINCT ' + @DefaultSelect + ' FROM ' + @FromClause + ' WHERE FirstName LIKE ''Peter'' ');

    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

  • It worked !

    Thanks a lot!

Viewing 3 posts - 1 through 2 (of 2 total)

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