加入收藏 | 设为首页 | 会员中心 | 我要投稿 鄂州站长网 (https://www.0711zz.com/)- 数据分析、网络、云渲染、应用安全、大数据!
当前位置: 首页 > 数据库 > MsSql > 正文

SqlServer参数化查询之where in和like实现详解

发布时间:2020-11-14 09:36:42 所属栏目:MsSql 来源:互联网
导读:若有一天你不可避免的需要提高SQL的查询性能,需要一次性where in 几百、上千、甚至上万条数据时,参数化查询将是必然进行的选择

身为一名小小的程序猿,在日常开发中不可以避免的要和where in和like打交道,在大多数情况下我们传的参数不多简单做下单引号、敏感字符转义之后就直接拼进了SQL,执行查询,搞定。若有一天你不可避免的需要提高SQL的查询性能,需要一次性where in 几百、上千、甚至上万条数据时,参数化查询将是必然进行的选择。然而如何实现where in和like的参数化查询,是个让不少人头疼的问题。

where in 的参数化查询实现

首先说一下我们常用的办法,直接拼SQL实现,一般情况下都能满足需要
<div class="codetitle"><a style="CURSOR: pointer" data="66221" class="copybut" id="copybut66221" onclick="doCopy('code66221')"> 代码如下:<div class="codebody" id="code66221">
string userIds = "1,2,3,4";
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
SqlCommand comm = new SqlCommand();
comm.Connection = conn;
comm.CommandText = string.Format("select from Users(nolock) where UserID in({0})",userIds);
comm.ExecuteNonQuery();
}

需要参数化查询时进行的尝试,很显然如下这样执行SQL会报错错误
<div class="codetitle"><a style="CURSOR: pointer" data="57935" class="copybut" id="copybut57935" onclick="doCopy('code57935')"> 代码如下:<div class="codebody" id="code57935">
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
SqlCommand comm = new SqlCommand();
comm.Connection = conn;
comm.CommandText = "select
from Users(nolock) where UserID in(@UserID)";
comm.Parameters.Add(new SqlParameter("@UserID",SqlDbType.VarChar,-1) { Value = "1,4" });
comm.ExecuteNonQuery();
}

很显然这样会报错误:在将 varchar 值 '1,4' 转换成数据类型 int 时失败,因为参数类型为字符串,where in时会把@UserID当做一个字符串来处理,相当于实际执行了如下语句
<div class="codetitle"><a style="CURSOR: pointer" data="35909" class="copybut" id="copybut35909" onclick="doCopy('code35909')"> 代码如下:<div class="codebody" id="code35909">
select from Users(nolock) where UserID in('1,4')

若执行的语句为字符串类型的,SQL执行不会报错,当然也不会查询出任何结果
<div class="codetitle"><a style="CURSOR: pointer" data="75429" class="copybut" id="copybut75429" onclick="doCopy('code75429')"> 代码如下:<div class="codebody" id="code75429">
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
SqlCommand comm = new SqlCommand();
comm.Connection = conn;
comm.CommandText = "select
from Users(nolock) where UserName in(@UserName)";
comm.Parameters.Add(new SqlParameter("@UserName",-1) { Value = "'john','dudu','rabbit'" });
comm.ExecuteNonQuery();
}

这样不会抱任何错误,也查不出想要的结果,因为这个@UserName被当做一个字符串来处理,实际相当于执行如下语句
<div class="codetitle"><a style="CURSOR: pointer" data="56867" class="copybut" id="copybut56867" onclick="doCopy('code56867')"> 代码如下:<div class="codebody" id="code56867">
select from Users(nolock) where UserName in('''john'',''dudu'',''rabbit''')

由此相信大家对于为何简单的where in 传参无法得到正确的结果知道为什么了吧,下面我们来看一看如何实现正确的参数化执行where in,为了真正实现参数化where in 传参,很多淫才想到了各种替代方案 方案1,使用CHARINDEX或like 方法实现参数化查询,毫无疑问,这种方法成功了,而且成功的复用了查询计划,但同时也彻底的让查询索引失效(在此不探讨索引话题),造成的后果是全表扫描,如果表里数据量很大,百万级、千万级甚至更多,这样的写法将造成灾难性后果;如果数据量比较小、只想借助参数化实现防止SQL注入的话这样写也无可厚非,还是得看具体需求。(不推荐) <div class="codetitle"><a style="CURSOR: pointer" data="39113" class="copybut" id="copybut39113" onclick="doCopy('code39113')"> 代码如下:<div class="codebody" id="code39113">
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
SqlCommand comm = new SqlCommand();
comm.Connection = conn;
//使用CHARINDEX,实现参数化查询,可以复用查询计划,同时会使索引失效
comm.CommandText = "select
from Users(nolock) where CHARINDEX(','+ltrim(str(UserID))+',','+@UserID+',')>0";
comm.Parameters.Add(new SqlParameter("@UserID",4" });
comm.ExecuteNonQuery();
} using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
SqlCommand comm = new SqlCommand();
comm.Connection = conn;
//使用like,实现参数化查询,可以复用查询计划,同时会使索引失效
comm.CommandText = "select from Users(nolock) where ',' like '%,%' ";
comm.Parameters.Add(new SqlParameter("@UserID",4" });
comm.ExecuteNonQuery();
}

方案2 使用exec动态执行SQL,这样的写法毫无疑问是很成功的,而且代码也比较优雅,也起到了防止SQL注入的作用,看上去很完美,不过这种写法和直接拼SQL执行没啥实质性的区别,查询计划没有得到复用,对于性能提升没任何帮助,颇有种脱了裤子放屁的感觉,但也不失为一种解决方案。(不推荐)
<div class="codetitle"><a style="CURSOR: pointer" data="65816" class="copybut" id="copybut65816" onclick="doCopy('code65816')"> 代码如下:<div class="codebody" id="code65816">
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
SqlCommand comm = new SqlCommand();
comm.Connection = conn;
//使用exec动态执行SQL
  //实际执行的查询计划为(@UserID varchar(max))select
from Users(nolock) where UserID in (1,4)
  //不是预期的(@UserID varchar(max))exec('select from Users(nolock) where UserID in ('+@UserID+')')
comm.CommandText = "exec('select
from Users(nolock) where UserID in ('+@UserID+')')";
comm.Parameters.Add(new SqlParameter("@UserID",4" });
comm.ExecuteNonQuery();
}

方案3 为where in的每一个参数生成一个参数,写法上比较麻烦些,传输的参数个数有限制,最多2100个,可以根据需要使用此方案(推荐)
<div class="codetitle"><a style="CURSOR: pointer" data="85706" class="copybut" id="copybut85706" onclick="doCopy('code85706')"> 代码如下:<div class="codebody" id="code85706">
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
SqlCommand comm = new SqlCommand();
comm.Connection = conn;
//为每一条数据添加一个参数
comm.CommandText = "select from Users(nolock) where UserID in (@UserID1,@UserId2,@UserID3,@UserID4)";
comm.Parameters.AddRange(
new SqlParameter[]{
new SqlParameter("@UserID1",SqlDbType.Int) { Value = 1},
new SqlParameter("@UserID2",SqlDbType.Int) { Value = 2},
new SqlParameter("@UserID3",SqlDbType.Int) { Value = 3},
new SqlParameter("@UserID4",SqlDbType.Int) { Value = 4}
}); comm.ExecuteNonQuery();
}

方案4 使用临时表实现(也可以使用表变量性能上可能会更加好些),写法实现上比较繁琐些,可以根据需要写个通用的where in临时表查询的方法,以供不时之需,个人比较推崇这种写法,能够使查询计划得到复用而且对索引也能有效的利用,不过由于需要创建临时表,会带来额外的IO开销,若查询频率很高,每次的数据不多时还是建议使用方案3,若查询数据条数较多,尤其是上千条甚至上万条时,强烈建议使用此方案,可以带来巨大的性能提升(强烈推荐)
<div class="codetitle"><a style="CURSOR: pointer" data="52594" class="copybut" id="copybut52594" onclick="doCopy('code52594')"> 代码如下:<div class="codebody" id="code52594">
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
SqlCommand comm = new SqlCommand();
comm.Connection = conn;
string sql = @"
declare @Temp_Variable varchar(max)
create table #Temp_Table(Item varchar(max))
while(LEN(@Temp_Array) > 0)
begin
if(CHARINDEX(',@Temp_Array) = 0)
begin
set @Temp_Variable = @Temp_Array
set @Temp_Array = ''
end
else
begin
set @Temp_Variable = LEFT(@Temp_Array,CHARINDEX(',@Temp_Array)-1)
set @Temp_Array = RIGHT(@Temp_Array,LEN(@Temp_Array)-LEN(@Temp_Variable)-1)
end
insert into #Temp_Table(Item) values(@Temp_Variable)
end
select
from Users(nolock) where exists(select 1 from #Temp_Table(nolock) where #Temp_Table.Item=Users.UserID)
drop table #Temp_Table";
comm.CommandText = sql;
comm.Parameters.Add(new SqlParameter("@Temp_Array",4" });
comm.ExecuteNonQuery();
}

like参数化查询
like查询根据个人习惯将通配符写到参数值中或在SQL拼接都可,两种方法执行效果一样,在此不在详述
<div class="codetitle"><a style="CURSOR: pointer" data="51931" class="copybut" id="copybut51931" onclick="doCopy('code51931')"> 代码如下:<div class="codebody" id="code51931">
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
SqlCommand comm = new SqlCommand();
comm.Connection = conn;
//将 % 写到参数值中
comm.CommandText = "select from Users(nolock) where UserName like @UserName";
comm.Parameters.Add(new SqlParameter("@UserName",200) { Value = "rabbit%" });
comm.ExecuteNonQuery();
} using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
SqlCommand comm = new SqlCommand();
comm.Connection = conn;
//SQL中拼接 %
comm.CommandText = "select
from Users(nolock) where UserName like @UserName+'%'";
comm.Parameters.Add(new SqlParameter("@UserName",200) { Value = "rabbit%" });
comm.ExecuteNonQuery();
}

看到Tom.汤和蚊子额的评论 补充了下xml传参和tvp传参,并对6种方案做了个简单总结 Sql Server参数化查询之where in和like实现之xml和DataTable传参 此文章属懒惰的肥兔原创

(编辑:鄂州站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读