-3

How do you define variable value that includes new line?

const statement = "SELECT *
                   FROM `users` u;";

enter image description here

It shows error newline in string.

Henry
  • 42,982
  • 7
  • 68
  • 84

1 Answers1

3

You would normally use a raw string literal like this

const s = `first line
second line`

However, this is not possible in your case since the string itself contains backticks. In this case I would use + to connect the lines:

const statement = "SELECT *" +
                  " FROM `users` u;";

The string value in this example does not contain a newline (but that's ok for SQL). If you really want a newline in the string, use the escape sequence \n.

const s = "first line\nsecond line"
Henry
  • 42,982
  • 7
  • 68
  • 84
  • 1
    It’s also OK to remove the backticks from the SQL, since “users” is not a reserved word in any flavour of SQL. MySQL unfortunately dumps schema with every entity name backtick wrapped, whether it needs it or not. – Bohemian Feb 14 '21 at 07:29