SQL Server:將存儲過程輸出的輸出保存到不同的表中

[英]SQL Server : save output from stored procedure output into a different table


As a simple example, suppose I have the following stored procedure in my SQL Server database that produces an output:

舉個簡單的例子,假設我的SQL Server數據庫中有以下存儲過程,它產生一個輸出:

CREATE PROCEDURE MyProc
    (@Id INT, 
     @Var1 VARCHAR(100))
AS
BEGIN
    SELECT
        Id = @Id,
        Var1 = @Var1,
        Var2 = LTRIM(@Id) + ' - ' + @Var1
END

Now, what I would like to do is have the output from this stored procedure inserted into a table with other columns such as:

現在,我想要做的是將此存儲過程的輸出插入到包含其他列的表中,例如:

DECLARE @NewTable TABLE 
(
     ExtraCols INT, 
     Id INT, 
     Var1 VARCHAR(100), 
     Var2 VARCHAR(MAX)
)

So, I would like to do something along the lines of:

所以,我想做的事情是:

DECLARE @RunVars TABLE (ExtraCols INT, Id INT, Var1 VARCHAR(100))
    INSERT INTO @RunVars (ExtraCols, Id, Var1)
    VALUES (123, 1, 'Test'),
           (456, 2, 'Test 1')

Then, after running each row of @RunVars through the stored procedure, to end up with:

然后,在通過存儲過程運行@RunVars的每一行之后,最終得到:

@NewTable:

ExtraCols  Id       Var1        Var2
---------------------------------------------
123         1       'Test'      '1 - Test'
456         2       'Test 1'    '2 - Test 1'

I know I could create a temporary table to get the data from the stored procedure and then do a join myself, but I'm wondering if there's any better way to accomplish this - kind of a join or cross apply to the stored procedure.

我知道我可以創建一個臨時表來從存儲過程中獲取數據,然后自己進行連接,但我想知道是否有更好的方法來實現這一點 - 一種連接或交叉應用於存儲過程。

1 个解决方案

#1


1  

If you can't modify the proc, and OPENQUERY is frowned upon, then no, there's no better way.

如果你不能修改proc,並且OPENQUERY不贊成,那么不,沒有更好的方法。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2018/03/19/72fe27931d252f6648a608db615f3365.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com