2016-05-12 82 views
0

我想从过程1的AddID1插入数据到程序2的AddTempData。但我做不到。如何使用另一个过程将数据插入到存储过程中的临时表中?

create proc AddID1 
as 
begin 
declare @TempData table 
(
    ID int 
) 
insert into @TempData select P.Id from Product as P 
select * from @TempData 
end 

create proc AddTempData 
as 
begin 
declare @TempDataID table 
(
    IDTemp int 
) 
insert into @TempDataID exec AddID1 
select * from @TempDataID 
end 
+0

做什么你的意思是“但我做不到。”? –

+0

我想插入数据到过程2 AddTempData从过程1 AddID1.But当我从@TempDataID IDTemp选择*为null – user3917715

回答

1

下面是一个完整的工作示例。

至于你的代码.....也许尝试

insert into @TempDataID (IDTemp) 
    exec AddID1 

但下面的工作实例:

/* START TSQL CODE */ 

/* Stored Procedure Definition */ 

Use Northwind 
GO 


IF EXISTS 
    (
    SELECT * FROM INFORMATION_SCHEMA.ROUTINES 
    WHERE ROUTINE_TYPE = N'PROCEDURE' and ROUTINE_SCHEMA = N'dbo' and ROUTINE_NAME = N'uspOrderDetailsByCustomerId' 
    ) 
BEGIN 
    DROP PROCEDURE [dbo].[uspOrderDetailsByCustomerId] 
END 


GO 

CREATE Procedure dbo.uspOrderDetailsByCustomerId 
(
    @CustomerID nchar(5) 
) 
AS 

BEGIN 

    SET NOCOUNT ON 



     SELECT 
      c.CustomerID, c.CompanyName /*,c.ContactName,c.ContactTitle,c.[Address],c.City,c.Region,c.PostalCode,c.Country ,c.Phone,c.Fax */ 
     FROM 
      Customers c 
      JOIN Orders o ON c.CustomerID = o.CustomerID /* this join here just to provide extra rows for the example */ 
     WHERE 
      c.CustomerID = @CustomerID 

END 

现在填入,先用#TEMP表。

IF OBJECT_ID('tempdb..#TempCustomer') IS NOT NULL 
    begin 
      drop table #TempCustomer 
    end 


    CREATE TABLE #TempCustomer 
    ( 
     [CustomerID] nchar(5) 
    , [CompanyName] nvarchar(40) 
    ) 

INSERT INTO #TempCustomer ([CustomerID] , [CompanyName]) 
exec dbo.uspOrderDetailsByCustomerId 'ALFKI' 

Select * from #TempCustomer 


    IF OBJECT_ID('tempdb..#TempCustomer') IS NOT NULL 
    begin 
      drop table #TempCustomer 
    end 

您也可以使用@Variable表(如你的例子)

declare @VariableTableCustomer table 
    ( 
     [CustomerID] nchar(5) 
    , [CompanyName] nvarchar(40) 
    ) 

INSERT INTO @VariableTableCustomer ([CustomerID] , [CompanyName]) 
exec dbo.uspCustomerByCustomerId 'ALFKI' 

Select * from @VariableTableCustomer 

我只是跑所有的代码(减去一行),它的工作原理:

--drop proc AddID1 
GO 

create proc AddID1 
as 
begin 
declare @TempData table 
(
    ID int 
) 


insert into @TempData 
/*select P.Id from Product as P */ 
/* note, i'm commenting out the above line and using the below line since I don't have your db table/rows */ 
select 333 union all select 444 union all select 555 


select * from @TempData 
end 


GO 



/* make sure hte procedure works...as you desire..before trying to stuff it into a temp or varaible table */ 
EXEC AddID1 

GO 




--drop proc AddTempData 
GO 


create proc AddTempData 
as 
begin 
declare @TempDataID table 
(
    IDTemp int 
) 
insert into @TempDataID (IDTemp) exec AddID1 
select 'Inside_AddTempData' as MyPlace , * from @TempDataID 
end 

GO 

EXEC AddTempData 
+0

'ALFKI'这是什么? – user3917715

+0

插入@TempDataID(IDTemp)exec AddID1。我试过,但是当我从TempDataID中选择*时,IDTemp为空 – user3917715

+0

ALFKI适用于我的Northwind示例。请注意“使用Northwind”。 Northwind是微软发布的一个数据库,大多数人拥有它......它可以用来编写例子...... – granadaCoder

相关问题