在t-sql中调用其他过程的过程

时间:2013-04-29 20:39:21

标签: sql-server sql-server-2008 tsql stored-procedures

我在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过程中插入了这些过程,但是现在有些错误,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

感谢任何方向

亚历

3 个答案:

答案 0 :(得分:3)

您不能从存储过程中SELECT数据 - 您必须将结果数据放入(临时)表中以获取它。您可能希望将它们创建为函数,因为它们只是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 :(得分: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

有关详细信息,请参阅:How to return the output of stored procedure into a variable in sql server

答案 2 :(得分: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的:

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