定義一個存儲過程如下:
create proc [dbo].[test1]
@id int
as
select 1 as id,'abc' as name union all
select @id as id,'zzz' as name
傳回兩行資料.
現在想用sql語句來調用這個存儲過程,并把他傳回的表放入變量中.可以如下做:
declare @table table(id int,name varchar(50))--定義表變量來存放存儲過程傳回的内容
insert into @table exec test1 2--将存儲過程執行的結果放入表變量中
select *
from @table --檢視表變量中的結果