[英]nodejs mssql syntax error
当我将以下查询与node-mssql模块一起使用时,出现错误,指出存在一个
Invalid column name 'L'.
由于某种原因,它认为like语句中的值是列名。
'SELECT TOP 10 * From [Products] WHERE [Code] LIKE "%LO%"
我需要使用某种特殊的语法吗
完整代码(以mssql npm为例):
var express = require('express');
var app = express();
app.get('/', function (req, res) {
var sql = require("mssql");
// config for your database
var config = {
user: 'username',
password: 'password',
server: '192.168.0.165\\database', // You can use 'localhost\\instance' to connect to named instance
database: 'Products',
options: {
encrypt: false // Use this if you're on Windows Azure
}
}
// connect to your database
sql.connect(config, function (err) {
if (err) console.log(err);
// create Request object
var request = new sql.Request();
// query to the database and get the records
request.query('select TOP 10 * From [Products] WHERE [Code] LIKE "%LO%"', function (err, recordset) {
if (err) console.log(err)
var items = [];
recordset.forEach(function(row){
items.push(row.Code);
})
res.send(items);
});
});
});
var server = app.listen(5000, function () {
console.log('Server is running..');
});
在sql语句中使用单引号时,必须转义'字符。
例如:
'SELECT TOP 10 * FROM [Products] WHERE [Code] LIKE \'%LO%\''
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.