简体   繁体   English

将存储过程获取的数据插入表中

[英]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 . 如您所见,此过程正在从不同的表中检索多个列,现在在此存储过程的结尾,我想将所有要提取的数据插入表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一个insert into

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 检查此链接https://msdn.microsoft.com/en-us/library/ms174335.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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