日期:2014-05-17 浏览次数:20724 次
create proc usp_test
@p0 varchar(500)
as
begin
create table #ta(id varchar(50))
insert into #ta
select @p0
;with cte as
(
select left(id,CHARINDEX(',',id)-1) as a,
right(id,len(id)-CHARINDEX(',',id))b
from #ta
union all
select left(b,CHARINDEX(',',b)-1) as a,
right(b,len(b)-CHARINDEX(',',b))b
from cte
where len(b)>1
)
select a from cte
drop table #ta
end
go
exec usp_test '1,2,3,4,5,'
---------------------------------------------
(1 行受影响)
a
--------------------------------------------------
1
2
3
4
5
--创建自定义类型,这里的自定义类型是表,可存储记录
CREATE TYPE MyTable AS TABLE
( a VARCHAR(50),
b INT )
GO
--drop proc proc_table
--创建存储过程,传入参数为自定义表
CREATE Procedure dbo.proc_table
(@ManyRows as MyTable readonly
)
as
select * from @manyrows
go
declare @t MyTABLE
--可以直接把数据添加到LocationTableType类型中
--可以存储多条记录
insert into @t
select '123',1
union all
select 'abc',2
exec proc_table @t
/*
a b
-------------------------------------------------- -----------
123 1
abc 2
(2 行受影响)
*/
alter proc usp_test
@p0 varchar(500)
as
begin
create table #ta(id varchar(500))
insert into #ta
select @p0
&n