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?
-
Array.prototype.join() or a backslash: var s = 'line1\ line2\ line3';Alina Loi– Alina Loi2017-04-25 13:20:24 +00:00Commented Apr 25, 2017 at 13:20
-
2If 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.andreim– andreim2017-04-25 13:21:35 +00:00Commented Apr 25, 2017 at 13:21
Add a comment
|
1 Answer
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
2 Comments
sortinousn
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.
Shahar Hadas
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.