Sql server 使用过程更改视图
这是一个视图的源代码,我使用此视图作为名为buscacancelados的过程的基础:Sql server 使用过程更改视图,sql-server,sql-server-2008,tsql,Sql Server,Sql Server 2008,Tsql,这是一个视图的源代码,我使用此视图作为名为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;
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
我想要这样的东西
SELECT @min = ( SELECT MIN (numero) from filtraperiodo 504,2,2013
将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
无论您在哪里引用controlanum,都将您的3个筛选值传递给它。例如:
--...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
您想用该过程更改视图是什么意思?因为我需要查看不同的间隔和客户机示例504,32012是客户机504 abril 2012对不起,我仍然不明白您想做什么我不确定您想做什么,但我猜您正在寻找一个可以传递参数的。你可以把它看作是一个参数化的视图。我的场景与此稍有不同。我从另一个表中填充具有数周历史记录的唯一命名表,然后从表中删除这些记录。随着时间的推移,我会删除最旧的表,并更改存储过程以合并所需格式的现有表。实际上,它对我们的目的非常有效。在这种情况下,我认为我错了;我认为“庞德生活”是对的;我想他想要一个表值函数。
--...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