写存储过程时 N个参数(n不知道) 怎么弄?

hxjtiger 2009-03-14 03:23:17
table1为
ID date num
a 1 11
b 1 12
c 1 9
a 2 10
b 2 12
c 2 9
a 3 7
b 3 12
c 3 2

写个存储过程计算我要选的ID的num的总和,我在前台给定参数,如 id=a 或 id为a和b 或 id为b和c。
怎么写这个存储过程啊,主要是不知道怎么弄未知个数的参数!
...全文
59 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
ws_hgo 2009-03-14
  • 打赏
  • 举报
回复
--ID  date  num 
create proc Pro_sum
(
@ID varchar(20)
)
as
select sum(num) from tb where ID in(@ID)
return

exec('a','b')
hxjtiger 2009-03-14
  • 打赏
  • 举报
回复
太佩服你了!!!!!!
htl258_Tony 2009-03-14
  • 打赏
  • 举报
回复
if object_id('table1') is not null drop table table1
create table table1(ID varchar(10),date int, num int)
insert table1 select 'a' , 1 , 11
insert table1 select 'b' , 1 , 12
insert table1 select 'c' , 1 , 9
insert table1 select 'a' , 2 , 10
insert table1 select 'b' , 2 , 12
insert table1 select 'c' , 2 , 9
insert table1 select 'a' , 3 , 7
insert table1 select 'b' , 3 , 12
insert table1 select 'c' , 3 , 2

go



if object_id('sp_test') is not null drop proc sp_test
go
create proc sp_test
@n varchar(200)
as
declare @s varchar(3000)
set @s='select sum(num) as SUM_NUM from table1 where ID in ('''+replace(@n,',',''',''')+''')'
exec(@s)
go

exec sp_test 'a,b'

--
SUM_NUM
-----------
64

(所影响的行数为 1 行)
htl258_Tony 2009-03-14
  • 打赏
  • 举报
回复
create proc sp_test
@n varchar(200)
as
declare @s varchar(3000)
set @s='select sum(num) as SUM_NUM from table1 where ID in ('+replace(@n,',',''',''')+')'
exec(@s)
go

22,181

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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