简体   繁体   中英

Stored procedure return results into temp table in another stored procedure

What I want to do is insert new transaction that is a copy of the latest transaction but change couple of the values.

I have SP that returning the latest transaction given the request id called sp_Get_YellowCard_Request_Last_Transaction

I have another SP that I want to use the above called sp_Update_CC_Move_YCR_TO .

But since the sp_Get_YellowCard_Request_Last_Transaction returns a lot of fields (26) I don't want to specify all of them and just insert the result of it into temporary table and later change some field, and finally insert the temp table into the destination transaction table.

what i'v writter so far is:

USE [YellowCard_NewDesign]
GO
/****** Object:  StoredProcedure [dbo].[sp_Update_CC_Move_YCR_TO]    Script Date: 02/26/2013 09:43:26 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[sp_Update_CC_Move_YCR_TO]
    @YellowCard_Request_ID int,
    @yc_changer_username nvarchar(MAX),
    @yc_changer_rolename nvarchar(MAX),
    @yc_new_status nvarchar(MAX)
AS
    INSERT INTO #last_transaction
    EXEC dbo.sp_Get_YellowCard_Request_Last_Transaction @YellowCard_Request_ID*

I'm getting an error that #last_transaction is not defined.

How can I complete my SP without defining the temporary table fields?

In SQL Server 2000, I used the following trick:

IF OBJECT_ID('tempdb..#last_transaction') IS NULL
    CREATE TABLE #last_transaction(
        ....
    )

INSERT INTO #last_transaction
    EXEC ...

For later versions of SQL Server, I'd prefer table-valued functions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM