如何将存储过程输出保存到表中

时间:2015-04-22 02:23:25

标签: sql sql-server tsql

我是T-SQL的新手,所以如果我说错了我的话,我会道歉。

我有一个SQL Server存储过程,它具有属性并返回整数值,也从不同的表中选择一些列。

我想知道如何将该结果存储到常规表

DECLARE @prop1 int;
DECLARE @prop2 int;
DECLARE @result int;

set @prop1 = 2
set @prop2 = 5

exec @result = dbo.Proc1 @prop1    @prop2

结果:

id    name      value
---------------------
1     Example     6
2     Process     8
..    ........    ..

提前感谢您的所有意见。

// -----------编辑04/22/2015 --------------------------- < / p>

我正在尝试使用下面的T-SQL:

  SELECT * INTO Tx.ReportFacts FROM OPENROWSET('SQLNCLI',   'Server=GCA_A;Trusted_Connection=yes;',
   'SET NOCOUNT ON;SET FMTONLY OFF; EXEC @RC =   ReportLibrary.Rpt.Server_GetAllDiscrepancies @FacilityKey ,@StationKeys  ,@MedItemKeys ,@MedClassCodes ,@UserAccountKeys ,@ResolutionStatus  ,@TransactionLimit ,@ReportStartDate ,@ReportEndDate')
  -- Select Table
  SELECT *
  FROM Tx.ReportFacts;

但是得到以下错误:

OLE DB provider "SQLNCLI11" for linked server "(null)" returned message "Deferred prepare could not be completed.".
Msg 8180, Level 16, State 1, Line 1
Statement(s) could not be prepared.
Msg 137, Level 15, State 2, Line 1
Must declare the scalar variable "@RC".

任何人都可以指出为什么会这样。我的T-SQL语句出了什么问题?

1 个答案:

答案 0 :(得分:3)

您可以使用insert - exec。

试试这个:

insert into table_name
exec dbo.Proc1 @prop1 @prop2;