SqlSever not in条件中如何加入字符串(小弟新手,请大哥帮忙)

DLnetchengxuyuan 2012-07-29 07:16:52
各位大哥小弟是新手,遇到个问题,帮帮忙吧!!!

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 的记录都删除了

该怎么办呢???
...全文
363 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
DLnetchengxuyuan 2012-07-30
  • 打赏
  • 举报
回复
大哥们,原本我的需求是

注(两个表有外键关系)
删除ClassTable中的记录 条件ClassId not in 1,2,3,4
同时删除StudnetTable中的记录 条件 ClassId not in 1,2,3,4

虽然可以在c#中写两条delete语句执行,但是小弟现在想用 数据库中的存储过程来解决。。。。请各位大哥指导。。。 小弟谢谢你们了。。。
gaobinbinbin 2012-07-30
  • 打赏
  • 举报
回复

declare @ss varchar(20)
set @ss='2'
declare @sql varchar(100)
set @sql=''
set @sql='delete from course where C# in ('+@ss+')'
exec(@sql)
gaobinbinbin 2012-07-30
  • 打赏
  • 举报
回复

declare @ss varchar(20)
set @ss='2'
declare @sql varchar(100)
set @sql=''
set @sql='delete from course where C# in ('+@ss+')'
exec(@sql)
gaobinbinbin 2012-07-30
  • 打赏
  • 举报
回复

declare @ss varchar(20)
set @ss='2'
declare @sql varchar(100)
set @sql=''
set @sql='delete from course where C# in ('+@ss+')'
exec(@sql)
xinguohit 2012-07-30
  • 打赏
  • 举报
回复
set @ss='5';
delete from ClassTable where ClassId in (@ss);
这么写呢?
DLnetchengxuyuan 2012-07-30
  • 打赏
  • 举报
回复
还是没有成功
lzly0812 2012-07-30
  • 打赏
  • 举报
回复
delete from ClassTable where ClassId not in (+quotename(@NotClassId,'''')+)
delete from StudentTable where ClassId not in (+quotename(@NotClassId,'''')+)
这样试一试
DLnetchengxuyuan 2012-07-29
  • 打赏
  • 举报
回复
这么写,就出错了。。。
DLnetchengxuyuan 2012-07-29
  • 打赏
  • 举报
回复
大哥,小弟原本 是想用到存储过程中的,那么这样行吗

if(exists(select * from sys.objects where name='proc_delete_class_student'))
drop proc proc_delete_class_student
go
create proc proc_delete_class_student(
@NotClassId nvarchar(max)
)
as
delete from ClassTable where ClassId not in (+@NotClassId+)
delete from StudentTable where ClassId not in (+@NotClassId+)
DLnetchengxuyuan 2012-07-29
  • 打赏
  • 举报
回复
谢谢你,大哥。。。。
人生无悔 2012-07-29
  • 打赏
  • 举报
回复

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'而不是一个集合
DLnetchengxuyuan 2012-07-29
  • 打赏
  • 举报
回复
各位大哥,帮帮忙吧

34,587

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧