简体   繁体   中英

Insert into table data fetched by stored procedure

I have a stored procedure which is fetching data from different tables with the help of joins.

Here is how it looks

ALTER PROCEDURE profinalinstexpensesonid
    (@from varchar(50),
     @to varchar(50),
     @trainer varchar(50),
     @sonvinid varchar(50)
    )          
AS              
BEGIN              
    SELECT
        instructoreexpense.sonvinid,
        CONVERT(VARCHAR, sonvininsert.date, 105) AS date,
        sonvininsert.brandname,              
        SUBSTRING(sonvininsert.zone, 1, 1) AS zone,              
        sonvininsert.location,              
        sonvininsert.area,              
        companysonvinunitvenue.venuename,            
        sonvininsert.venue,              
        sonvininsert.instructore,              
        sonvininsert.trainingno,              
        instructoreexpense.amount,              
        finalinstructoreexpense.amount AS amount1,              
        finalinstructoreexpense.utno,              
        finalinstructoreexpense.paymentid,              
        CONVERT(VARCHAR, finalinstructoreexpense.issuedate, 105) AS issuedate
    FROM
        instructoreexpense               
    LEFT OUTER JOIN   
        sonvininsert ON sonvininsert.sonvinid = instructoreexpense.sonvinid
                     AND sonvininsert.status = '0'                
    LEFT OUTER JOIN
        finalinstructoreexpense ON finalinstructoreexpense.sonvinid = instructoreexpense.sonvinid                
    LEFT OUTER JOIN
        companysonvinunitvenue ON companysonvinunitvenue.id = sonvininsert.comsonvinid                         
    WHERE
        sonvininsert.date BETWEEN CONVERT(DATETIME, @from, 105)  
                              AND CONVERT(datetime, @to, 105)     
        AND sonvininsert.trainer = (SELET empname 
                                    FROM trainerdetails 
                                    WHERE trid = @trainer)
        AND instructoreexpense.sonvinid NOT IN (SELECT CAST(Item AS INTEGER)
                                                FROM SplitString(@sonvinid, ','))
    ORDER BY
        instructoreexpense.sonvinid
END 

As you can see this procedure is retrieving multiple columns from different tables, now at the end of this stored procedure, I want to insert all the data which is being fetched into table 2 invoice .

What do I need to do here?

I just want to insert the data fetched by this stored procedure into another table, and I want to do this in this stored procedure itself.

I hope am able to make you understand

You can simply put an insert into before the select:

INSERT INTO invoice (<Columns list>)
SELECT ....
INSERT INTO invoice (<<column list>>)
EXEC profinalinstexpensesonid @from=..., @to=...

Remember that the order of the column list specified for invoice table should be the same as the output of the query.

Check this link https://msdn.microsoft.com/en-us/library/ms174335.aspx

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