1

I have a large mysql select query (Over 100 lines) that I need to run in a node.js app. The fields in my sql statement use backticks ` so I'm not sure if I can use ES6's multi-line string feature. Is there any other method I can use to wrap my sql query without having to concatenate each line?

2
  • Array.prototype.join() or a backslash: var s = 'line1\ line2\ line3'; Commented Apr 25, 2017 at 13:20
  • 2
    If you have lots of lines of SQL you might consider creating a Stored Procedure. It will be easier to maintain, improve and test by your dba. Your backticks should work in recent node versions as they are supported. Commented Apr 25, 2017 at 13:21

1 Answer 1

4

I would suggest saving the long SQL to a .sql file (and keep under version control), and then reading it into a variable once on load.

This way you can also format or beautify the SQL even more.

But, adding to another comment before, long SQLs are heavy on the SQL parser so either use Prepared Statements (if available on your DB engine), Stored Procedures or even a View

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! I used this and fs to read the query from an external .sql file. I would've used a Stored Procedure but I don't have the permissions in my organization to create stored procedures in our data warehouse.
what about prepared statements? with a 100 lines ... it might still be faster to check if the statement exists (and if not create it). It can give you a major performance boost if you calling this SQL many times.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.