SQL Server:使用临时表

时间:2015-02-20 23:12:05

标签: sql sql-server-2008

我是否可以通过调用存储过程获取临时表的值? 由于@productTable是一个临时表,我可以EXEC [dbo.FindProductTotal] 并获取[dbo.updateProductTotal]中的值? 在我看来它不起作用。如果没有,有没有办法获得价值 从第一个临时表的结果在第二个存储过程中执行?真正的编码更复杂,所以我不知道只是做简单的插入和更新表。我需要知道这种编码方式是否适用于我的实际编码。

ALTER PROCEDURE dbo.FindProductTotal (@ProductID int)
AS

DECLARE @productTable TABLE (subProductID int, productCode varchar(10), productDesc varchar(25), chargeAmt money)

    insert into @productTable 
    select productID, productCode, productDesc, chargeAmt
    from   product
    where region='CA'

--calculate charge here

    UPDATE @productTable 
    SET chargeAmt = (calculation here)
    where ProductID = @ProductID


ALTER PROCEDURE dbo.updateProductTotal (@ProductID int)
AS

DECLARE @productTable TABLE (subProductID int, productCode varchar(10), productDesc varchar(25), chargeAmt money)

EXEC dbo.FindProductTotal @ProductID = 123456

    UPDATE dbo.Manufacture
    SET    productChgAmt = p.chargeAmt 
    FROM   @productTable p
    WHERE  productID = @ProductID 

1 个答案:

答案 0 :(得分:0)

    CREATE PROCEDURE dbo.FindProductTotal (@ProductID int)
AS
    IF SELECT OBJECT_ID('tempdb..##productTable') IS NULL
    DROP TABLE ##productTable

    CREATE TABLE ##productTable(subProductID int, productCode varchar(10), productDesc varchar(25), chargeAmt money)

    --DECLARE @productTable TABLE (subProductID int, productCode varchar(10), productDesc varchar(25), chargeAmt money)

    insert into ##productTable
    select productID, productCode, productDesc, chargeAmt
    from   product
    where region='CA'

--calculate charge here

    UPDATE ##productTable
    SET chargeAmt = (calculation here)
    where ProductID = @ProductID


CREATE PROCEDURE dbo.updateProductTotal (@ProductID int)
AS

EXEC dbo.FindProductTotal @ProductID = 123456

    UPDATE dbo.Manufacture
    SET    productChgAmt = p.chargeAmt 
    FROM   ##productTable p
    WHERE  productID = @ProductID