Sql server 在t-sql中调用其他过程的过程
我在SQL Server 2008中有以下T-SQL过程:Sql server 在t-sql中调用其他过程的过程,sql-server,sql-server-2008,tsql,stored-procedures,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
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
我已经在rolled back
过程中插入了这些过程,但是现在有些问题,rolled back2返回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
只有这样才行
fmin 504,22013
谢谢你的介绍
Alejandro您不能
从存储过程中选择这样的数据-您必须将结果数据放入(temp)表中才能获得它。您可能希望将它们创建为函数,因为它们只是SELECT
ing数据,然后您可以内联使用它们
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)
您不能从存储过程中选择这样的数据,您必须将结果数据放入(temp)表中才能获得它。您可能希望将它们创建为函数,因为它们只是SELECT
ing数据,然后您可以内联使用它们
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)
正如zimdanen所说,函数非常适合于此。但是,如果确实要使用存储过程,可以使用output参数并在Minix和maxiN存储过程中进行设置。然后,在执行过程时引用它们
create proc myproc
@b varchar(50) output
as
begin
select @b='hello'
end
go
declare @j varchar(50)
exec myproc @j output
有关更多信息,请参见:正如zimdanen所说,函数非常适合于此。但是,如果确实要使用存储过程,可以使用output参数并在Minix和maxiN存储过程中进行设置。然后,在执行过程时引用它们
create proc myproc
@b varchar(50) output
as
begin
select @b='hello'
end
go
declare @j varchar(50)
exec myproc @j output
有关更多信息,请参阅:这可能对您有所帮助-
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 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
执行官: