参考:把存储过程记录保存到表中create table #table(id int identity,txt varchar(1000))
insert into #table(txt) exec master..xp_cmdshell 'dir c:\*.'
select * from #table用存储过程插入数据
在INSERT 语句中可以通过执行存储过程来取得要插入的数据所插入的数据是存储
过程中SELECT 语句所检索的结果集使用存储过程插入数据的语法如下
INSERT [INTO]
{ table_name WITH ( <table_hint_limited> [...n])
| view_name
| rowset_function_limited }
{ [(column_list)]
EXECUTE procedure
其中procedure 既可以是一个已经存在的系统存储过程或用户自定义的存储过程也
可以在INSERT 语句中直接编写存储过程
例11-4 对每个部门求员工工资总额并把结果存入department_info 表中
use pangu
insert into department_info(dept_id, d_wage)
execute ('select dept_id, sum(e_wage)
from employee
group by dept_id')
select * from department_info
运行结果如下
(7 row(s) affected)
dept_id d_chief_name d_location e_num d_wage
------- -------------------- -------------------------------------------------- ------ ------------
1001 dbo NULL NULL 15000.0000
1002 dbo NULL NULL 19500.0000

解决方案 »

  1.   

    好像不行。我用
    Select * From (exec sp1 para1,para2) as tab1
    报错
      

  2.   

    好象不可以select 
    只允许
    inert into xx exec procedure
      

  3.   

    直接引用不行.可以将存储过程返回的值保存到临时表中,再引用临时表:
    类似下面这样:--创建两个临时表,用来保存存储过程的返回值,表结构与你的存储过程返回的结果要一致
    create table #tb1(f1 varchar(10),f2 varchar(10))
    create table #tb2(f3 varchar(10),f4 varchar(10))--调用存储过程,并将结果保存到临时表中
    insert #tb1 exec SP1
    insert #tb2 exec SP2--引用临时表中的数据来实现你的查询要求
    SELECT * FROM 
        (Select f1,f2 From #tb1) as tab1,
        (Select f3,f4 From #tb2) as tab2--处理完成后,删除临时表
    drop table #tb1,#tb2
      

  4.   

    测试通过在Select中引用函数。set nocount on
    select  TotalSize , dbo.UM_SysInfo_GetMailUser(@OUId, CurrentSize) CurrentNum,dbo.UM_SysInfo_GetMailUserNumber(@OUID) UserNumber
     from Organization where OUID = @OUID--下面为UM_SysInfo_GetMailUser
    CREATE FUNCTION UM_SysInfo_GetMailUser
    (@OUID int,
    @MailboxSize int)
    RETURNS int AS  
    BEGIN 
    declare @sql nvarchar(256)
    declare @CurrentNum int
    --select @CurrentNum = Count(defaultsize) from mailboxinfo where defaultsize =  @MailboxSize and OUID = @OUID
    select @CurrentNum = sum(defaultsize)-2 from mailboxinfo where OUID = @OUID
    return @CurrentNum
    END