首页 > 解决方案 > 按 charindex >0 和 SQL concat 排序

问题描述

当 ID 在数组中时,我想通过设置优先级来订购 Recordset:

这是包含一系列 ID的逗号分隔的myString :,1,2,4,6,8,12,34,

我使用 concat 在 ID 前后添加逗号,以便将其与字符串进行比较:

sql="select * from customers order by case when charindex( concat(','," & id & ",',') , '" & myString & "' )>0 then 1 else 0 end desc"

我收到语法错误消息。我不确定concat在深层结构中使用的 SQL 结构是否有错误,或者使用引号和双引号是否有错误?

标签: sqlsql-servervbscript

解决方案


我在尝试回答@Diado 评论时发现了问题。参数 ID 应该在 SQL 上下文中使用,而不是作为 VbScript 上下文中的变量。所以我已将查询更改为此并且它有效:

sql="select * from customers order by case when charindex( concat(',',id,',') , '" & myString & "' )>0 then 1 else 0 end desc"

推荐阅读