22,181
社区成员




--ID date num
create proc Pro_sum
(
@ID varchar(20)
)
as
select sum(num) from tb where ID in(@ID)
return
exec('a','b')
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 行)
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