传递参数设置为SQL SELECT语句子句中的行为怪异。 [英] Passing parameter to SQL select statement IN clause acts weird.

查看:141
本文介绍了传递参数设置为SQL SELECT语句子句中的行为怪异。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经得到了下面的查询返回2条记录(在DataSet中的查询生成器)

I've got the following query that returns 2 records (in DataSet's query builder)

SELECT        EmpID, Name, id
FROM          Users
WHERE        (CAST(id AS Varchar(20)) IN ('5688','5689'))

现在如果我不从后面的代码传递参数,而不是相同的查询:字符串参数='5688','5689';它返回null。

Now if I do the same query passing the parameter instead from code behind: String param = "'5688','5689'"; it returns null.

WHERE        (CAST(id AS Varchar(20)) IN (@param))

我试图起飞的第一个和最后一个,但是这并没有使探源。

I tried taking off the very first and last ', but that did not make a diffrence.

!!! id是一个独特的PK !!!

!!!id is a unique PK!!!

任何人有一个线索?

推荐答案

我找到解决的办法很简单,这就像一个魅力,而且也没有必要SPS或其他功能;

The solution I found is quite simple, this works like a charm and there's no need for sps or other functions;

SQL:

SELECT whatever 
FROM whatever
WHERE (PATINDEX('%''' + CAST(id AS Varchar(20)) + '''%', @param) > 0)

C#:

String param = "'''1234'',''4567'''";
dataTable1 = tableAdapter1.getYourValues(param);

这篇关于传递参数设置为SQL SELECT语句子句中的行为怪异。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆