SQLTeam.com | Weblogs | Forums

SQL Strings to Lua Strings


I noticed that SQL strings are defined using single quotes:

While in Lua, strings are defined using double quotes:

I want to retrieve elements from a SQL (table) database using Lua, but how can I change the way the string is defined? When I use:

In Lua or:

In SQL, it causes an syntax error. How can I make it such that strings convert properly into Lua when retrieving data from SQL?


Double-quotes are used in MS SQL (and Oracle and others) to indicate a column name - so-called QUOTED_IDENTIFIER. Nowadays that seems to be little used in MS SQL in favour of using square brackets to delimit column names thus: [MyColumnName] - although no delimiters are needed at all unless the column name includes spaces, punctuation or other unusual characters, or is a reserved word.

You can probably use double quotes for strings if you turn QUOTED_IDENTIFIERS off, but I would strongly recommend that use single quotes instead as that is the default in SQL Server, and you will probably need QUOTED_IDENTIFIERS ON at some point (from memory things like Indexed Views require that) in which case if you have double-quotes delimiting your strings you will have a whole lot of grief having to retrospectively change them :frowning:



I am not familiar with Lua, but C#/.Net uses double-quotes for string literals. When you pass strings between SQL Server and .Net, you are passing objects - of type CHAR, VARCHAR, NVARCHAR etc. on the SQL side and System.String in .Net. The objects themselves do not have any single quotes or double quotes in them.

For example, the following are equivalent:


In C#
string CSharpParam = "ABCD'123""XYZ";

If you were to pass @SQLParam to C# or pass CSharpParam to SQL, the data passed will be the same.

Maybe I did not understand your use-case, but usually the character used to denote string literals is not an issue.


Thank you. I was able to use something in Lua in the end to solve this issue, but your explanations really helped further my understanding of SQL