这个存储过程该如何处理?

chenjiong 2011-01-30 02:29:28
举例:

A表 有字段 ID Name(varchar)
已有记录1条: 12345 例子



现有从客户端输入的ID的集如:
12345,54321,111111


想用这个集作为输入参数传入后,查询表A如:

update A set Name=xxx where ID in (12345,54321,111111)

以上都简单,查出的也是1条记录12345那条。但我想把没查到的'54321,111111' 返回,应该怎么办?

初步的想法,是把传入的'12345,54321,111111',写到一个有ID字段的表变量里,然后子查询,把not in A表的记录查询出来,再以结果集方式返回。


问题一:
此外还有没有其它的好办法?

问题二:
如果用这个想法的话,把'12345,54321,111111'插入到表变量里时,用BULK INSERT 的话,似乎是要先保存到文件去的。那可不可以直接从字符串中直接导入?
...全文
167 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
-晴天 2011-01-30
  • 打赏
  • 举报
回复
create table tb(id int,name nvarchar(10))
insert into tb select 54321,'aaaaa'
insert into tb select 111111,'bbbbb'
go
declare @sid nvarchar(20)
set @sid='12345,54321,111111'
set @sid=@sid+','
select @sid=replace(@sid,convert(varchar,id)+',','') from tb
select left(@sid,len(@sid)-1)
go
drop table tb
/*
--------------------
12345

(1 行受影响)

*/
-晴天 2011-01-30
  • 打赏
  • 举报
回复
create table tb(id int,name nvarchar(10))
insert into tb select 54321,'aaaaa'
go
declare @sid nvarchar(20)
set @sid='12345,54321,111111'
set @sid=@sid+','
select @sid=replace(@sid,convert(varchar,id)+',','') from tb
select left(@sid,len(@sid)-1)
go
drop table tb
/*
--------------------
12345,111111

(1 行受影响)

*/
feixianxxx 2011-01-30
  • 打赏
  • 举报
回复
写个函数...

create function f_sss (@s varchar(1000))
returns varchar(1000)
as
begin
declare @s3 varchar(100),@str nvarchar(4000)
exec(update A set Name=xxx where ID in ('+@s+')';
@str='select @s3=id from a wheer id in('+@s+')';
exec sp_exeutesql @str,N'@s3 varchar(100),@s varchar(1000) ',@s3 =@s3
SET @s3=replace(@s+',',@s3+',','')
return @3

end
-晴天 2011-01-30
  • 打赏
  • 举报
回复
create table tb(id int,name nvarchar(10))
insert into tb select 12345,'aaaaa'
go
declare @sid nvarchar(20)
set @sid='12345,54321,111111'
set @sid=@sid+','
select @sid=replace(@sid,convert(varchar,id)+',','') from tb
select left(@sid,len(@sid)-1)
go
drop table tb
/*
--------------------
54321,111111

(1 行受影响)
*/
叶子 2011-01-30
  • 打赏
  • 举报
回复

declare @table table (ID int,Name varchar(4))
insert into @table
select 12345,'例子'

declare @parm varchar(20)
set @parm='12345,54321,111111'

select * from @table where
','+@parm+',' like '%,'+cast(ID as varchar(20))+',%'
/*
ID Name
----------- ----
12345 例子
*/
AcHerat 2011-01-30
  • 打赏
  • 举报
回复
刚看到,就被你们回了。顶楼上两位。
叶子 2011-01-30
  • 打赏
  • 举报
回复

/*按照符号分割字符串*/
create function [dbo].[m_split](@c varchar(2000),@split varchar(2))
returns @t table(col varchar(200))
as
begin
while(charindex(@split,@c)<>0)
begin
insert @t(col) values (substring(@c,1,charindex(@split,@c)-1))
set @c = stuff(@c,1,charindex(@split,@c),'')
end
insert @t(col) values (@c)
return
end



select * from dbo.m_split('12345,54321,111111',',')
/*
col
-----------
12345
54321
111111
*/
昵称被占用了 2011-01-30
  • 打赏
  • 举报
回复
问题一:按你的思路做吧,不做不知道有问题
问题二:用个字符串拆分处理就可以了,不要想得太复杂

27,582

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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