sql - 如何以字符串格式编写 SQL 命令
问题描述
如何转换此代码:
SELECT * FROM DB
WHERE field LIKE N'%'+RTRIM(LTRIM('98'))+N'%'
至:
SET @stringWhere= ' SELECT * FROM DB AND field LIKE '+N'%' +RTRIM(LTRIM(@field))+ N'%';
解决方案
这里有点猜测。请输入下面的代码。并确保您查看代码中的注释,因为有几件事可以改进。
declare @field nvarchar(10) = N'98'
select * --don't use *, select ONLY the columns you need
from DB
where Name like '%' + @field+ '%' --be careful here, this is a nonSARGable predicate with the leading wildcard. As such all indexes will be rendered useless.
推荐阅读
- node.js - Mongoose 不保存我的数据,错误消息:无法读取未定义的属性 'then'
- sql - 如何在 laravel 的 HASMANY 关系中使用 WHERE 生成 SUM()?任何帮助表示赞赏
- javascript - 每 x 秒更改一次内部 html
- python - 随机森林作为广义加法模型
- json - 无效的 JSON 双引号错误。如果我已经有双引号,我该如何更正我的语法?
- python-3.x - 为什么 Pillow Python 在将 CMYK 图像转换为 PDF 时会反转其颜色
- javascript - React Typescript createContext 类型问题
- r - 如何在 R Studio 中为向量中的每个值添加 1 分钟?
- kubernetes - kubectl 仅部署 yaml 中的一个组件
- python - Python 脚本无法以 www-data 的身份访问库