Escape double quotes in SQL 2005/2008

后端 未结 8 2072
渐次进展
渐次进展 2020-12-21 00:41

I have an international company that has recently been added, which is named \"BLA \"BLAHBLAH\" Ltd. (The double quotes are part of the name. )

Whenever a user tries

8条回答
  •  星月不相逢
    2020-12-21 00:52

    This is a bit of a theoretical answer, but maybe it will help. The short version is "use parameters in the query", but it helps to understand the full details.

    In standard SQL, strings are enclosed in single quotes, and embedded single quotes are represented by two single quotes in a row:

    SELECT * FROM SomeWhere
        WHERE SomeThing = 'He said, "Don''t do it!"';
    

    In some dialects of SQL, you can instead close strings in double quotes; you then need to double the double quotes to embed a single instance of a double quote:

    SELECT * FROM SomeWhere
        WHERE SomeThing = "He said, ""Don't do it!""';
    

    It isn't clear from the question whether the company name includes the outer double quotes as well as the middle one, or if it just contains the middle one. However, in principle, the rules are the same. Assuming all three double quotes are required, and using single quotes in the SQL - much easier in this context:

    SELECT c.companyID, c.companyName, c.dateAdded, count(cm.maxID) as NumDirect
        FROM RussoundGeneral.dbo.Company c  
             LEFT JOIN RussoundGeneral.dbo.CompanyMax cm
                    ON (cm.companyId = c.companyId and cm.maxID is not null)  
        WHERE CONTAINS ( companyName,  '"BLAH "BLAHBLAH" Ltd.' )
        GROUP BY c.companyID, c.companyName, c.dateAdded  
        ORDER BY c.companyName ASC;
    

    Using double quotes:

    SELECT c.companyID, c.companyName, c.dateAdded, count(cm.maxID) as NumDirect
        FROM RussoundGeneral.dbo.Company c  
             LEFT JOIN RussoundGeneral.dbo.CompanyMax cm
                    ON (cm.companyId = c.companyId and cm.maxID is not null)  
        WHERE CONTAINS ( companyName,  """BLAH ""BLAHBLAH"" Ltd." )
        GROUP BY c.companyID, c.companyName, c.dateAdded  
        ORDER BY c.companyName ASC;
    

    If you are building strings in a programming language, then you have to worry about getting these quotes past whatever evaluates strings in your programming language. For example, if you were building a string literal in C, you'd have to escape the double quotes with backslashes:

    static const char sql_stmt[] =
    "SELECT c.companyID, c.companyName, c.dateAdded,\n"
    "       COUNT(cm.maxID) AS NumDirect\n"
    "    FROM RussoundGeneral.dbo.Company c\n"
    "         LEFT JOIN RussoundGeneral.dbo.CompanyMax cm\n"
    "                ON (cm.companyId = c.companyId AND cm.maxID IS NOT NULL)\n"  
    "    WHERE CONTAINS(companyName,  \"\"\"BLAH \"\"BLAHBLAH\"\" Ltd.\")\n"
    "    GROUP BY c.companyID, c.companyName, c.dateAdded\n"
    "    ORDER BY c.companyName ASC";
    

    On the other hand, if you read the data from the user - such as the company name, then you simply have to ensure what is read is correctly quoted.

    Those who said "use parameters" are right - it is much easier and more reliable and less vulnerable to SQL injection attacks (see XKCD if you haven't already see it). But if you understand the fundamentals, you can adapt to the actual requirements of your system.

    Final note: in standard SQL, double quotes enclose 'delimited identifiers'. That is, the double quotes surround a name which must be treated as the name of something in the database, not as a string literal. In MS SQL Server, the [square brackets] serve the same purpose; what is between the brackets is the name of a column or whatever inside the database. Many systems are more flexible than that; not all systems are the same in how they deviate from the standard.

提交回复
热议问题