0

Why can't I proceed non-English characters with N' when specifying the value for an input parameter in an SAP HANA "SQL" query, while I can for an input variable?

For example, the following query works (notice the N' in the WHERE clause generated by an input variable):

SELECT 
       "NDATA", "DATA", "CC_NON_ENGLISH"
      , sum("ID") AS "ID" 
FROM 
    "_SYS_BIC"."Test/MY_VIEW" 
    ('PLACEHOLDER' = ('$CC_PARAM$', 'दिल्ली भारत की राजधानी है.')) 
WHERE 
    (("NDATA" IN (N'दिल्ली भारत की राजधानी है.'))) 
GROUP BY 
      "NDATA", "DATA", "CC_NON_ENGLISH"

However, if I add N' in front of the value for the CC_PARAM variable, I get a syntax error.

 SELECT 
        "NDATA", "DATA", "CC_NON_ENGLISH"
      , sum("ID") AS "ID" 
 FROM 
       "_SYS_BIC"."Test/MY_VIEW" 
         ('PLACEHOLDER' = ('$CC_PARAM$', N'दिल्ली भारत की राजधानी है.')) 
 WHERE (("NDATA" IN (N'दिल्ली भारत की राजधानी है.'))) 
 GROUP BY  
         "NDATA", "DATA", "CC_NON_ENGLISH" 

The resulting error:

Could not execute 'SELECT "NDATA", "DATA", "CC_NON_ENGLISH", sum("ID") AS "ID" FROM ...' 
 SAP DBTech JDBC: [257]: sql syntax error: 
 incorrect syntax near "दिल्ली भारत की राजधानी है.": line 7 col 3 (at pos 173)

In general, when writing SQL queries in HANA, when does a Unicode string need to be preceded by N' and when doesn't it?

I notice that I can execute the query in question without any N' at all and it works fine, so what's going on here?

1 Answer 1

1

The N' modifier for string literals marks those string literals as Unicode strings for SQL. This is nearly the same as providing the string with TO_NVARCHAR() in the sense that the database should treat the resulting value as a Unicode string, except that for the literal version, the client guarantees that the string is a Unicode string.

This is SQL standard behaviour.

For the SAP HANA specific (read proprietary) PLACEHOLDER clause (used to provide the values for calculation view parameters) always takes input strings as Unicode strings. Also, it does not allow for the N' modifier (or SQL functions for that matter).

In short: when you want to specify Unicode string literals in SQL you can/should use the N' modifier. When you use SAP HANA specific syntax and commands, you implicitly always provide a Unicode string and must not use the N' modifier.

9
  • I see. So the reason N' is allowed for string literals that we use in the WHERE clause (i.e. filters derived from input variables) is that here HANA sticks to the SQL standard. However, since the PLACEHOLDER clause is SAP HANA proprietary, the HANA team made the call to deviate from the standard, since we're always passing in Unicode strings to the PLACEHOLDER clause (i.e. it's implicit that these literals are always Unicode strings)
    – Adam
    Dec 12, 2018 at 0:34
  • Also, why does WHERE (("NDATA" IN ('दिल्ली भारत की राजधानी है.'))) work? Don't I need to explictly specify that my filter string literal is unicode, since this is standard SQL?
    – Adam
    Dec 12, 2018 at 0:37
  • Ok, so for proprietary features, there is no standard - it's always the vendor that implements the feature who decides how it works. Remember old Oracle (+) outer join syntax? Same thing.
    – Lars Br.
    Dec 12, 2018 at 0:57
  • As for why you don't need to use the N' modifier: HANA handles dealing with string types rather flexible. If your client software deals with your unicode string correctly even without the modifier, HANA doesn't complain. That's the case e.g. for JAVA/JAVAScript based clients, that have no ASCII string type. For ODBC this is different, as it does make a different there, how the client handles this data.
    – Lars Br.
    Dec 12, 2018 at 1:01
  • When you say it makes a difference for ODBC-based clients, do you mean that ODBC-based clients can represent strings either way (i.e. a CHAR or WCHAR), so internally the ODBC client would need to be consistent? However, I imagine that when it comes to passing strings to the HANA server, ODBC clients could still choose to include the N' or not, since HANA is flexible in this respect.
    – Adam
    Dec 12, 2018 at 17:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.