当我对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..');
});
答案 0 :(得分:1)
在sql语句中使用单引号时,必须转义'字符。
例如:
'SELECT TOP 10 * FROM [Products] WHERE [Code] LIKE \'%LO%\''