2013-04-30 77 views
2

這是一個視圖的源視圖,我用這個觀點作爲一個程序命名buscacancelados的基礎:更改與程序

SELECT  NUMERO FROM dbo.CTRC 
WHERE  (EMITENTE = 504) AND (MONTH(EMISSAODATA) = 3) 
AND (YEAR(EMISSAODATA) = 2013) 

此過程中一組返回丟失號碼

alter proc buscarcancelado (@emp int) as 
begin 
set nocount on; 
declare @min int --- declare the variavels to be used 
declare @max int 
declare @I int 

IF OBJECT_ID ('TEMP..#TempTable') is not null -- Controls if exists this table 
begin 
     drop table #TempTable -- If exist delete 
end 

create table #TempTable 
       (TempOrderNumber int)-- create a temporary table 

SELECT @min = (SELECT MIN (numero)       
       from controlanum with (nolock)) -- search the min value of the set 

SELECT @max = (SELECT Max (numero)       
        from controlanum with (nolock)) -- search the max value of the set  

select @I = @min -- control where begins the while 

while @I <= @max -- finish with the high number 
     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 filtraperiodo (@emp int,@mes int,@ano int)as 
     select numero from ctrc where 
     EMITENTE = 504 
     and MONTH (EMISSAODATA) = 3 and YEAR (EMISSAODATA)=2013 

我想是這樣的

  SELECT @min = (SELECT MIN (numero) from filtraperiodo 504,2,2013 
+0

你是什麼意思你想改變該過程的觀點? – Lamak 2013-04-30 20:17:26

+0

因爲我需要看不同的時間間隔和客戶的例子504,3,2012是客戶端504阿布里爾2012 – 2013-04-30 20:24:00

+0

對不起,我還是不明白它是什麼,你想 – Lamak 2013-04-30 20:24:56

回答

1

創建controlanum作爲表值函數,而不是到處都引用controlanum視圖

IF EXISTS (SELECT * FROM dbo.sysobjects WHERE ID = OBJECT_ID('[dbo].[controlanum]') AND XTYPE IN ('FN', 'IF', 'TF')) 
     DROP FUNCTION [dbo].[controlanum] 
    GO 

    CREATE FUNCTION [dbo].[controlanum] (
     @emp int 
     ,@mes int 
     ,@ano int 
    ) 

    RETURNS @numeros TABLE (numero int) 

    AS 
    BEGIN 

     INSERT @numeros 
     SELECT numero 
     FROM ctrc WITH (NOLOCK) 
     WHERE EMITENTE    = @emp 
     AND MONTH (EMISSAODATA) = @mes 
     AND YEAR (EMISSAODATA)  = @ano 

     RETURN 

    END 
    GO 

,通過它你三個濾值。例如:

--...other code here... 

    SELECT @min = MIN(numero) 
     FROM dbo.controlanum(@emp, @mes, @ano) 

    SELECT @max = MAX(numero) 
     FROM dbo.controlanum(@emp, @mes, @ano) 

    --...other code here... 

    SELECT tempordernumber 
     FROM #TempTable A 
     LEFT JOIN dbo.controlanum(@emp, @mes, @ano) O 
     ON A.TempOrderNumber <> O.numero 
    WHERE O.numero IS NULL