简体   繁体   English

如何避免 SQL 查询中的硬编码

[英]How can I avoid hard coding in SQL Query

I have a query like Select '16453842' AS ACCOUNT, I want to change this Account number to a dynamic one.我有一个像 Select '16453842' AS ACCOUNT 这样的查询,我想将此帐号更改为动态帐号。 Can anyone tell me what are some possible ways to do it?谁能告诉我有哪些可能的方法来做到这一点? The scenario is like, I would also like to accommodate other values as well for the ACCOUNT.这种情况就像,我还想为 ACCOUNT 提供其他值。 So that multiple values can be used for ACCOUNT.这样就可以将多个值用于 ACCOUNT。

the Nodejs code uses that sql in below code by carrierData.sql Nodejs 代码在以下代码中使用该 sql 由carrierData.sql

writeDebug({'shipment': shipment, 'carrier': carrier, 'message': 'reading sql file: ' + carrierData.sql});
  fs.readFile(carrierData.sql, 'utf8', function (err, sql) {

The carrierData is coming from a json file from where the sql contains the path and name of the SQL which it is going to use. carrierData 来自一个 json 文件,其中 sql 包含它将要使用的 SQL 的路径和名称。 And finally the SQL file which have the query runs a query like below SELECT 'T' AS RECORD , '16453842' AS ACCOUNT最后,具有查询的 SQL 文件运行如下查询 SELECT 'T' AS RECORD , '16453842' AS ACCOUNT

and here lies my problem as we have some additional ACCOUNT numbers as well which we would like to accommodate.这就是我的问题,因为我们还有一些我们希望容纳的额外帐户号码。 And the service starts by node server.js which will call the workers.js file which contains the code that I pasted above.该服务由node server.js启动,它将调用包含我在上面粘贴的代码的workers.js文件。 So please let me know what can be the possible ways to do this所以请让我知道有什么可能的方法来做到这一点

Here are things to research:以下是要研究的内容:

  1. Using bind variables .使用绑定变量 These are used for security and for performance when a statement is executed multiple times.当一个语句被多次执行时,它们用于安全性和性能。

  2. Using multiple values in IN clauses . IN子句中使用 多个值

  3. Using executeMany() if you are loading data into the database.如果要将数据加载到数据库中,请使用executeMany()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM