1

我在 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        

这与视图控件完美配合,但我需要选择日期间隔

我已经写了另外两个程序来过滤这个时期

 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,但是现在rollback2返回0有点问题,就像max和min的程序返回任何东西一样。

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

感谢任何方向

亚历杭德罗

4

3 回答 3

3

您不能SELECT从存储过程中获取这样的数据 - 您必须将结果数据放入(临时)表中才能获取它。您可能希望将它们创建为函数,因为它们只是SELECTing 数据,然后您可以内联使用它们。

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)
于 2013-04-29T20:44:42.607 回答
1

就像 zimdanen 所说,一个函数非常适合这个。但是,如果您真的想使用存储过程,您可以使用输出参数并在您的 Minix 和 maxiN 存储过程中设置它。然后,在执行 proc 时引用它们。

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

有关更多信息,请参阅:如何将存储过程的输出返回到 sql server 中的变量中

于 2013-04-29T21:23:40.317 回答
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  
于 2013-04-30T04:33:27.993 回答