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

SqlSever not in条件中如何加入字符串(小弟新手,请大哥帮忙)
各位大哥小弟是新手,遇到个问题,帮帮忙吧!!!

declare @ss varchar(50);
set @ss='1,2,3,4';
delete from ClassTable where ClassId not in (@ss);





  各位大哥,假如我只想删除ClassTable表中 ClassId=5 的记录,可是现在却把 classId=1, 2 , 3 4 的记录都删除了
   
该怎么办呢???

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

declare @ss varchar(50),@sql varchar(max);
set @ss='1,2,3,4';
set @sql='delete from ClassTable where ClassId not in ('+@ss+')';
exec(@sql);
--@ss是字符串,你那样写是按一个字符串'1,2,3,4'而不是一个集合

------解决方案--------------------
delete from ClassTable where ClassId not in (+quotename(@NotClassId,'''')+)
delete from StudentTable where ClassId not in (+quotename(@NotClassId,'''')+)
这样试一试
------解决方案--------------------
set @ss='5';
delete from ClassTable where ClassId in (@ss);
这么写呢?