0

私はデータベースとsql-server2008を初めて使用します。次のような手順があります。

CREATE PROCEDURE P @myint as int
AS
BEGIN

CREATE TABLE #temp (Quantity smallint, Timing smallint)

INSERT INTO #temp

SELECT 
    Order.quantity as 'Quantity',
    Order.ValidUntil - Order.ValidFrom / X

FROM
    Order

WHERE 
    Order.id = 123

SELECT * FROM #temp

DROP TABLE #temp

END

ここで問題は、「X」について言及した2番目の列の上記のselectステートメントにあります。このXの場合、テーブルを返す別のプロシージャを実行した後、値が必要であり、そのテーブルの特定の列の値を使用します。

だから、Xの代わりに私は次のようなものを書きたい

create table #tmp (col1 nvarchar(512), col2 smalldatetime, col3 smalldatetime, col4 int, col5 float)
Insert into #tmp EXEC ProcedureHere 6, '20130101', '20131231', 0, 400
select col4 from #tmp
4

1 に答える 1

1

プロシージャでは、出力属性を使用してパラメータを設定する必要があります。パラメータをOUT / OUTPUTとして定義すると、プロシージャの実行が終了した後に値が使用可能になります。

--first declare all variables with the same type as table #tmp fields     
--remember: It's a better design put the declare block in the top of the procedure
declare @p1 nvarchar(512), 
        @p2 smalldatetime, 
        @p3 smalldatetime,
        @p4 int,
        @p5 float

--create the table  
create table #tmp (col1 nvarchar(512), col2 smalldatetime, col3 smalldatetime, col4 int, col5 float)

--call the procedure
EXEC ProcedureHere @p1, @p2, @p3, @p4, @p5             

--insert data into temporary table
Insert into #tmp 
  select @p1, @p2, @p3, @p4, @p5

--read col4 
select col4 from #tmp 
--or 
select @p4

プロシージャDDL:

if another parameters is required, you simply add then in the mark (*):
Create Procedure ProcedureHere(
        @p1 nvarchar(512) output, 
        @p2 smalldatetime output, 
        @p3 smalldatetime output,
        @p4 int output,
        @p5 float output,
        *) as
begin
  .
  DoStuff
  .
  --define @p1 result value
  select @p1 = select something from somewhere
  --so on for the others parameters      
  .
end
go
于 2013-03-22T13:49:41.143 に答える