2013-04-29 24 views
1

我有這個T-SQL過程中的SQL Server 2008:過程調用T-SQL等程序

create procedure rolledback as 
begin 
    set nocount on ; 

    declare @min int 
    declare @max int 
    declare @I INT 

    IF OBJECT_ID ('TEMDB..#TempTable') IS NOT NULL 
    begin 
     drop table #TempTable 
    end 
    create table #TempTable 
     (TempOrderNumber int ) 
    SELECT @min = (SELECT MIN (numero)       
       from controlanum with (nolock)) 

    SELECT @max = (SELECT Max (numero)       
       from controlanum with (nolock)) 

select @I = @min 

while @I <= @max 
    begin 
      insert into #TempTable 
      select @I 
      select @I = @I + 1    
    end    
    select tempordernumber from #TempTable    
    left join controlanum O with (nolock) 
    on TempOrderNumber = o.numero where o.numero is null        
end   

這可以完美兼容視圖controlanum,但我需要選擇的日期間隔

我已經寫了另外兩個程序來過濾此期間

create proc maxiN (@emp int, @mes int, @ano int) 
as 
    select Max (numero) 
    from ctrc 
    WHERE (EMITENTE = @emp) 
    AND (MONTH(EMISSAODATA) = @mes) AND (YEAR(EMISSAODATA) = @ano) 

create proc Minix (@emp int, @mes int, @ano int) 
as 
    select Min (numero) 
    from ctrc 
    WHERE (EMITENTE = @emp) 
    AND (MONTH(EMISSAODATA) = @mes) AND (YEAR(EMISSAODATA) = @ano) 

我插在rolledback程序,這些程序,但什麼是錯的,現在rolledback2返回0,就像是在p最大值和最小值的rocedures返回任何東西。

create procedure rolledback2 (@emp int, @mes int, @ano int) as 
begin 
    set nocount on ; 

    declare @min int 
    declare @max int 
    declare @I INT 


    IF OBJECT_ID ('TEMDB..#TempTable') IS NOT NULL 
     begin 
      drop table #TempTable 
     end 
    create table #TempTable 
     (TempOrderNumber int ) 
    exec @min = min2 @emp,@mes ,@ano 

    exec @min = min2 @emp,@mes ,@ano 

    select @I = @min 

    while @I <= @max 
     begin 
      insert into #TempTable 
      select @I 
      select @I = @I + 1    
     end    
    select tempordernumber from #TempTable    
    left join controlanum O with (nolock) 
    on TempOrderNumber = o.numero where o.numero is null        
    end   

與功能不返回任何

declare @min int 
    select @min = fmin 504,2,2013 

只能這樣

FMIN 504,2,2013

感謝任何方向

亞歷

回答

3

您不能從存儲過程中獲得類似數據的SELECT數據 - 您必須將結果數據放入(臨時)表中才能獲取數據。您可能希望將它們創建爲函數,因爲它們只是數據,然後可以使用它們內聯。

CREATE FUNCTION maxiN 
(
    @emp int, 
    @mes int, 
    @ano int 
) 
RETURNS int 
AS 
BEGIN 
    DECLARE @Result INT; 

    select @Result = Max (numero) 
        from ctrc 
        WHERE (EMITENTE = @emp) 
          AND (MONTH(EMISSAODATA) = @mes) 
          AND (YEAR(EMISSAODATA) = @ano); 

    RETURN @Result; 
END 
GO 

然後在你的存儲過程中使用它們:

SELECT @max = maxIN(@emp, @mes ,@ano) 
1

像zimdanen說,一個功能是爲這個完美的。但是,如果您確實想要使用存儲過程,則可以使用輸出參數並將其設置在Minix和MaxiN存儲過程中。然後,在執行過程時引用它們。

create proc myproc 
    @b varchar(50) output 
as 
begin 
    select @b='hello' 
end 
go 
declare @j varchar(50) 
exec myproc @j output 

詳情參見:How to return the output of stored procedure into a variable in sql server

0

可能這是有幫助的你 -

CREATE PROCEDURE dbo.rolledback 
AS BEGIN 

    SET NOCOUNT ON; 

    DECLARE 
      @max INT 
     , @min INT 

    DECLARE @TempTable TABLE 
    (
     TempOrderNumber INT 
    ) 

    SELECT 
      @min = MIN(numero) 
     , @max = MAX(numero) 
    FROM dbo.controlanum WITH(NOLOCK) 

    INSERT INTO @TempTable (TempOrderNumber) 
    SELECT d.num 
    FROM (
     SELECT num = @min + ROW_NUMBER() OVER (ORDER BY sv.number) 
     FROM [master].dbo.spt_values sv 
     LEFT JOIN [master].dbo.spt_values sv2 ON @max > 2048 AND sv2.[type] = 'P' AND sv2.number < 5 
     WHERE sv.[type] = 'P' 
    ) d 
    WHERE num <= @max 

    SELECT TempOrderNumber 
    FROM @TempTable    
    LEFT JOIN controlanum o WITH(NOLOCK) ON TempOrderNumber = o.numero 
    WHERE o.numero IS NULL 

END 

,並回答你的問題:

CREATE PROC dbo.maxiN 
(
     @emp INT 
    , @mes INT 
    , @ano INT 
) 
AS BEGIN 

    DECLARE @result INT 

    SELECT @result = MAX(numero) 
    FROM ctrc 
    WHERE EMITENTE = @emp 
     AND MONTH(EMISSAODATA) = @mes 
     AND YEAR(EMISSAODATA) = @ano 

    RETURN @result 

END 

高管:

EXEC @max = dbo.maxiN @emp2, @mes2, @ano2