这里的a,b,c,d作为查询条件是从数组里得到的,具体几个值是不固定的,也就是list[0],到list[list.length-1],那么这个sql查询语句怎么写呢?求大神指点。
declare @val varchar(100)=''
declare @sql varchar(max)
set @val='''a'''+','+'''b'''+','+'''c'''+','+'''d'''
set @sql='select * from tb where id in ('+@val+')'
print @sql
exec (@sql)
declare @sql varchar(max)
set @val='''a'''+','+'''b'''+','+'''c'''+','+'''d'''
set @sql='select * from tb where id in ('+@val+')'
print @sql
exec (@sql)
温馨提示:答案为网友推荐,仅供参考
第1个回答 推荐于2017-09-09
string sql = "select * from table where id in ({0})";
string ids = "";
foreach(var item in list)
{
ids+=",'"+item.ToString()+"'";
}
ids = ids.SubString(1);
sql = string.Formart(sql,ids);本回答被提问者采纳
string ids = "";
foreach(var item in list)
{
ids+=",'"+item.ToString()+"'";
}
ids = ids.SubString(1);
sql = string.Formart(sql,ids);本回答被提问者采纳