日期:2014-05-17  浏览次数:22332 次

在select中关于in的使用。
VB code

<%
...........
srt="12,23,45,67"
sql="select * from table where id in('"&str&"')" '这样好像不行?
...........
%>



id为int类型,选择str里的id的数据,使用上面的语句好像不能通过。

------解决方案--------------------
SQL code

--模拟数据表
create table tablename(id int)
insert into tablename
select 12 union all
select 13 union all
select 14 union all
select 15 union all
select 16 union all
select 45 union all
select 46 union all
select 47


declare @str varchar(14)
set @str='12,23,45,67'

exec('select * from tablename where id in ('+@str+')')
/*
id
-----------
12
45
*/

------解决方案--------------------
SQL code

declare @str varchar(14)
set @str='12,23,45,67'

select * from tablename where ltrim(id) in (@str)

------解决方案--------------------
改成
<%
...........
srt="12,23,45,67"
sql="select * from table where id in("&str&")" 
...........
%>
就可以了,不要单引号